From 923b5928eeafd9bf969b95c4d49699d280b1ce0f Mon Sep 17 00:00:00 2001 From: Schark Date: Tue, 29 Sep 2026 18:46:53 -0400 Subject: Double jump foundation --- platformer/src/config.h | 12 +++++ platformer/src/main.c | 12 +++-- platformer/src/player.c | 136 +++++++++++++++++++++++++++++++++++++++++------- platformer/src/player.h | 18 ++++++- 4 files changed, 153 insertions(+), 25 deletions(-) (limited to 'platformer/src') diff --git a/platformer/src/config.h b/platformer/src/config.h index 8816e98..c767709 100644 --- a/platformer/src/config.h +++ b/platformer/src/config.h @@ -49,6 +49,14 @@ * PLAYER_AIR_ACCEL 9.0f m/s^2 speeding up toward target speed in air * PLAYER_JUMP_VELOCITY 14.0f m/s upward at jump start (~2.4 m apex) * PLAYER_JUMP_CUT 0.50f vel.y multiplier when A released while rising + * PLAYER_SECOND_JUMP_VELOCITY 18.41f m/s upward at second-jump start (1.75x + * the first jump's height, ~4.1 m apex) + * PLAYER_SECOND_JUMP_WINDOW 0.3f s after landing a jump that the second + * jump is available (heightened_movement) + * PLAYER_SECOND_JUMP_MAX_SPEED 5.0f m/s top horizontal speed during the second + * jump (it trades speed for height) + * PLAYER_SECOND_JUMP_SLOWDOWN 3.5f m/s^2 how fast speed above that top speed + * bleeds off in the air (gradual on purpose) * * Physics * GRAVITY 40.0f m/s^2 (stronger than real 9.8 on purpose: @@ -146,6 +154,10 @@ #define PLAYER_AIR_ACCEL 9.0f /* m/s^2 toward target speed in air; higher = more steerable jumps */ #define PLAYER_JUMP_VELOCITY 14.0f /* m/s upward at jump start; higher = taller jumps (~2.4 m apex at 14) */ #define PLAYER_JUMP_CUT 0.50f /* vel.y multiplier on early A release; lower = shorter taps */ +#define PLAYER_SECOND_JUMP_VELOCITY 18.41f /* m/s upward for the second jump; tuned to 1.75x the first jump's height (4.08 m at 60 Hz) */ +#define PLAYER_SECOND_JUMP_WINDOW 0.3f /* s after landing a jump that A gives the second jump; higher = more forgiving timing */ +#define PLAYER_SECOND_JUMP_MAX_SPEED 5.0f /* m/s top horizontal speed until the second jump lands; lower = more vertical, less distance */ +#define PLAYER_SECOND_JUMP_SLOWDOWN 3.5f /* m/s^2 bleed from run speed down to that top speed; lower = more gradual, higher = more abrupt */ /* ---- Physics ----------------------------------------------------------- */ #define GRAVITY 40.0f /* m/s^2; stronger than real 9.8 on purpose, real gravity feels floaty */ diff --git a/platformer/src/main.c b/platformer/src/main.c index 0435737..61972cc 100644 --- a/platformer/src/main.c +++ b/platformer/src/main.c @@ -61,9 +61,10 @@ * * - Debug readout for tuning (a few times per second, not every frame): * window_set_title() with something like - * "platformer | RUN | speed 7.42 m/s | y 1.83 | pad" - * using player_state_name(), forward_speed, body.pos.y and - * input.using_gamepad. This is our "HUD" until text rendering exists. + * "platformer | RUN | speed 7.42 m/s | y 1.83 | pad | heightened" + * using player_state_name(), forward_speed, body.pos.y, + * input.using_gamepad, and "| heightened" only while + * player.heightened_movement is on. This is our "HUD" until text rendering exists. * * - Shutdown in reverse order: render_shutdown() -> window_shutdown(). * @@ -98,12 +99,13 @@ static void update_debug_title(const Player *player, const Input *input) { char title[128]; - snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s", + snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s%s", WINDOW_TITLE, player_state_name(player->state), player->forward_speed, player->body.pos.y, - input->using_gamepad ? "pad" : "keyboard"); + input->using_gamepad ? "pad" : "keyboard", + player->heightened_movement ? " | heightened" : ""); window_set_title(title); } diff --git a/platformer/src/player.c b/platformer/src/player.c index cbb2c1c..88df0c4 100644 --- a/platformer/src/player.c +++ b/platformer/src/player.c @@ -14,7 +14,8 @@ * IMPLEMENT: * - player_init / player_reset: body.pos = spawn, vel = 0, on_ground = true, * body.half_width = PLAYER_HALF_WIDTH, body.height = PLAYER_HEIGHT, - * facing_yaw = 0, forward_speed = 0, state = PLAYER_IDLE. + * facing_yaw = 0, forward_speed = 0, state = PLAYER_IDLE, + * heightened_movement = false, heightened_timer = 0, jump_number = 0. * * - player_update(p, in, camera_yaw, dt). Use one static helper per state * (update_idle / update_run / update_jump / update_fall), dispatched by a @@ -43,18 +44,45 @@ * - 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. + * on_ground = false; state = JUMP, and then either + * - heightened_movement on: the SECOND jump, SM64's double jump. + * body.vel.y = PLAYER_SECOND_JUMP_VELOCITY (1.75x the height); + * jump_number = 2. heightened_movement stays on for the whole jump, + * however long it lasts. Until it lands, the top speed is + * PLAYER_SECOND_JUMP_MAX_SPEED instead of PLAYER_MAX_SPEED (see D). + * - otherwise a normal jump: body.vel.y = PLAYER_JUMP_VELOCITY; + * jump_number = 1. + * + * C2. heightened_movement: landing from a normal jump (jump_number == 1, + * short hop or full) turns it on with heightened_timer = + * PLAYER_SECOND_JUMP_WINDOW. Landing any other way (from the second + * jump, or after walking off an edge) turns it off. Every tick, + * unless the player is in the second jump, the timer counts down and + * it turns off at 0. WHY a named state rather than a private "can + * double jump" flag: later moves will key off it too. * * 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, + * - Top speed: steering aims for top_speed() * mag, where top_speed() + * is PLAYER_SECOND_JUMP_MAX_SPEED while jump_number == 2 and + * PLAYER_MAX_SPEED otherwise. While forward_speed is ABOVE it (a + * full run into the second jump), the stick only turns: the extra + * speed bleeds off at PLAYER_SECOND_JUMP_SLOWDOWN, whatever the + * stick does. So a 6 m/s run into the second jump eases down to + * 5 m/s over ~0.3 s. WHY a separate, gentle rate: slowing at + * PLAYER_AIR_ACCEL (~0.25 s) felt like hitting a wall of syrup + * right at takeoff. 6 m/s only comes back (through normal ground + * accel) after landing. WHY no new state: + * jump_number == 2 already means "in the second jump". + * - Variable jump height (both jumps): in JUMP, if !in->jump_held and vel.y > 0, * multiply vel.y by PLAYER_JUMP_CUT ONCE (don't repeat it every tick; * either switch to FALL right away or keep a flag). Comment WHY: tapping A * gives a hop, holding it gives a full jump. * - JUMP -> FALL when vel.y <= 0. - * - FALL -> IDLE/RUN when body.on_ground (landing). + * - FALL -> IDLE/RUN when body.on_ground (landing; apply C2 first, so + * an A press on the landing tick already gets the second jump). * - If the player is on the ground state-wise but body.on_ground is false * (walked off a platform edge) -> FALL. * @@ -68,7 +96,7 @@ * accelerations in m/s^2, and every one of them multiplies by dt. * No magic numbers; everything comes from config.h. * - * NOT IN SCOPE: Double/triple jump, long jump, wall kick, ground pound, + * NOT IN SCOPE: Triple jump, long jump, wall kick, ground pound, * skidding/turnaround, slopes, coyote time, jump buffering. * Leave a short comment at the state enum pointing to the * README roadmap as the place to grow. @@ -77,8 +105,10 @@ * 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. + * gives a short hop and holding A gives a full ~2.4 m jump. Pressing + * A within 0.3 s of landing gives a second jump 1.75x as high, with + * heightened_movement on from that landing until the second jump + * lands. The player lands cleanly back in IDLE/RUN. * ============================================================ */ @@ -109,12 +139,25 @@ static Wish compute_wish(const Input *in, float camera_yaw) return wish; } +/* The fastest the player may go right now. The second jump trades + * horizontal speed for its extra height, until it lands. */ +static float top_speed(const Player *p) +{ + return p->jump_number == 2 ? PLAYER_SECOND_JUMP_MAX_SPEED : PLAYER_MAX_SPEED; +} + +/* Turns the facing toward the stick, at most turn_rate. */ +static void turn(Player *p, Wish wish, float turn_rate, float dt) +{ + p->facing_yaw = approach_angle(p->facing_yaw, wish.yaw, turn_rate * dt); +} + /* Turns toward the stick and speeds up toward a speed set by how far it's * pushed. Shared by ground and air movement; the air passes smaller rates. */ static void steer(Player *p, Wish wish, float turn_rate, float accel, float dt) { - p->facing_yaw = approach_angle(p->facing_yaw, wish.yaw, turn_rate * dt); - p->forward_speed = approachf(p->forward_speed, PLAYER_MAX_SPEED * wish.mag, accel * dt); + turn(p, wish, turn_rate, dt); + p->forward_speed = approachf(p->forward_speed, top_speed(p) * wish.mag, accel * dt); } /* Standing still vs walking vs running is only a matter of speed. */ @@ -123,6 +166,47 @@ static PlayerState grounded_state(const Player *p) return p->forward_speed > 0.0f ? PLAYER_RUN : PLAYER_IDLE; } +/* C. Leaving the ground. While heightened_movement is on (just after landing + * a normal jump), A gives the higher second jump instead, like SM64's + * double jump. heightened_movement stays on through the whole second jump. */ +static void start_jump(Player *p) +{ + if (p->heightened_movement) { + p->body.vel.y = PLAYER_SECOND_JUMP_VELOCITY; + p->jump_number = 2; + } else { + p->body.vel.y = PLAYER_JUMP_VELOCITY; + p->jump_number = 1; + } + p->body.on_ground = false; + p->state = PLAYER_JUMP; +} + +/* C2. Touching down. Landing a normal jump (a hop or a full one) opens the + * short window for the second jump; landing any other way closes it, so + * the second jump can't chain into another one. */ +static void land(Player *p) +{ + if (p->jump_number == 1) { + p->heightened_movement = true; + p->heightened_timer = PLAYER_SECOND_JUMP_WINDOW; + } else { + p->heightened_movement = false; + } + p->jump_number = 0; +} + +/* C2. The window after landing runs out on its own. It doesn't during the + * second jump: heightened_movement lasts as long as that jump does. */ +static void count_down_heightened(Player *p, float dt) +{ + if (!p->heightened_movement || p->jump_number == 2) + return; + p->heightened_timer -= dt; + if (p->heightened_timer <= 0.0f) + p->heightened_movement = false; +} + /* B + C. IDLE and RUN: full control, friction when the stick is released, * and the only place a jump can start. */ static void update_ground(Player *p, const Input *in, Wish wish, float dt) @@ -147,27 +231,38 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt) } p->state = grounded_state(p); - if (in->jump_pressed) { - p->body.vel.y = PLAYER_JUMP_VELOCITY; - p->body.on_ground = false; - p->state = PLAYER_JUMP; - } + if (in->jump_pressed) + start_jump(p); } /* D. Air movement: weaker steering, and NO friction when the stick is * released, so you keep your momentum like in SM64. */ static void air_steer(Player *p, Wish wish, float dt) { + float air_turn_rate = PLAYER_TURN_RATE * PLAYER_AIR_CONTROL; + + /* Faster than the top speed allows (a full run into the second jump): + * shed the extra slowly, whatever the stick says, so takeoff doesn't feel + * like hitting a wall. The stick can still turn. */ + if (p->forward_speed > top_speed(p)) { + if (wish.mag > 0.0f) + turn(p, wish, air_turn_rate, dt); + p->forward_speed = approachf(p->forward_speed, top_speed(p), + PLAYER_SECOND_JUMP_SLOWDOWN * dt); + return; + } + if (wish.mag > 0.0f) - steer(p, wish, - PLAYER_TURN_RATE * PLAYER_AIR_CONTROL, PLAYER_AIR_ACCEL, dt); + steer(p, wish, air_turn_rate, PLAYER_AIR_ACCEL, dt); } /* FALL: in the air, going down. Landing hands straight over to the ground - * logic on the same tick, so a jump pressed on the landing tick isn't lost. */ + * logic on the same tick, so a jump pressed on the landing tick isn't lost + * (and already counts as the second jump). */ static void update_fall(Player *p, const Input *in, Wish wish, float dt) { if (p->body.on_ground) { + land(p); p->state = grounded_state(p); update_ground(p, in, wish, dt); return; @@ -188,7 +283,8 @@ static void update_jump(Player *p, const Input *in, Wish wish, float dt) air_steer(p, wish, dt); /* Variable jump height: letting go of A while rising cuts the upward speed. - * Tapping A gives a short hop, holding it gives the full jump. Switching to + * Tapping A gives a short hop, holding it gives the full jump (for the + * second jump too). Switching to * FALL right away makes sure the cut happens only once. */ if (!in->jump_held) { p->body.vel.y *= PLAYER_JUMP_CUT; @@ -206,6 +302,9 @@ void player_init(Player *p, Vec3 spawn) p->facing_yaw = 0.0f; p->forward_speed = 0.0f; p->state = PLAYER_IDLE; + p->heightened_movement = false; + p->heightened_timer = 0.0f; + p->jump_number = 0; } void player_reset(Player *p, Vec3 spawn) @@ -216,6 +315,7 @@ void player_reset(Player *p, Vec3 spawn) void player_update(Player *p, const Input *in, float camera_yaw, float dt) { Wish wish = compute_wish(in, camera_yaw); + count_down_heightened(p, dt); /* IDLE and RUN share one helper: they differ only in speed. */ switch (p->state) { diff --git a/platformer/src/player.h b/platformer/src/player.h index 35e5b6a..5f8d963 100644 --- a/platformer/src/player.h +++ b/platformer/src/player.h @@ -25,6 +25,15 @@ * which matches mat4_rotate_y in math3d.h. * float forward_speed; ground speed along facing_yaw, m/s (>= 0) * PlayerState state; + * bool heightened_movement; on for PLAYER_SECOND_JUMP_WINDOW s after + * landing a normal jump, and for the whole + * second jump. While on, A gives the second + * jump. Later moves will read it too. + * float heightened_timer; s left before heightened_movement ends + * (not counted down during the second jump) + * int jump_number; which jump the player is in the air from: + * 0 = none (walked off an edge, or grounded), + * 1 = a normal jump, 2 = the second jump * } Player; * * void player_init(Player *p, Vec3 spawn); @@ -37,7 +46,8 @@ * * CONVENTIONS: Include guard: PLAYER_H. * - * NOT IN SCOPE: Health, animation, extra moves (see README roadmap). + * NOT IN SCOPE: Health, animation, extra moves beyond the second jump (see the + * README roadmap). * * DONE WHEN: main.c and render.c compile against this header alone. * ============================================================ @@ -50,7 +60,8 @@ #include "physics.h" /* SM64-style "actions". Add states here only when a new move needs one; - * see the README roadmap (double jump, long jump, wall kick, ...). */ + * see the README roadmap (triple jump, long jump, wall kick, ...). The second + * jump didn't need one: it's a JUMP with a higher launch (see jump_number). */ typedef enum PlayerState { PLAYER_IDLE, /* on ground, not moving */ PLAYER_RUN, /* on ground, moving (walk vs run is just speed) */ @@ -63,6 +74,9 @@ typedef struct Player { float facing_yaw; /* radians; facing vector = (sin(yaw), 0, cos(yaw)) */ float forward_speed; /* ground speed along facing_yaw, m/s (>= 0) */ PlayerState state; + bool heightened_movement; /* second jump available (or in progress); later moves read it too */ + float heightened_timer; /* s left before heightened_movement ends; paused during the second jump */ + int jump_number; /* airborne from: 0 = no jump (walked off an edge), 1 = normal, 2 = second */ } Player; void player_init(Player *p, Vec3 spawn); -- cgit v1.2.3-18-g5258