/* * ============================================================ * FILE: physics.c * * 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_floor_below(), world_ceiling_above(), world_find_overlap(), * world_box_moved() and world_clamp_to_bounds() from world.h. * main.c moves the moving boxes (world_update) BEFORE this runs. * * IMPLEMENT physics_step(b, dt), in this order: * 0. hit_wall = false; steps 2-3 set it again if a side is touched. * Ride: if on_ground on a box (ground_box >= 0), move by * world_box_moved(ground_box) first, so a moving platform carries whatever * stands on it, up, down and sideways. * Step out: a moving box can move INTO a body that isn't moving. If the * body overlaps a box now, push it out the shortest way (up onto it, down * under it, or out a side), a few passes. Still boxes never cause this, * because the steps below never leave a body inside one. * 1. Gravity ALWAYS pulls: vel.y -= GRAVITY * gravity_scale * dt (the scale * is 1 except while the owner drives vertical motion), 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. * Step up first: if the body was on the ground and the box's top is less * than STEP_HEIGHT above the feet, and there's room there, lift the body * onto it instead of pushing it back (walking up a stair; no hit_wall). * Each push records hit_wall = true, the side's outward normal (moving * +x into a box means its -x side, normal (-1, 0, 0)) and the box, so the * owner can react to walls (the player's wall hang). * 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, &box), * 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, ground_box = box). Step down: if the body WAS on * the ground, isn't moving up, and the floor is less than STEP_HEIGHT * below where the feet were, snap down onto it the same way, so * walking down a stair never leaves the ground (no fall, no landing). * Both stair moves add to step_dy (reset each step), which the owner * uses to smooth the visual pop; the collision itself stays exact. * Otherwise ground_box = -1. 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), 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. 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. Pushing * into a box's side sets hit_wall with that side's normal and box. * ============================================================ */ #include "config.h" #include "physics.h" #include "world.h" #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) */ /* Step 0: stand on a moving box and it carries you. */ static void ride_ground_box(Body *b) { if (b->on_ground && b->ground_box >= 0) b->pos = vec3_add(b->pos, world_box_moved(b->ground_box)); } /* Step 0: a moving box moved into the body. Push the body out along * whichever of the six directions is shortest: usually up onto a platform * that rose into the feet, or out of the way of one sliding sideways. */ static void step_out_of_boxes(Body *b) { 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 up = hit.max.y - b->pos.y; float down = b->pos.y + b->height - hit.min.y; float east = hit.max.x - (b->pos.x - b->half_width); float west = b->pos.x + b->half_width - hit.min.x; float north = hit.max.z - (b->pos.z - b->half_width); float south = b->pos.z + b->half_width - hit.min.z; float least = fminf(fminf(up, down), fminf(fminf(east, west), fminf(north, south))); if (least == up) { b->pos.y = hit.max.y; if (b->vel.y < 0.0f) b->vel.y = 0.0f; } else if (least == down) { b->pos.y = hit.min.y - b->height - PUSH_SKIN; if (b->vel.y > 0.0f) b->vel.y = 0.0f; } else if (least == east) { b->pos.x += east + PUSH_SKIN; } else if (least == west) { b->pos.x -= west + PUSH_SKIN; } else if (least == north) { b->pos.z += north + PUSH_SKIN; } else { b->pos.z -= south + PUSH_SKIN; } } } /* 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. */ /* Step 2-3: walking into a box whose top is less than STEP_HEIGHT above the * feet (a stair) lifts the body onto it, if there's room up there, instead * of stopping it. Only on the ground: jumps and falls collide as usual. */ static bool step_up_onto(Body *b, Box hit) { float rise = hit.max.y - b->pos.y; if (!b->on_ground || rise <= 0.0f || rise >= STEP_HEIGHT) return false; Vec3 raised = vec3_make(b->pos.x, hit.max.y, b->pos.z); Box above; if (world_find_overlap(raised, b->half_width, b->height, &above)) return false; /* no room up there: it's a wall after all */ b->step_dy += hit.max.y - b->pos.y; /* reported so the owner can smooth the pop */ b->pos.y = hit.max.y; if (b->vel.y < 0.0f) b->vel.y = 0.0f; return true; } static void resolve_axis(Body *b, float *pos_axis, float *vel_axis, bool axis_is_x) { for (int pass = 0; pass < MAX_PUSH_PASSES; pass++) { Box hit; if (!world_find_overlap(b->pos, b->half_width, b->height, &hit)) return; if (step_up_onto(b, hit)) continue; /* stepped up a stair: check again from the new height */ float box_min = axis_is_x ? hit.min.x : hit.min.z; float box_max = axis_is_x ? hit.max.x : hit.max.z; float side; /* which way the touched side faces along this axis */ if (*vel_axis > 0.0f) { *pos_axis = box_min - b->half_width - PUSH_SKIN; /* was moving +: back off to the min side */ side = -1.0f; } else if (*vel_axis < 0.0f) { *pos_axis = box_max + b->half_width + PUSH_SKIN; /* was moving -: back off to the max side */ side = 1.0f; } else { return; /* not moving on this axis, so this axis didn't cause it */ } *vel_axis = 0.0f; /* Tell the owner which wall it touched and which way that wall faces. */ b->hit_wall = true; b->wall_normal = axis_is_x ? vec3_make(side, 0.0f, 0.0f) : vec3_make(0.0f, 0.0f, side); b->wall = hit; } } /* Step 4: vertical movement with floor and ceiling. */ static void move_vertically(Body *b, float dt) { float prev_y = b->pos.y; bool was_grounded = b->on_ground; /* 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; /* 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. */ int floor_box; float floor_y = world_floor_below(vec3_make(b->pos.x, prev_y, b->pos.z), b->half_width, &floor_box); /* Land on it, or, walking off a stair (the floor just under STEP_HEIGHT * below and we weren't going up), step down onto it without leaving the * ground. */ bool step_down = was_grounded && b->vel.y <= 0.0f && prev_y - floor_y < STEP_HEIGHT; if (b->pos.y <= floor_y || step_down) { if (b->pos.y > floor_y) b->step_dy += floor_y - prev_y; /* a stair down, not a landing: report it */ b->pos.y = floor_y; if (b->vel.y < 0.0f) b->vel.y = 0.0f; b->on_ground = true; b->ground_box = floor_box; /* remembered so a moving box can carry us next tick */ return; } b->on_ground = false; b->ground_box = -1; /* 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) { /* 0. Walls are reported fresh every step. Ride whatever we stand on, and * step out of anything that moved into us. */ b->hit_wall = false; b->step_dy = 0.0f; ride_ground_box(b); step_out_of_boxes(b); /* 1. Gravity always pulls; the floor pushes back in step 4. */ b->vel.y -= GRAVITY * b->gravity_scale * 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); /* 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; if (clamped.z != b->pos.z) b->vel.z = 0.0f; b->pos = clamped; }