summaryrefslogtreecommitdiff
path: root/platformer/src
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-29 23:36:45 -0400
committerSchark <jordan@schark.online>2026-09-29 23:36:45 -0400
commit8af1210d45c3fa3f41a174d2906ab597dcc22f1c (patch)
tree43f1a9814faae852af943f71de53d4ca38dc6cd2 /platformer/src
parentfdc8c951fc72d9dc4c8faa58a21b2b9de0599698 (diff)
downloadgamedev-8af1210d45c3fa3f41a174d2906ab597dcc22f1c.tar.gz
gamedev-8af1210d45c3fa3f41a174d2906ab597dcc22f1c.zip
Final state of the game day 3HEADmaster
Diffstat (limited to 'platformer/src')
-rw-r--r--platformer/src/camera.c105
-rw-r--r--platformer/src/camera.h13
-rw-r--r--platformer/src/config.h14
-rw-r--r--platformer/src/level.c40
-rw-r--r--platformer/src/level.h4
-rw-r--r--platformer/src/main.c9
-rw-r--r--platformer/src/physics.c46
-rw-r--r--platformer/src/physics.h4
-rw-r--r--platformer/src/player.c44
-rw-r--r--platformer/src/player.h11
-rw-r--r--platformer/src/render.c6
-rw-r--r--platformer/src/world.c18
-rw-r--r--platformer/src/world.h11
13 files changed, 296 insertions, 29 deletions
diff --git a/platformer/src/camera.c b/platformer/src/camera.c
index 7663003..b11f6bb 100644
--- a/platformer/src/camera.c
+++ b/platformer/src/camera.c
@@ -130,7 +130,18 @@
* Framing is in NDC so it matches what you see on screen, and it
* converts to meters via depth * tan(fov/2) (times aspect for x).
*
- * NOT IN SCOPE: Camera collision, snapping directly behind the player on its
+ * - Step 7, objects (keep_eye_clear): the eye never sits INSIDE an object
+ * (world_object_at; zones never count). Objects BETWEEN the player and the
+ * eye are fine: a platform in the shot stays in the shot. Only when the
+ * spot the eye wants is inside an object does the eye jump THROUGH it, the
+ * same tick, to the object's near side, along its line to the player (so
+ * the player stays on the same screen spot and the framing model is
+ * untouched). When the wanted spot is free again, ease the eye back out:
+ * speed up at CAMERA_RETURN_ACCEL, cruise at CAMERA_RETURN_SPEED, brake to
+ * arrive exactly (like the auto-tilt). If that path would take it into an
+ * object, it jumps through to the far side instead of stopping inside.
+ *
+ * NOT IN SCOPE: Snapping directly behind the player on its
* own (only the recenter button does that), shake, vertical
* look-ahead. (Good candidates for later tuning, not for now.)
*
@@ -151,6 +162,7 @@
#include "camera.h"
#include "config.h"
+#include "world.h"
#define THIRD (1.0f / 3.0f) /* the thirds lines, as window fractions */
#define STILL_EPSILON 1e-5f /* m moved per tick below which the player is "still" */
@@ -236,6 +248,90 @@ static void orbit(Camera *c, const Input *in, float dt)
/* Auto-pitch: after CAMERA_AUTO_PITCH_DELAY seconds of the camera not being
* moved by hand, slowly tilt to the preferred downward viewing angle. The
* timer is capped at the delay so it can't grow forever. */
+/* Where the line from `from` along `dir` enters and leaves box b grown by
+ * `radius` (the slab test: along each axis the line is inside the box's
+ * range between two distances; it's inside the box where all three ranges
+ * overlap). */
+static void line_through_box(Vec3 from, Vec3 dir, Box b, float radius, float *enter, float *leave)
+{
+ float lo[3] = { b.min.x - radius, b.min.y - radius, b.min.z - radius };
+ float hi[3] = { b.max.x + radius, b.max.y + radius, b.max.z + radius };
+ float o[3] = { from.x, from.y, from.z };
+ float d[3] = { dir.x, dir.y, dir.z };
+ *enter = -1e9f;
+ *leave = 1e9f;
+ for (int axis = 0; axis < 3; axis++) {
+ if (fabsf(d[axis]) < 1e-6f)
+ continue; /* parallel: the point is inside this slab (it was found inside the box) */
+ float t0 = (lo[axis] - o[axis]) / d[axis];
+ float t1 = (hi[axis] - o[axis]) / d[axis];
+ *enter = fmaxf(*enter, fminf(t0, t1));
+ *leave = fminf(*leave, fmaxf(t0, t1));
+ }
+}
+
+/* Moves a distance along the eye line out of any object it's inside: toward
+ * the player (`toward_player`), to that object's near side, or away from
+ * them, to its far side. A few passes, since objects can touch or overlap. */
+static float step_out_of_objects(Vec3 subject, Vec3 dir, float distance, bool toward_player)
+{
+ for (int pass = 0; pass < 4; pass++) {
+ Box hit;
+ if (!world_object_at(vec3_add(subject, vec3_scale(dir, distance)), CAMERA_COLLISION_RADIUS, &hit))
+ break;
+ float enter, leave;
+ line_through_box(subject, dir, hit, CAMERA_COLLISION_RADIUS, &enter, &leave);
+ distance = toward_player ? fmaxf(enter, 0.0f) : leave;
+ }
+ return distance;
+}
+
+/* The eye never sits inside an object. An object may be between the player
+ * and the eye (a platform in the shot is fine); only when the eye itself
+ * would end up inside one does it jump THROUGH it, the same tick, to the
+ * object's near side (the player's side), along its line to the player, so
+ * the player stays on the same screen spot and the framing is untouched.
+ * When the wanted spot is free again, the eye eases back out (speed up,
+ * cruise, brake, like the auto-tilt) instead of popping; if that path takes
+ * it into an object, it jumps through to the far side rather than stopping
+ * inside. Zones never count. */
+static void keep_eye_clear(Camera *c, float dt)
+{
+ Vec3 to_eye = vec3_sub(c->position, c->subject);
+ float wanted = vec3_length(to_eye);
+ if (wanted < 1e-4f)
+ return;
+ Vec3 dir = vec3_scale(to_eye, 1.0f / wanted);
+ float allowed = step_out_of_objects(c->subject, dir, wanted, true);
+ bool blocked = allowed < wanted - 1e-4f;
+
+ if (allowed <= c->eye_distance || !c->pulled_in) {
+ /* The wanted spot is inside an object nearer than we are (jump in
+ * now), or we're not recovering: simply sit where allowed. */
+ c->eye_distance = allowed;
+ c->return_speed = 0.0f;
+ c->pulled_in = blocked;
+ } else {
+ /* Free further out again: ease back toward it. */
+ float remaining = allowed - c->eye_distance;
+ float brake_limit = sqrtf(2.0f * CAMERA_RETURN_ACCEL * remaining);
+ c->return_speed = fminf(c->return_speed + CAMERA_RETURN_ACCEL * dt,
+ fminf(CAMERA_RETURN_SPEED, brake_limit));
+ float next = c->eye_distance + c->return_speed * dt;
+ next = fminf(step_out_of_objects(c->subject, dir, next, false), allowed);
+ if (next >= allowed) {
+ next = allowed;
+ c->return_speed = 0.0f;
+ c->pulled_in = blocked;
+ }
+ c->eye_distance = next;
+ }
+
+ Vec3 look = vec3_sub(c->target, c->position); /* keep looking the same way */
+ c->position = vec3_add(c->subject, vec3_scale(dir, c->eye_distance));
+ c->target = vec3_add(c->position, look);
+}
+
static void auto_pitch(Camera *c, const Input *in, float dt)
{
bool touched = in->look.x != 0.0f || in->look.y != 0.0f || in->recenter_pressed;
@@ -394,6 +490,10 @@ void camera_init(Camera *c, Vec3 follow_point)
c->auto_pitch_speed = 0.0f;
/* The aspect ratio only matters for a non-zero framing, which is 0 here. */
place_camera(c, 1.0f);
+ c->eye_distance = vec3_length(vec3_sub(c->position, c->subject));
+ c->pulled_in = false;
+ c->return_speed = 0.0f;
+ keep_eye_clear(c, 0.0f);
}
void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_facing,
@@ -460,8 +560,9 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f
if (c->framing.y > max_y)
c->framing.y = max_y;
- /* 6. Derive the eye from the framing. */
+ /* 6. Derive the eye from the framing, then keep it out of objects. */
place_camera(c, aspect);
+ keep_eye_clear(c, dt);
}
float camera_yaw(const Camera *c)
diff --git a/platformer/src/camera.h b/platformer/src/camera.h
index 7dc2cc1..eeafe6c 100644
--- a/platformer/src/camera.h
+++ b/platformer/src/camera.h
@@ -32,6 +32,11 @@
* CAMERA_AUTO_PITCH_DELAY; drives the auto-pitch
* float auto_pitch_speed; current auto-pitch speed, rad/s (>= 0), so the
* tilt can ease in and out instead of moving linearly
+ * float eye_distance; how far the eye really is from the player along
+ * its line of sight (shorter than wanted when an
+ * object is in the way)
+ * bool pulled_in; an object pulled the eye in and it's easing back
+ * float return_speed; m/s of that ease back out
* } Camera;
*
* void camera_init(Camera *c, Vec3 follow_point);
@@ -61,8 +66,9 @@
*
* CONVENTIONS: Include guard: CAMERA_H.
*
- * NOT IN SCOPE: Collision with geometry, automatic "behind the player"
- * rotation, cutscene/fixed cameras, zoom levels.
+ * NOT IN SCOPE: Automatic "behind the player" rotation, cutscene/fixed
+ * cameras, zoom levels. (The eye does stay out of objects: see
+ * camera.c step 7.)
*
* DONE WHEN: player.c and render.c can be written against this header alone.
* ============================================================
@@ -88,6 +94,9 @@ typedef struct Camera {
float recenter_yaw; /* the yaw that glide is headed for */
float hands_off_time; /* s since the camera was last moved by hand (capped) */
float auto_pitch_speed; /* current auto-pitch speed, rad/s (>= 0), for easing */
+ float eye_distance; /* how far the eye actually is from the player along its line of sight */
+ bool pulled_in; /* an object pulled the eye in; it's easing back out */
+ float return_speed; /* current speed of that ease back out, m/s */
} Camera;
void camera_init(Camera *c, Vec3 follow_point);
diff --git a/platformer/src/config.h b/platformer/src/config.h
index 86677ac..58520ab 100644
--- a/platformer/src/config.h
+++ b/platformer/src/config.h
@@ -95,6 +95,11 @@
* GRAVITY 40.0f m/s^2 (stronger than real 9.8 on purpose:
* platformers feel floaty with real gravity)
* TERMINAL_VELOCITY 50.0f m/s max fall speed
+ * STEP_HEIGHT 0.25f m; a grounded body walks up or down any ledge
+ * lower than this (stairs), with no jump or fall
+ * STEP_SMOOTH_RATE 14.0f 1/s; how fast the DRAWN and camera-followed
+ * height catches up after a stair step (physics
+ * steps instantly; this hides the pop)
*
* Player: size (used by render, and later by collision)
* PLAYER_HALF_WIDTH 0.4f m
@@ -130,6 +135,10 @@
* CAMERA_AUTO_PITCH_SPEED 0.054f rad/s; top speed of that tilt
* CAMERA_AUTO_PITCH_ACCEL 0.045f rad/s^2; how gently the tilt speeds up
* and slows down (ease in/out)
+ * CAMERA_COLLISION_RADIUS 0.25f m the eye keeps from objects
+ * CAMERA_RETURN_SPEED 3.0f m/s top speed the eye slides back out once an
+ * object stops blocking it (it snaps IN at once)
+ * CAMERA_RETURN_ACCEL 3.0f m/s^2 ease in/out of that return
* CAMERA_FOV_DEG 60.0f vertical field of view
* CAMERA_NEAR 0.1f
* CAMERA_FAR 500.0f
@@ -216,6 +225,8 @@
/* ---- Physics ----------------------------------------------------------- */
#define GRAVITY 40.0f /* m/s^2; stronger than real 9.8 on purpose, real gravity feels floaty */
+#define STEP_HEIGHT 0.25f /* m; ledges lower than this are walked up/down (stairs): no jump, no fall */
+#define STEP_SMOOTH_RATE 14.0f /* 1/s the drawn/camera height glides onto the body after a stair step; lower = smoother, laggier */
#define TERMINAL_VELOCITY 50.0f /* m/s max fall speed; lower = long falls feel more controlled */
/* ---- Player: size (used by render, and later by collision) ------------- */
@@ -243,6 +254,9 @@
#define CAMERA_AUTO_PITCH_DEG 15.0f /* degrees above horizontal the camera prefers to look down at the player */
#define CAMERA_AUTO_PITCH_SPEED 0.054f /* rad/s top tilt speed toward that angle; lower = slower, subtler tilt */
#define CAMERA_AUTO_PITCH_ACCEL 0.045f /* rad/s^2 ease in/out of the tilt; lower = softer start and stop */
+#define CAMERA_COLLISION_RADIUS 0.25f /* m the eye keeps from objects (never zones) */
+#define CAMERA_RETURN_SPEED 3.0f /* m/s top speed back out after an object stopped blocking; lower = slower, calmer */
+#define CAMERA_RETURN_ACCEL 3.0f /* m/s^2 ease in/out of that return, like the auto-tilt */
#define CAMERA_FOV_DEG 60.0f /* vertical field of view, degrees; higher = wider, faster-feeling view */
#define CAMERA_NEAR 0.1f /* m; near clip plane */
#define CAMERA_FAR 500.0f /* m; far clip plane (must reach past the world edge) */
diff --git a/platformer/src/level.c b/platformer/src/level.c
index 3ae45be..34e03c3 100644
--- a/platformer/src/level.c
+++ b/platformer/src/level.c
@@ -89,6 +89,10 @@
* choice ("how do I get up there?") instead of one correct jump after
* another, and the route makes sure a way up always exists.
*
+ * 4. Two test staircases east of spawn, 0.2 m steps (under STEP_HEIGHT):
+ * stairwell 1 (z -2..2) has 40 steps 0.5 m deep (20 m long, 8 m tall);
+ * stairwell 2 (z 4..8) has 100 steps 0.12 m deep (12 m long, 20 m tall):
+ * the shallowest steps the step-up can take at full speed, for fun.
* - level_build_zones(): TEST_ZONES, hand-placed. One locked-pan zone covers the
* whole cliff and its climb band (south and east faces, out to where the
* climb platforms reach), from the ground up to exactly CLIFF_TOP, so the
@@ -900,6 +904,40 @@ static void add_crossing(void)
}
}
+/* ---- 4. The staircase -------------------------------------------------- */
+
+/* Two test staircases east of spawn, side by side, each 4 m wide. Every step
+ * is lower than STEP_HEIGHT (0.25 m), so the player walks up them, never
+ * jumping. */
+#define STAIR_START_X 32.0f /* m, both staircases' bottom step starts here */
+#define STAIR_WIDTH 4.0f /* m */
+#define STAIR_RISE 0.2f /* m high per step (under STEP_HEIGHT) */
+/* Stairwell 1: 40 steps 0.5 m deep: 20 m long, 8 m tall. */
+#define STAIR1_Z -2.0f /* m, its south edge */
+#define STAIR1_RUN 0.5f
+#define STAIR1_COUNT 40
+/* Stairwell 2 (for fun): 100 steps as shallow as the step-up allows: 12 m
+ * long, 20 m tall. WHY 0.12 m: the step-up/down handles one stair per tick,
+ * and the fastest the player moves in a tick is 7 m/s / 60 = 0.117 m. A
+ * shallower step gets crossed two at a time and blocks the climb (tested:
+ * 0.10 m fails running down at 7 m/s, 0.11 m and up work). */
+#define STAIR2_Z 4.0f
+#define STAIR2_RUN 0.12f
+#define STAIR2_COUNT 100
+
+/* One staircase rising toward +x: `count` steps `run` deep and STAIR_RISE
+ * high, each a box from the ground up to its step. */
+static void add_staircase(float south_z, float run, int count)
+{
+ for (int i = 0; i < count; i++) {
+ float x = STAIR_START_X + run * (float)i;
+ Box step = { { x, 0.0f, south_z },
+ { x + run, STAIR_RISE * (float)(i + 1), south_z + STAIR_WIDTH },
+ false, BOX_STYLE_PLATFORM };
+ add_box(step, no_motion());
+ }
+}
+
/* ---- Zones ------------------------------------------------------------- */
/* Non-solid regions (see world.h). Bounds: min corner, max corner; the Box's
@@ -932,6 +970,8 @@ int level_build(LevelBox *out, int capacity)
add_rock_pillars();
add_scatter();
add_crossing();
+ add_staircase(STAIR1_Z, STAIR1_RUN, STAIR1_COUNT); /* last, so the cliff course's boxes keep their order */
+ add_staircase(STAIR2_Z, STAIR2_RUN, STAIR2_COUNT);
return s_count;
}
diff --git a/platformer/src/level.h b/platformer/src/level.h
index b66414e..6984f19 100644
--- a/platformer/src/level.h
+++ b/platformer/src/level.h
@@ -32,7 +32,7 @@
* typedef struct LevelBox { Box box; BoxMotion motion; } LevelBox;
* box is where the box sits at offset 0.
*
- * #define LEVEL_MAX_BOXES 256
+ * #define LEVEL_MAX_BOXES 384
* #define LEVEL_MAX_ZONES 16
* int level_build(LevelBox *out, int capacity);
* Fills out[] with every box in the level and returns how many.
@@ -77,7 +77,7 @@ typedef struct LevelBox {
BoxMotion motion;
} LevelBox;
-#define LEVEL_MAX_BOXES 256
+#define LEVEL_MAX_BOXES 384
#define LEVEL_MAX_ZONES 16
int level_build(LevelBox *out, int capacity);
diff --git a/platformer/src/main.c b/platformer/src/main.c
index 8e1d139..040ad37 100644
--- a/platformer/src/main.c
+++ b/platformer/src/main.c
@@ -39,8 +39,10 @@
* camera_init(&camera, player.body.pos); // see WHY below
* player_update(&player, &input, camera_yaw(&camera), TICK_DT);
* pan_locked = world_in_zone(player's body center, ZONE_LOCKED_PAN);
- * camera_update(&camera, &input, player.body.pos, player.facing_yaw,
- * pan_locked, window_aspect(), TICK_DT);
+ * camera_update(&camera, &input, player_visual_pos(&player),
+ * player.facing_yaw, pan_locked, window_aspect(), TICK_DT);
+ * (the visual position smooths stair steps, so the camera
+ * doesn't jolt on stairs)
* accumulator -= TICK_DT;
*
* render_frame(&camera, &player, window_aspect(), fps);
@@ -178,7 +180,8 @@ int main(void)
/* In a locked-pan zone the camera doesn't swing behind the player. */
Vec3 body_center = vec3_add(player.body.pos, vec3_make(0.0f, 0.5f * PLAYER_HEIGHT, 0.0f));
pan_locked = world_in_zone(body_center, ZONE_LOCKED_PAN);
- camera_update(&camera, &input, player.body.pos, player.facing_yaw,
+ /* The camera follows the smoothed position, so stairs don't jolt it. */
+ camera_update(&camera, &input, player_visual_pos(&player), player.facing_yaw,
pan_locked, window_aspect(), TICK_DT);
accumulator -= TICK_DT;
}
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);
diff --git a/platformer/src/physics.h b/platformer/src/physics.h
index 9da41ac..9976aca 100644
--- a/platformer/src/physics.h
+++ b/platformer/src/physics.h
@@ -28,6 +28,9 @@
* Vec3 wall_normal; that side's outward normal (+-x or +-z, y = 0);
* only meaningful if hit_wall
* Box wall; the box that side belongs to; only meaningful if hit_wall
+ * float step_dy; how far a stair step moved the body up (+) or down (-)
+ * during the last step (0 if it didn't step), so the
+ * owner can smooth the pop out of what it draws
* int ground_box; index (world_box) of the box the body stands on, or -1
* for the ground / in the air. Used to ride moving boxes.
* } Body;
@@ -64,6 +67,7 @@ typedef struct Body {
Vec3 wall_normal; /* that side's outward normal (+-x or +-z); only if hit_wall */
Box wall; /* the box that side belongs to; only if hit_wall */
int ground_box; /* world_box index stood on (-1 = ground or airborne); for riding moving boxes */
+ float step_dy; /* m a stair step moved the body this step (+ up, - down; 0 if none), for smoothing visuals */
} Body;
void physics_step(Body *b, float dt);
diff --git a/platformer/src/player.c b/platformer/src/player.c
index 9f0e822..429baa8 100644
--- a/platformer/src/player.c
+++ b/platformer/src/player.c
@@ -98,11 +98,12 @@
* jump_kind = JUMP_NORMAL. heightened_movement stays OFF, so a
* first jump can't grab walls (tried; it made wall hangs too easy).
*
- * C2. Landing: landing from a normal jump (JUMP_NORMAL, short hop or
- * full) or a retreat jump (JUMP_RETREAT) turns heightened_movement on
- * with heightened_timer = PLAYER_SECOND_JUMP_WINDOW, so either one
- * can lead into the second jump. Landing any other way (from the
- * second jump, or after walking off an edge) turns it off. Every
+ * C2. Landing: every landing turns heightened_movement on with
+ * heightened_timer = PLAYER_SECOND_JUMP_WINDOW (after a normal jump,
+ * a retreat jump, or a plain fall: walking off a platform, letting go
+ * of a wall), so any of them can lead into the second jump. The one
+ * exception is landing the second jump itself: that turns it off, so
+ * second jumps can't chain. Every
* landing turns retreating_movement off and resets jump_kind to
* JUMP_NONE, and clears has_last_wall. The heightened timer counts
* down (turning it off at 0) only while jump_kind == JUMP_NONE and
@@ -187,6 +188,13 @@
* body.gravity_scale = 0 in WALL_HANG, else 1.
* Then physics_step(&p->body, dt).
*
+ * G. Stairs look smooth: after physics_step, visual_offset -= body.step_dy
+ * (so the drawn height stays put when physics steps a stair), clamped to
+ * +-2 * STEP_HEIGHT, then decays toward 0 by exp(-STEP_SMOOTH_RATE * dt).
+ * player_visual_pos() = body.pos + (0, visual_offset, 0): render.c draws
+ * the player there and main.c points the camera at it. Gameplay, the
+ * shadow and zones all use the real body.pos.
+ *
* - player_state_name: a switch that returns string literals.
*
* CONVENTIONS: Facing vector = (sin(yaw), 0, cos(yaw)). Speeds are in m/s,
@@ -336,13 +344,14 @@ static void start_retreat_jump(Player *p)
leave_ground(p, PLAYER_RETREAT_JUMP_VELOCITY, JUMP_RETREAT);
}
-/* C2. Touching down. Landing a normal jump (a hop or a full one) or a
- * retreat jump opens the short window for the second jump; landing any
- * other way closes it, so the second jump can't chain into another one.
- * Any landing ends a retreat jump's retreating_movement. */
+/* C2. Touching down. Every landing opens the short window for the second
+ * jump (after a normal or retreat jump, and after a plain fall such as
+ * walking off a platform or letting go of a wall), EXCEPT landing the
+ * second jump itself, so second jumps can't chain. Any landing ends a
+ * retreat jump's retreating_movement. */
static void land(Player *p)
{
- if (p->jump_kind == JUMP_NORMAL || p->jump_kind == JUMP_RETREAT) {
+ if (p->jump_kind != JUMP_SECOND) {
p->heightened_movement = true;
p->heightened_timer = PLAYER_SECOND_JUMP_WINDOW;
} else {
@@ -682,6 +691,8 @@ void player_init(Player *p, Vec3 spawn)
p->skid_speed = 0.0f;
p->skid_yaw = 0.0f;
p->jump_buffer = 0.0f;
+ p->visual_offset = 0.0f;
+ p->body.step_dy = 0.0f;
p->wall_time = 0.0f;
p->wall_jump_yaw = 0.0f;
p->has_last_wall = false;
@@ -735,6 +746,19 @@ void player_update(Player *p, const Input *in, float camera_yaw, float dt)
p->body.vel.z = cosf(move_yaw) * move_speed;
p->body.gravity_scale = hanging ? 0.0f : 1.0f;
physics_step(&p->body, dt);
+
+ /* G. Stairs: physics lifts or drops the body a whole stair in one tick,
+ * which would pop on screen (and jolt the camera). Keep the DRAWN height
+ * where it was and let it glide onto the body, so stairs look smooth
+ * while collision and timing stay exact. Real jumps and landings aren't
+ * steps, so they're never smoothed. */
+ p->visual_offset = clampf(p->visual_offset - p->body.step_dy, -2.0f * STEP_HEIGHT, 2.0f * STEP_HEIGHT);
+ p->visual_offset *= expf(-STEP_SMOOTH_RATE * dt);
+}
+
+Vec3 player_visual_pos(const Player *p)
+{
+ return vec3_add(p->body.pos, vec3_make(0.0f, p->visual_offset, 0.0f));
}
const char *player_state_name(PlayerState s)
diff --git a/platformer/src/player.h b/platformer/src/player.h
index 035a344..b39f4c5 100644
--- a/platformer/src/player.h
+++ b/platformer/src/player.h
@@ -36,7 +36,8 @@
* float forward_speed; ground speed along facing_yaw, m/s (>= 0)
* PlayerState state;
* bool heightened_movement; on for PLAYER_SECOND_JUMP_WINDOW s after
- * landing a normal or retreat jump, and for the
+ * any landing except the second jump's (normal
+ * or retreat jump, or a plain fall), and for the
* whole second jump, retreat jump, wall hang and
* every wall jump (NOT a plain first jump). On the ground, A gives the second
* jump; in the air, it lets the player wall hang.
@@ -51,6 +52,9 @@
* float skid_speed; forward_speed when the skid began
* float skid_yaw; direction the skid slides (the old run
* direction; the player already faces the stick)
+ * float visual_offset; m the drawn height is off the body's feet after a
+ * stair step (physics steps instantly; this eases
+ * back to 0 at STEP_SMOOTH_RATE so it looks smooth)
* float jump_buffer; s left on the last A press: while > 0, the first
* moment a jump is possible, it happens
* float wall_time; s since the current wall hang began
@@ -67,6 +71,9 @@
* void player_init(Player *p, Vec3 spawn);
* void player_reset(Player *p, Vec3 spawn);
* void player_update(Player *p, const Input *in, float camera_yaw, float dt);
+ * Vec3 player_visual_pos(const Player *p);
+ * Where to DRAW the player and point the camera: body.pos,
+ * but with stair steps smoothed out (see visual_offset).
* camera_yaw is a plain float so player.c doesn't depend on camera.h.
* const char *player_state_name(PlayerState s); "IDLE", "RUN", ... for debug
*
@@ -122,6 +129,7 @@ typedef struct Player {
float skid_time; /* s since the current skid began */
float skid_speed; /* forward_speed when the skid began */
float skid_yaw; /* direction the skid slides; facing_yaw already points at the stick */
+ float visual_offset; /* m the drawn/followed height lags the body after stair steps; eases to 0 */
float jump_buffer; /* s left on the last A press; a jump fires as soon as one is possible */
float wall_time; /* s since the current wall hang began */
float wall_jump_yaw; /* a wall jump's launch direction: the approach mirrored off the wall */
@@ -133,6 +141,7 @@ typedef struct Player {
void player_init(Player *p, Vec3 spawn);
void player_reset(Player *p, Vec3 spawn);
void player_update(Player *p, const Input *in, float camera_yaw, float dt);
+Vec3 player_visual_pos(const Player *p);
const char *player_state_name(PlayerState s);
#endif /* PLAYER_H */
diff --git a/platformer/src/render.c b/platformer/src/render.c
index 65237f5..3fd2ca6 100644
--- a/platformer/src/render.c
+++ b/platformer/src/render.c
@@ -60,7 +60,8 @@
* floor_y = world_floor_below(body.pos, body.half_width, NULL): the surface
* the player would land on, whether that's the ground or a platform.
* 7. Player box:
- * model = translate(body.pos) * rotate_y(facing_yaw)
+ * model = translate(player_visual_pos()) * rotate_y(facing_yaw)
+ * (body.pos with stair steps smoothed out)
* * scale(2*PLAYER_HALF_WIDTH, PLAYER_HEIGHT, 2*PLAYER_HALF_WIDTH)
* Bright color (e.g. red). Also draw a small second box as a "nose" on
* the front face (+Z in model space) so the facing direction is visible.
@@ -459,7 +460,8 @@ static void draw_shadow(Mat4 vp, const Player *player)
* so you can see which way it faces and tune the turn rate by eye. */
static void draw_player(Mat4 vp, const Player *player)
{
- Mat4 placed = mat4_mul(mat4_translate(player->body.pos), mat4_rotate_y(player->facing_yaw));
+ /* The visual position smooths out stair steps (see player_visual_pos). */
+ Mat4 placed = mat4_mul(mat4_translate(player_visual_pos(player)), mat4_rotate_y(player->facing_yaw));
Mat4 body = mat4_mul(placed, mat4_scale(vec3_make(2.0f * PLAYER_HALF_WIDTH,
PLAYER_HEIGHT,
diff --git a/platformer/src/world.c b/platformer/src/world.c
index c999f41..4c088aa 100644
--- a/platformer/src/world.c
+++ b/platformer/src/world.c
@@ -29,7 +29,9 @@
* WORLD_NO_CEILING.
* world_find_overlap: the first box that overlaps the body's full volume
* (footprint AND height range).
- * - Zones (level_build_zones()): stored separately from the boxes, so no
+ * - world_object_at(point, radius, hit): the first object (never a zone)
+ * that, grown by `radius` on every side, contains the point.
+ * - Zones (level_build_zones()): stored separately from the objects, so no
* collision query ever sees them. world_in_zone(point, kind): is the point
* inside (edges count) any zone of that kind.
* - world_clamp_to_bounds, world_spawn_point (level_spawn_point()),
@@ -176,6 +178,20 @@ Vec3 world_spawn_point(void)
return level_spawn_point();
}
+bool world_object_at(Vec3 point, float radius, Box *hit)
+{
+ for (int i = 0; i < s_box_count; i++) {
+ Box b = s_boxes[i];
+ if (point.x > b.min.x - radius && point.x < b.max.x + radius &&
+ point.y > b.min.y - radius && point.y < b.max.y + radius &&
+ point.z > b.min.z - radius && point.z < b.max.z + radius) {
+ *hit = b;
+ return true;
+ }
+ }
+ return false;
+}
+
int world_zone_count(void)
{
return s_zone_count;
diff --git a/platformer/src/world.h b/platformer/src/world.h
index 4cfcf71..422be28 100644
--- a/platformer/src/world.h
+++ b/platformer/src/world.h
@@ -3,8 +3,10 @@
* FILE: world.h
*
* PURPOSE: Public interface for "the level" while the game runs: a flat
- * square of ground with box-shaped platforms and terrain on it
- * (some of them moving). This API is how other modules ask
+ * square of ground with OBJECTS on it (solid boxes: platforms,
+ * terrain, walls; some of them moving) and ZONES (non-solid
+ * regions that change behavior inside them, even when box-shaped).
+ * "Box" here means an object unless it's a zone's bounds. This API is how other modules ask
* questions about the level's geometry.
* What's IN the level is decided by level.c (level.h).
*
@@ -68,6 +70,10 @@
* Vec3 world_spawn_point(void);
* Where the player starts / resets to (level_spawn_point()). The
* area around it is kept clear of platforms.
+ * bool world_object_at(Vec3 point, float radius, Box *hit);
+ * True (and *hit = that object) if the point is within `radius` of
+ * the inside of an OBJECT (zones never count). The camera uses it
+ * to keep the eye out of objects.
* int world_zone_count(void);
* Zone world_zone(int i); 0 <= i < world_zone_count() (render.c draws them)
* bool world_in_zone(Vec3 point, ZoneKind kind);
@@ -131,6 +137,7 @@ float world_ceiling_above(Vec3 head, float half_width);
bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit);
Vec3 world_clamp_to_bounds(Vec3 p);
Vec3 world_spawn_point(void);
+bool world_object_at(Vec3 point, float radius, Box *hit);
int world_zone_count(void);
Zone world_zone(int i);
bool world_in_zone(Vec3 point, ZoneKind kind);