diff --git a/config.json b/config.json index 6bfdf7a4..1f6cdff7 100644 --- a/config.json +++ b/config.json @@ -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], @@ -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], } } diff --git a/source/game/camera.c b/source/game/camera.c index b4c58252..f6f4523f 100644 --- a/source/game/camera.c +++ b/source/game/camera.c @@ -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); @@ -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; @@ -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)); diff --git a/source/main.c b/source/main.c index 2ff16ef1..e67e8a6c 100644 --- a/source/main.c +++ b/source/main.c @@ -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); diff --git a/source/platform/input.c b/source/platform/input.c index 1131f8c1..7bd69a20 100644 --- a/source/platform/input.c +++ b/source/platform/input.c @@ -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; } } diff --git a/source/platform/input.h b/source/platform/input.h index aa812db2..f532d4b1 100644 --- a/source/platform/input.h +++ b/source/platform/input.h @@ -29,6 +29,7 @@ enum input_button { IB_RIGHT, IB_ACTION1, IB_ACTION2, + IB_LOCK_CAMERA, IB_JUMP, IB_SNEAK, IB_INVENTORY,