diff options
Diffstat (limited to '')
| -rw-r--r-- | platformer/src/controls.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/platformer/src/controls.c b/platformer/src/controls.c index 3bcf7b7..9a400b9 100644 --- a/platformer/src/controls.c +++ b/platformer/src/controls.c @@ -64,6 +64,20 @@ * Also clamp the final length to 1, because some pads report corners past 1. * Use MOVE_DEADZONE / LOOK_DEADZONE from config.h. * + * - Axis snap, RIGHT stick only (after its deadzone): a thumb pushing "up" + * is rarely exactly vertical, and with a radial deadzone that small lean + * passes straight through as camera yaw (5 deg off = 12.5 deg/s of drift). + * So pushes within LOOK_AXIS_SNAP_DEG of an axis become exactly that axis, + * and the rest of each 45 deg octant is stretched to fill the gap: an + * angle `off` from the nearest axis maps to + * 0 if off <= snap + * (off - snap) * 45 / (45 - snap) otherwise + * so there's no jump at the snap edge and true diagonals stay diagonal. + * The tilt amount is kept, so full tilt is still full speed. Inside the + * snap, return the other component as EXACTLY 0 (not cos(90 deg), which + * is only nearly 0). The left stick doesn't get this: movement must + * stay free in every direction. + * * - Keyboard fallback, used ONLY when no gamepad is connected: * W/S/A/D -> move (normalize diagonals so the length is <= 1) * Arrow keys -> look @@ -145,6 +159,33 @@ static Vec2 apply_radial_deadzone(float x, float y, float deadzone) return vec2_make(x * scale, y * scale); } +/* Axis snap for the camera stick (see the prompt above for the why). */ +static Vec2 snap_to_axes(Vec2 v, float snap_deg) +{ + float len = vec2_length(v); + if (len == 0.0f) + return v; + + const float quarter = M3D_PI / 2.0f; /* 90 deg between axes */ + const float octant = M3D_PI / 4.0f; /* 45 deg: axis to diagonal */ + float snap = snap_deg * (M3D_PI / 180.0f); + float angle = atan2f(v.y, v.x); + float axis = roundf(angle / quarter) * quarter; /* nearest axis direction */ + float off = angle - axis; /* -45..45 deg from it */ + + if (fabsf(off) <= snap) { + /* Exactly on the axis, so the other component is a true 0. */ + if (fabsf(v.x) >= fabsf(v.y)) + return vec2_make(v.x > 0.0f ? len : -len, 0.0f); + return vec2_make(0.0f, v.y > 0.0f ? len : -len); + } + + /* Stretch (snap..45] back over (0..45], so the angle never jumps. */ + float stretched = (fabsf(off) - snap) * octant / (octant - snap); + float new_angle = axis + (off > 0.0f ? stretched : -stretched); + return vec2_make(cosf(new_angle) * len, sinf(new_angle) * len); +} + /* Returns the first connected gamepad, or NO_GAMEPAD. Called every poll, * which is also what makes hot-plugging work. */ static int find_gamepad(void) @@ -182,6 +223,7 @@ static Buttons read_gamepad(int jid, Input *out) out->look = apply_radial_deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X], -state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y], LOOK_DEADZONE); + out->look = snap_to_axes(out->look, LOOK_AXIS_SNAP_DEG); buttons.jump = state.buttons[GLFW_GAMEPAD_BUTTON_A] == GLFW_PRESS; buttons.reset = state.buttons[GLFW_GAMEPAD_BUTTON_BACK] == GLFW_PRESS; |
