summaryrefslogtreecommitdiff
path: root/platformer/src/physics.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-29 21:02:22 -0400
committerSchark <jordan@schark.online>2026-09-29 21:02:22 -0400
commit21404c26d3907cefa524878fb031e7a2022fb3d7 (patch)
tree38b2bb26d6d0c05447213edca4911ccc3887f474 /platformer/src/physics.c
parent5f04f3ae87fb307e055d39e81f7c0c841e8a9af8 (diff)
downloadgamedev-21404c26d3907cefa524878fb031e7a2022fb3d7.tar.gz
gamedev-21404c26d3907cefa524878fb031e7a2022fb3d7.zip
Game world test with updated movement
Diffstat (limited to 'platformer/src/physics.c')
-rw-r--r--platformer/src/physics.c106
1 files changed, 91 insertions, 15 deletions
diff --git a/platformer/src/physics.c b/platformer/src/physics.c
index 423550d..4e842f6 100644
--- a/platformer/src/physics.c
+++ b/platformer/src/physics.c
@@ -8,26 +8,39 @@
*
* 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()
- * and world_clamp_to_bounds() from world.h.
+ * 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:
- * 1. Gravity ALWAYS pulls: vel.y -= GRAVITY * dt, clamped to no lower than
+ * 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.
+ * 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), 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
+ * 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
* 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
@@ -45,7 +58,8 @@
* 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.
+ * 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.
* ============================================================
*/
@@ -56,6 +70,50 @@
#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. */
@@ -67,13 +125,22 @@ static void resolve_axis(Body *b, float *pos_axis, float *vel_axis, bool axis_is
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)
+ 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 */
- else if (*vel_axis < 0.0f)
+ 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 */
- else
+ 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;
}
}
@@ -91,15 +158,18 @@ static void move_vertically(Body *b, float 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. */
- float floor_y = world_floor_below(vec3_make(b->pos.x, prev_y, b->pos.z), b->half_width);
+ 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) {
b->pos.y = floor_y;
if (b->vel.y < 0.0f)
b->vel.y = 0.0f;
- b->on_ground = true;
+ 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->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),
@@ -113,8 +183,14 @@ static void move_vertically(Body *b, float dt)
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;
+ ride_ground_box(b);
+ step_out_of_boxes(b);
+
/* 1. Gravity always pulls; the floor pushes back in step 4. */
- b->vel.y -= GRAVITY * dt;
+ b->vel.y -= GRAVITY * b->gravity_scale * dt;
if (b->vel.y < -TERMINAL_VELOCITY)
b->vel.y = -TERMINAL_VELOCITY;