Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"worlds": "saves"
},
"input": {
"player_forward": [0, 200, 910],
"player_backward": [1, 201, 911],
"player_left": [2, 202, 912],
"player_right": [3, 203, 913],
"player_jump": [5, 210],
"player_forward": [ 900, 200, 910 ],
"player_backward": [ 901, 201, 911 ],
"player_left": [902, 202, 912],
"player_right": [903, 203, 913],
"player_jump": [4, 210],
"player_sneak": [211],
"item_action_left": [100, 204],
"item_action_right": [101, 205],
"scroll_left": [9, 208, 213],
"scroll_right": [8, 209, 212],
"item_action_left": [5, 204],
"item_action_right": [100, 205],
"scroll_left": [9, 2, 208, 213],
"scroll_right": [8, 3, 209, 212],
"inventory": [6, 206],
"open_menu": [10, 214],
"gui_up": [0, 200, 900, 910, 920],
Expand All @@ -22,6 +22,7 @@
"gui_right": [3, 203, 903, 913, 923],
"gui_click": [4, 204],
"gui_click_alt": [5, 205],
"screenshot": [7],
"screenshot": [ 7 ],
"lock_camera": [101],
}
}
22 changes: 14 additions & 8 deletions source/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "../cglm/cglm.h"

const float cameraSensitivity = 0.0002;

// all vectors normalized
static float plane_distance(vec3 n, vec3 p0, vec3 l0, vec3 l) {
float d = glm_vec3_dot(n, l);
Expand Down Expand Up @@ -104,10 +106,12 @@ void camera_ray_pick(struct world* w, float gx0, float gy0, float gz0,
void camera_physics(struct camera* c, float dt) {
assert(c);

float jdx, jdy;
if(input_joystick(dt, &jdx, &jdy)) {
c->rx -= jdx * 2.0F;
c->ry -= jdy * 2.0F;
if(!input_held(IB_LOCK_CAMERA)){
float px, py, prot;
if(input_pointer(&px, &py, &prot)) {
c->rx -= (px-gfx_width()/2)*cameraSensitivity;
c->ry += (py-gfx_height()/2)*cameraSensitivity;
}
}

float acc_x = 0, acc_y = 0, acc_z = 0;
Expand Down Expand Up @@ -205,10 +209,12 @@ void camera_attach(struct camera* c, struct entity* e, float tick_delta,
c->y = pos_lerp[1];
c->z = pos_lerp[2];

float jdx, jdy;
if(e->data.local_player.capture_input && input_joystick(dt, &jdx, &jdy)) {
c->rx -= jdx * 2.0F;
c->ry -= jdy * 2.0F;
if(!input_held(IB_LOCK_CAMERA)){
float px, py, prot;
if(e->data.local_player.capture_input && input_pointer(&px, &py, &prot)) {
c->rx -= (px-gfx_width()/2)*cameraSensitivity;
c->ry += (py-gfx_height()/2)*cameraSensitivity;
}
}

c->ry = glm_clamp(c->ry, glm_rad(0.5F), GLM_PI - glm_rad(0.5F));
Expand Down
18 changes: 15 additions & 3 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,22 @@ int main(void) {
world_pre_render(&gstate.world, &gstate.camera, gstate.camera.view);

struct camera* c = &gstate.camera;

float px, py, prot;
input_pointer(&px, &py, &prot);

float aspectRatio = gfx_width()/gfx_height();

float xFov = gstate.config.fov;
float yFov = xFov / aspectRatio;

float newRX = c->rx-((px-gfx_width()/2)/(gfx_width()/xFov)*aspectRatio*2*(3.14/180));
float newRY = c->ry+((py-gfx_height()/2)/(gfx_height()/yFov)*(3.14/180));

camera_ray_pick(&gstate.world, c->x, c->y, c->z,
c->x + sinf(c->rx) * sinf(c->ry) * 4.5F,
c->y + cosf(c->ry) * 4.5F,
c->z + cosf(c->rx) * sinf(c->ry) * 4.5F,
c->x + sinf(newRX) * sinf(newRY) * 4.5F,
c->y + cosf(newRY) * 4.5F,
c->z + cosf(newRX) * sinf(newRY) * 4.5F,
&gstate.camera_hit);
} else {
world_pre_render_clear(&gstate.world);
Expand Down
1 change: 1 addition & 0 deletions source/platform/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ static const char* input_config_translate(enum input_button key) {
case IB_GUI_CLICK: return "input.gui_click";
case IB_GUI_CLICK_ALT: return "input.gui_click_alt";
case IB_SCREENSHOT: return "input.screenshot";
case IB_LOCK_CAMERA: return "input.lock_camera";
default: return NULL;
}
}
Expand Down
1 change: 1 addition & 0 deletions source/platform/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum input_button {
IB_RIGHT,
IB_ACTION1,
IB_ACTION2,
IB_LOCK_CAMERA,
IB_JUMP,
IB_SNEAK,
IB_INVENTORY,
Expand Down