summaryrefslogtreecommitdiff
path: root/platformer/src/player.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-29 18:26:32 -0400
committerSchark <jordan@schark.online>2026-09-29 18:26:32 -0400
commit25992eb0baa1be5b49e8732e4b63734cdafe3b80 (patch)
tree20afa57514ba4ca2cb41fa1c6f8ee46bbd64d50a /platformer/src/player.c
parent87486b2f6c11791d1e192bba8a583a994f273201 (diff)
downloadgamedev-25992eb0baa1be5b49e8732e4b63734cdafe3b80.tar.gz
gamedev-25992eb0baa1be5b49e8732e4b63734cdafe3b80.zip
Small inertia and movement tweaks
Diffstat (limited to '')
-rw-r--r--platformer/src/player.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/platformer/src/player.c b/platformer/src/player.c
index 16924ec..cbb2c1c 100644
--- a/platformer/src/player.c
+++ b/platformer/src/player.c
@@ -28,18 +28,26 @@
* wish_yaw = atan2f(wish.x, wish.z) (only meaningful if mag > 0)
*
* B. Ground movement (IDLE / RUN):
+ * - If mag > 0, forward_speed == 0 and wish_yaw is more than
+ * PLAYER_SNAP_TURN_DEG away from facing_yaw: facing_yaw = wish_yaw
+ * at once (standing still, a big turn happens on a dime). Smaller
+ * turns still use the quick turn rate below, so small stick
+ * adjustments stay smooth instead of twitching.
* - If mag > 0: facing_yaw = approach_angle(facing_yaw, wish_yaw,
* PLAYER_TURN_RATE * dt); forward_speed = approachf(forward_speed,
* PLAYER_MAX_SPEED * mag, PLAYER_GROUND_ACCEL * dt)
* - Else: forward_speed = approachf(forward_speed, 0,
- * PLAYER_GROUND_FRICTION * dt)
+ * PLAYER_GROUND_FRICTION * dt). Accel and friction are low on
+ * purpose: starts build up and stops skid briefly, so the player
+ * feels like a heavy, real object rather than a cursor.
* - State: forward_speed > 0 -> RUN, otherwise IDLE.
*
* C. Jump (from IDLE / RUN): if in->jump_pressed and body.on_ground:
* body.vel.y = PLAYER_JUMP_VELOCITY; on_ground = false; state = JUMP.
*
- * D. Air movement (JUMP / FALL): the same as B, but turn rate and accel are
- * multiplied by PLAYER_AIR_CONTROL, and there's NO friction when the
+ * D. Air movement (JUMP / FALL): the same as B, but the turn rate is
+ * multiplied by PLAYER_AIR_CONTROL, accel is PLAYER_AIR_ACCEL, there's
+ * no instant turn from a standstill, and there's NO friction when the
* stick is released (you keep your momentum, like SM64).
* - Variable jump height: in JUMP, if !in->jump_held and vel.y > 0,
* multiply vel.y by PLAYER_JUMP_CUT ONCE (don't repeat it every tick;
@@ -66,8 +74,9 @@
* README roadmap as the place to grow.
*
* DONE WHEN: Small stick tilts walk slowly and full tilt runs at
- * PLAYER_MAX_SPEED. Turning is quick but not instant, and letting go
- * of the stick brings the player to a short, smooth stop. A tap of A
+ * PLAYER_MAX_SPEED after a noticeable build-up. Turning is quick but
+ * not instant while moving, and instant for big turns from a standstill. Letting go
+ * of the stick skids the player to a short, smooth stop. A tap of A
* gives a short hop and holding A gives a full ~2.4 m jump. The
* player lands cleanly back in IDLE/RUN.
* ============================================================
@@ -125,10 +134,17 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt)
return;
}
- if (wish.mag > 0.0f)
+ if (wish.mag > 0.0f) {
+ /* Standing still there's no momentum to swing around, so a big turn
+ * faces the stick right away. Small ones keep the quick turn rate so
+ * nudging the stick to adjust your aim still looks smooth. */
+ float turn = fabsf(wrap_angle(wish.yaw - p->facing_yaw));
+ if (p->forward_speed == 0.0f && turn > PLAYER_SNAP_TURN_DEG * (M3D_PI / 180.0f))
+ p->facing_yaw = wish.yaw;
steer(p, wish, PLAYER_TURN_RATE, PLAYER_GROUND_ACCEL, dt);
- else
+ } else {
p->forward_speed = approachf(p->forward_speed, 0.0f, PLAYER_GROUND_FRICTION * dt);
+ }
p->state = grounded_state(p);
if (in->jump_pressed) {
@@ -144,8 +160,7 @@ static void air_steer(Player *p, Wish wish, float dt)
{
if (wish.mag > 0.0f)
steer(p, wish,
- PLAYER_TURN_RATE * PLAYER_AIR_CONTROL,
- PLAYER_GROUND_ACCEL * PLAYER_AIR_CONTROL, dt);
+ PLAYER_TURN_RATE * PLAYER_AIR_CONTROL, PLAYER_AIR_ACCEL, dt);
}
/* FALL: in the air, going down. Landing hands straight over to the ground