summaryrefslogtreecommitdiff
path: root/platformer
diff options
context:
space:
mode:
Diffstat (limited to 'platformer')
-rw-r--r--platformer/src/config.h8
-rw-r--r--platformer/src/controls.c42
2 files changed, 48 insertions, 2 deletions
diff --git a/platformer/src/config.h b/platformer/src/config.h
index 8739dbf..c71b330 100644
--- a/platformer/src/config.h
+++ b/platformer/src/config.h
@@ -30,6 +30,9 @@
* Input
* MOVE_DEADZONE 0.20f (radial, left stick)
* LOOK_DEADZONE 0.20f (radial, right stick)
+ * LOOK_AXIS_SNAP_DEG 15.0f (right stick: pushes this close
+ * to straight up/down/left/right
+ * become exactly that direction)
*
* Player: ground movement
* PLAYER_MAX_SPEED 9.0f m/s at full stick tilt
@@ -58,7 +61,7 @@
* CAMERA_PITCH_MIN 0.05f rad
* CAMERA_PITCH_MAX 1.20f rad
* CAMERA_YAW_SPEED 2.5f rad/s at full right-stick tilt
- * CAMERA_PITCH_SPEED 1.5f rad/s at full right-stick tilt
+ * CAMERA_PITCH_SPEED 1.125f rad/s at full right-stick tilt
* CAMERA_FOLLOW_RATE 8.0f 1/s; how fast the camera's DISTANCE catches up
* when the player runs toward/away from it
* CAMERA_ZONE_INSET 0.05f fraction of the window between the resting
@@ -124,6 +127,7 @@
/* ---- Input ------------------------------------------------------------- */
#define MOVE_DEADZONE 0.20f /* left stick radius ignored; higher = less drift, less fine control */
#define LOOK_DEADZONE 0.20f /* right stick radius ignored; higher = camera harder to nudge */
+#define LOOK_AXIS_SNAP_DEG 15.0f /* right stick pushes this close to an axis become pure up/down/left/right; 0 = off, keep < 45 */
/* ---- Player: ground movement ------------------------------------------- */
#define PLAYER_MAX_SPEED 9.0f /* m/s at full stick tilt; higher = faster top run speed */
@@ -151,7 +155,7 @@
#define CAMERA_PITCH_MIN 0.05f /* rad; lowest the camera can swing (just above the ground) */
#define CAMERA_PITCH_MAX 1.20f /* rad; highest the camera can swing (near top-down) */
#define CAMERA_YAW_SPEED 2.5f /* rad/s orbit at full right-stick tilt; higher = faster spin */
-#define CAMERA_PITCH_SPEED 1.5f /* rad/s tilt at full right-stick tilt */
+#define CAMERA_PITCH_SPEED 1.125f /* rad/s tilt at full right-stick tilt; lower = gentler up/down look */
#define CAMERA_FOLLOW_RATE 8.0f /* 1/s; how fast the distance catches up when running toward/away; lower = more lag */
#define CAMERA_ZONE_INSET 0.05f /* window fraction between resting zone and running bound; higher = tighter zone */
#define CAMERA_ZONE_TOP 0.50f /* resting zone's top edge (0 = bottom, 1 = top); lower = more view ahead of the player */
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;