diff options
| author | Schark <jordan@schark.online> | 2026-09-23 23:09:55 -0400 |
|---|---|---|
| committer | Schark <jordan@schark.online> | 2026-09-23 23:09:55 -0400 |
| commit | dab65412376d1b73381446f969c1461c24169811 (patch) | |
| tree | 27adfec438a9bd9f3c96a740ab54016df5f68b80 /platformer/src/physics.c | |
| parent | af03f388cbcff795158ea8345b5c876470229e8b (diff) | |
| download | gamedev-dab65412376d1b73381446f969c1461c24169811.tar.gz gamedev-dab65412376d1b73381446f969c1461c24169811.zip | |
Base prompts
Diffstat (limited to '')
| -rw-r--r-- | platformer/src/physics.c | 40 |
1 files changed, 40 insertions, 0 deletions
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. + * ============================================================ + */ |
