From dab65412376d1b73381446f969c1461c24169811 Mon Sep 17 00:00:00 2001 From: Schark Date: Wed, 23 Sep 2026 23:09:55 -0400 Subject: Base prompts --- platformer/src/physics.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 platformer/src/physics.c (limited to 'platformer/src/physics.c') diff --git a/platformer/src/physics.c b/platformer/src/physics.c new file mode 100644 index 0000000..3608c48 --- /dev/null +++ b/platformer/src/physics.c @@ -0,0 +1,40 @@ +/* + * ============================================================ + * FILE: physics.c + * + * PURPOSE: Moves a Body forward one fixed tick: gravity, integration, + * landing on the ground, and staying inside the world. + * + * FITS IN: Implements physics.h. Called by player.c inside player_update(), + * AFTER the player has chosen its velocity for this tick. Uses + * world_ground_height() and world_clamp_to_bounds() from world.h. + * + * IMPLEMENT physics_step(b, dt), in this order: + * 1. Gravity: if !b->on_ground, vel.y -= GRAVITY * dt, then clamp vel.y to + * no lower than -TERMINAL_VELOCITY. + * (When on the ground, vel.y is left alone. A jump sets it positive + * before this call, and that's what lifts the body off the ground.) + * 2. Integrate (semi-implicit Euler: velocity was updated first, then position): + * pos += vel * dt + * Add a comment explaining why this order is stable enough for a + * fixed timestep. + * 3. Ground: ground = world_ground_height(pos.x, pos.z). + * if pos.y <= ground: pos.y = ground; if vel.y < 0, vel.y = 0; + * on_ground = true + * else: on_ground = false + * Note: on_ground must become false the moment a jump lifts the body, + * and walking off a ledge will also set it false once terrain exists. + * 4. Bounds: pos = world_clamp_to_bounds(pos). If x or z got clamped, + * zero that velocity component so the body doesn't "stick" against + * the edge while pushing into it. + * + * CONVENTIONS: dt is always TICK_DT in practice, but use the parameter rather + * than the constant so the function stays general. + * + * NOT IN SCOPE: Friction/acceleration (that's player movement *intent*, in + * player.c), wall/ceiling collision, slopes, sub-stepping. + * + * DONE WHEN: A body placed at y = 10 falls, speeds up, lands exactly on + * y = 0 with on_ground = true, and stays there. + * ============================================================ + */ -- cgit v1.2.3-18-g5258