diff options
Diffstat (limited to 'platformer/src/physics.c')
| -rw-r--r-- | platformer/src/physics.c | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/platformer/src/physics.c b/platformer/src/physics.c index 4e842f6..07bc38a 100644 --- a/platformer/src/physics.c +++ b/platformer/src/physics.c @@ -29,6 +29,9 @@ * 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). @@ -40,7 +43,13 @@ * 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). Otherwise ground_box = -1. Using prev_y means a fast fall can never skip + * 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 @@ -52,7 +61,7 @@ * than the constant so the function stays general. * * NOT IN SCOPE: Friction/acceleration (that's player movement *intent*, in - * player.c), slopes, step-up onto small ledges, sub-stepping. + * 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 @@ -117,12 +126,33 @@ static void step_out_of_boxes(Body *b) /* 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 */ @@ -147,7 +177,8 @@ static void resolve_axis(Body *b, float *pos_axis, float *vel_axis, bool axis_is /* Step 4: vertical movement with floor and ceiling. */ static void move_vertically(Body *b, float dt) { - float prev_y = b->pos.y; + 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 @@ -160,7 +191,13 @@ static void move_vertically(Body *b, float dt) * 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); - if (b->pos.y <= floor_y) { + /* 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; @@ -186,6 +223,7 @@ 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); |
