diff options
Diffstat (limited to 'platformer/src/physics.c')
| -rw-r--r-- | platformer/src/physics.c | 143 |
1 files changed, 99 insertions, 44 deletions
diff --git a/platformer/src/physics.c b/platformer/src/physics.c index b04a78e..423550d 100644 --- a/platformer/src/physics.c +++ b/platformer/src/physics.c @@ -2,40 +2,50 @@ * ============================================================ * FILE: physics.c * - * PURPOSE: Moves a Body forward one fixed tick: gravity, integration, - * landing on the ground, and staying inside the world. + * PURPOSE: Moves a Body forward one fixed tick: gravity, then movement one + * axis at a time with collision against the level's boxes, 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. + * world_floor_below(), world_ceiling_above(), world_find_overlap() + * 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. + * 1. Gravity ALWAYS pulls: vel.y -= GRAVITY * dt, clamped to no lower than + * -TERMINAL_VELOCITY. Standing still, the floor check in step 4 pushes + * right back. WHY always: that's what makes walking off a ledge fall on its + * own, with no special "is there still floor under me?" case. + * 2. Move X: pos.x += vel.x * dt. If the body now overlaps a box + * (world_find_overlap), push it back out on the side it came from and + * zero vel.x. Repeat a few times in case the push lands in another box. + * 3. Move Z: the same, for z. Resolving one axis at a time is what makes + * running diagonally into a wall SLIDE along it instead of stopping dead. + * 4. Move Y: remember prev_y, then pos.y += vel.y * dt (semi-implicit Euler: + * velocity was updated first; add a comment on why that's stable for a + * fixed timestep). + * Floor: floor = world_floor_below((x, prev_y, z), half_width), the + * highest surface the feet were above at the START of the move. If + * pos.y <= floor: land (pos.y = floor, vel.y = 0 if falling, + * on_ground = true). Using prev_y means a fast fall can never skip + * through a thin platform between two ticks. + * Otherwise on_ground = false, and check the ceiling the same way from + * the previous head height: if the head crossed a box bottom, stop at + * it and zero upward speed (bonk). + * 5. Bounds: pos = world_clamp_to_bounds(pos). If x or z got clamped, + * zero that velocity component so the body doesn't "stick" to the edge. * * 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. + * player.c), slopes, step-up onto small ledges, 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. + * y = 0 with on_ground = true, and stays there. It lands on + * platform tops (even at terminal velocity), can't walk through a + * platform's sides, slides along them when running diagonally, + * falls when it walks off an edge, and bonks on undersides. * ============================================================ */ @@ -43,37 +53,82 @@ #include "physics.h" #include "world.h" -void physics_step(Body *b, float dt) +#define PUSH_SKIN 1e-3f /* m, extra gap left after a push-out so edges only touch */ +#define MAX_PUSH_PASSES 4 /* push-outs per axis per tick (boxes can overlap) */ + +/* Pushes the body out of any box it overlaps along ONE horizontal axis, + * back toward where it came from. pos_axis/vel_axis point at the x or z + * members of the body, so the same code handles both axes. */ +static void resolve_axis(Body *b, float *pos_axis, float *vel_axis, bool axis_is_x) { - /* 1. Gravity. 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. */ - if (!b->on_ground) { - b->vel.y -= GRAVITY * dt; - if (b->vel.y < -TERMINAL_VELOCITY) - b->vel.y = -TERMINAL_VELOCITY; + for (int pass = 0; pass < MAX_PUSH_PASSES; pass++) { + Box hit; + if (!world_find_overlap(b->pos, b->half_width, b->height, &hit)) + return; + float box_min = axis_is_x ? hit.min.x : hit.min.z; + float box_max = axis_is_x ? hit.max.x : hit.max.z; + if (*vel_axis > 0.0f) + *pos_axis = box_min - b->half_width - PUSH_SKIN; /* was moving +: back off to the min side */ + else if (*vel_axis < 0.0f) + *pos_axis = box_max + b->half_width + PUSH_SKIN; /* was moving -: back off to the max side */ + else + return; /* not moving on this axis, so this axis didn't cause it */ + *vel_axis = 0.0f; } +} - /* 2. Integrate with semi-implicit Euler: velocity was updated above, and - * position now moves with that NEW velocity. Unlike plain Euler (which - * uses the old velocity) this doesn't gain energy over time, and with a - * fixed dt the result is identical every run, so a jump always - * reaches the same height. */ - b->pos = vec3_add(b->pos, vec3_scale(b->vel, dt)); +/* Step 4: vertical movement with floor and ceiling. */ +static void move_vertically(Body *b, float dt) +{ + float prev_y = b->pos.y; + /* Semi-implicit Euler: vel.y was already updated by gravity, and the + * position moves with that NEW velocity. Unlike plain Euler (which uses + * the old velocity) it doesn't gain energy over time, and with a fixed dt + * the result is identical every run, so a jump always reaches the same + * height. */ + b->pos.y += b->vel.y * dt; - /* 3. Ground. Landing snaps the feet to the surface and kills the downward - * speed. on_ground goes false the moment a jump lifts the body (and, - * once terrain exists, when walking off a ledge). */ - float ground = world_ground_height(b->pos.x, b->pos.z); - if (b->pos.y <= ground) { - b->pos.y = ground; + /* The highest surface the feet were above BEFORE moving. Any top between + * the old and new height is at or below prev_y, so the fall lands on the + * first one it crossed, no matter how fast it was going. */ + float floor_y = world_floor_below(vec3_make(b->pos.x, prev_y, b->pos.z), b->half_width); + if (b->pos.y <= floor_y) { + b->pos.y = floor_y; if (b->vel.y < 0.0f) b->vel.y = 0.0f; b->on_ground = true; - } else { - b->on_ground = false; + return; + } + b->on_ground = false; + + /* Same idea upward: stop the head at the first underside it crossed. */ + float ceiling_y = world_ceiling_above(vec3_make(b->pos.x, prev_y + b->height, b->pos.z), + b->half_width); + if (b->pos.y + b->height >= ceiling_y) { + b->pos.y = ceiling_y - b->height; + if (b->vel.y > 0.0f) + b->vel.y = 0.0f; } +} + +void physics_step(Body *b, float dt) +{ + /* 1. Gravity always pulls; the floor pushes back in step 4. */ + b->vel.y -= GRAVITY * dt; + if (b->vel.y < -TERMINAL_VELOCITY) + b->vel.y = -TERMINAL_VELOCITY; + + /* 2 + 3. Horizontal movement, one axis at a time, so a diagonal run into + * a wall slides along it instead of stopping dead. */ + b->pos.x += b->vel.x * dt; + resolve_axis(b, &b->pos.x, &b->vel.x, true); + b->pos.z += b->vel.z * dt; + resolve_axis(b, &b->pos.z, &b->vel.z, false); + + /* 4. Vertical movement: land on floors, bonk on ceilings. */ + move_vertically(b, dt); - /* 4. Bounds. Zero the velocity into an edge we got clamped against, so + /* 5. Bounds. Zero the velocity into an edge we got clamped against, so * the body doesn't "stick" to it while pushing into it. */ Vec3 clamped = world_clamp_to_bounds(b->pos); if (clamped.x != b->pos.x) b->vel.x = 0.0f; |
