From 5d078ec17b27afd159f8b9a8c1298fcac15d695d Mon Sep 17 00:00:00 2001 From: Schark Date: Thu, 24 Sep 2026 00:06:00 -0400 Subject: Fix recentering, default button --- platformer/src/camera.c | 319 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 256 insertions(+), 63 deletions(-) (limited to 'platformer/src/camera.c') diff --git a/platformer/src/camera.c b/platformer/src/camera.c index 531d58c..de87078 100644 --- a/platformer/src/camera.c +++ b/platformer/src/camera.c @@ -2,101 +2,294 @@ * ============================================================ * FILE: camera.c * - * PURPOSE: A player-controlled third-person orbit camera, in the spirit of - * SM64's "Lakitu" camera but much simpler. A good camera matters as - * much for movement feel as the movement code itself. + * PURPOSE: A player-controlled third-person camera in the spirit of SM64's + * "Lakitu": it orbits the player, but it does NOT glue the player + * to the center of the screen. The player moves around inside a + * screen-space zone and the camera only moves when it has to. The + * resulting off-center shots are what give it a cinematic feel. * - * FITS IN: Implements camera.h. Reads Input.look (controls.h) and the - * player's position (passed in by main.c). Produces matrices for - * render.c and a yaw for player.c. + * FITS IN: Implements camera.h. Reads Input.look and Input.recenter_pressed + * (controls.h) and the player's position and facing (passed in + * by main.c). + * Produces matrices for render.c and a yaw for player.c. * * IMPLEMENT: - * - camera_init(c, follow_point): - * target = follow_point + (0, CAMERA_TARGET_HEIGHT, 0) (snap, no smoothing) - * yaw = 0 (camera sits on +Z looking toward -Z) - * pitch = CAMERA_PITCH_DEFAULT - * distance = CAMERA_DISTANCE - * compute position. + * - The camera is described by WHERE THE PLAYER SITS ON SCREEN (`framing`, + * in NDC) and HOW FAR AWAY they are (`depth`), plus yaw/pitch. The eye + * position is derived from those every tick. That makes the zone rules + * simple clamps on `framing`, and it makes orbiting keep the player at the + * same screen spot (the camera orbits the PLAYER, not empty space). * - * - camera_update(c, in, follow_point, dt): - * 1. Orbit: yaw += in->look.x * CAMERA_YAW_SPEED * dt - * pitch += in->look.y * CAMERA_PITCH_SPEED * dt - * Clamp pitch to [CAMERA_PITCH_MIN, CAMERA_PITCH_MAX], wrap yaw with - * wrap_angle(). Check the sign by feel: pushing the right stick RIGHT - * should swing the camera so the view turns right (the camera moves - * left around the player). Flip the sign if it feels backwards and - * leave a comment. - * 2. Follow: move target toward (follow_point + target height) with - * frame-rate-independent exponential smoothing: - * t = 1 - expf(-CAMERA_FOLLOW_RATE * dt) - * target = vec3_lerp(target, desired, t) - * Comment WHY: a camera that snaps rigidly to the player looks jittery, - * and a little lag lets you see the motion. - * 3. Recompute position with the YAW CONVENTION in camera.h. + * - The zone, in window fractions, with NDC = 2 * fraction - 1: + * thirds lines at 1/3 and 2/3 of the window -> NDC +-1/3 + * resting zone CAMERA_ZONE_INSET inside the thirds lines + * -> NDC +-(1/3 - 2 * CAMERA_ZONE_INSET) + * Applied to both axes. The "subject" is the player's feet + + * CAMERA_TARGET_HEIGHT. + * + * - camera_init(c, follow_point): yaw 0 (camera on +Z looking toward -Z), + * pitch CAMERA_PITCH_DEFAULT, distance and depth CAMERA_DISTANCE, framing + * (0,0), not recentering, subject = follow_point + target height. Place it. + * + * - Recentering is a one-shot glide to "centered, behind the player". When + * Input.recenter_pressed arrives, capture recenter_yaw = the yaw that puts + * the camera behind the player's facing (follow_facing + PI). Until the glide + * finishes, the camera rides along rigidly with the player (their movement + * does NOT change framing/depth), so it always finishes even while they run. + * Then the normal zone rules take over again with the player centered. + * The right stick still works mid-glide: it steers recenter_yaw along with + * the view, so there's no fight (and a wobble while clicking is harmless). + * + * - camera_update(c, in, follow_point, follow_facing, aspect, dt): + * 1. Movement check: the subject counts as "moving" if it changed since + * last tick (this includes slowing to a stop and jumping in place). + * On recenter_pressed, start a recenter (see above). + * 2. Orbit: yaw -= look.x * CAMERA_YAW_SPEED * dt (the sign is flipped so + * stick-right turns the view right), pitch += look.y * + * CAMERA_PITCH_SPEED * dt, clamp pitch, wrap yaw. `framing` is kept, + * so the player stays at the same screen spot while orbiting. While + * recentering, apply the same yaw change to recenter_yaw, then ease yaw + * toward recenter_yaw (the short way around) at CAMERA_RECENTER_RATE. + * 3. Hold still (except while recentering): the camera does NOT follow + * by default. Project how far the subject moved onto the camera's + * right/up/forward axes. Right/up change where they sit on screen, and + * forward changes `depth`. + * 4. Depth: while recentering, ease `depth` to `distance` at + * CAMERA_RECENTER_RATE. Otherwise, only while the player moves, ease it + * at CAMERA_FOLLOW_RATE, so running toward/away from the camera briefly + * changes the player's size. Once they stop, the depth freezes too: a + * still player inside the zone means a still camera. + * 5. Framing rules, in priority order: + * recentering -> ease framing to (0,0) at CAMERA_RECENTER_RATE; the + * recenter ends once framing, depth and yaw all arrive. + * not moving -> ease framing to the nearest point of the resting zone + * at CAMERA_SETTLE_RATE. Inside the zone that point is + * the player itself, so the camera doesn't move at all. + * always -> hard clamp framing to the thirds lines. While moving, + * that's what pushes the camera along with the player. + * Then keep the eye at least CAMERA_MIN_HEIGHT above the player's feet + * (at low pitch, putting the player high on screen would otherwise sink + * the eye into the floor). + * 6. Place the eye: subject - right*side - up*lift - forward*depth, where + * side/lift are the framing converted to meters at `depth`. target = + * eye + forward * distance, which keeps the YAW CONVENTION exact. + * Every ease is exponential (1 - e^(-rate*dt)) and snaps when within a + * hair of its goal, so a settled camera is truly still. * - * - camera_yaw(): return c->yaw. * - camera_view(): mat4_look_at(position, target, (0,1,0)). - * - camera_projection(c, aspect): mat4_perspective(CAMERA_FOV_DEG converted to - * radians, aspect, CAMERA_NEAR, CAMERA_FAR). + * - camera_projection(c, aspect): mat4_perspective(CAMERA_FOV_DEG in radians, + * aspect, CAMERA_NEAR, CAMERA_FAR). * - * CONVENTIONS: Radians internally; FOV converted once from degrees. + * CONVENTIONS: Radians internally; FOV converted from degrees where used. + * 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, auto-recenter, shake, look-ahead. - * (Good candidates for later tuning, not for now.) + * NOT IN SCOPE: Camera collision, AUTOMATIC rotate-behind-the-player, shake, + * look-ahead. (Good candidates for later tuning, not for now.) * - * DONE WHEN: The right stick orbits smoothly all the way around the player, - * pitch stops at its limits, and the camera trails the running - * player slightly without jitter. + * DONE WHEN: A player standing anywhere inside the resting zone never moves + * the camera. Running lets the player reach, but never pass, the + * thirds lines. Stopping outside the resting zone eases the camera + * until the player sits on the zone's edge. The recenter button + * glides the camera behind the player with them centered, finishes + * even mid-run, and leaves the zone rules working afterward. The + * right stick orbits around the player without moving them on screen. * ============================================================ */ #include "camera.h" #include "config.h" -/* The point the camera wants to look at: a bit above the player's feet. */ -static Vec3 desired_target(Vec3 follow_point) +#define THIRDS_LINE_NDC (1.0f / 3.0f) /* the thirds lines, 1/3 and 2/3 of the window */ +#define STILL_EPSILON 1e-5f /* m moved per tick below which the player is "still" */ +#define EASE_SNAP 1e-3f /* eases jump to their goal when this close */ + +/* The camera's axes for a given orbit. forward points from the eye into the + * scene; right and up are the screen's right and up in world space. */ +typedef struct { + Vec3 right, up, forward; +} Basis; + +static Basis camera_basis(float yaw, float pitch) +{ + float sy = sinf(yaw), cy = cosf(yaw), sp = sinf(pitch), cp = cosf(pitch); + Basis b; + b.forward = vec3_make(-sy * cp, -sp, -cy * cp); /* minus the orbit direction */ + b.right = vec3_make(cy, 0.0f, -sy); + b.up = vec3_make(-sy * sp, cp, -cy * sp); + return b; +} + +/* The point we frame: a bit above the player's feet. */ +static Vec3 subject_point(Vec3 follow_point) { return vec3_add(follow_point, vec3_make(0.0f, CAMERA_TARGET_HEIGHT, 0.0f)); } -/* The YAW CONVENTION from camera.h. player.c depends on this exact formula. */ -static void recompute_position(Camera *c) +/* How many meters one NDC unit spans at a given depth. The screen edge + * (NDC 1) is depth * tan(fov/2) away from the view center, and the + * horizontal field is wider by the aspect ratio. */ +static Vec2 meters_per_ndc(float depth, float aspect) { - Vec3 offset = vec3_make(sinf(c->yaw) * cosf(c->pitch), - sinf(c->pitch), - cosf(c->yaw) * cosf(c->pitch)); - c->position = vec3_add(c->target, vec3_scale(offset, c->distance)); + float half_height = depth * tanf(CAMERA_FOV_DEG * (M3D_PI / 180.0f) * 0.5f); + return vec2_make(half_height * aspect, half_height); } -void camera_init(Camera *c, Vec3 follow_point) +/* Exponential ease: covers the same fraction of the remaining gap every + * second no matter the tick rate. Snaps when close so settling ends. */ +static float ease_toward(float current, float goal, float rate, float dt) { - c->target = desired_target(follow_point); /* snap, no smoothing */ - c->yaw = 0.0f; /* on +Z, looking toward -Z */ - c->pitch = CAMERA_PITCH_DEFAULT; - c->distance = CAMERA_DISTANCE; - recompute_position(c); + float next = current + (goal - current) * (1.0f - expf(-rate * dt)); + return fabsf(goal - next) < EASE_SNAP ? goal : next; } -void camera_update(Camera *c, const Input *in, Vec3 follow_point, float dt) +static Vec2 ease_toward_vec2(Vec2 current, Vec2 goal, float rate, float dt) +{ + return vec2_make(ease_toward(current.x, goal.x, rate, dt), + ease_toward(current.y, goal.y, rate, dt)); +} + +/* ease_toward for angles: turns the SHORT way around the circle. */ +static float ease_toward_angle(float current, float goal, float rate, float dt) +{ + float next = current + wrap_angle(goal - current) * (1.0f - expf(-rate * dt)); + return fabsf(wrap_angle(goal - next)) < EASE_SNAP ? goal : wrap_angle(next); +} + +/* Derives the eye and look-at point from the framing description. */ +static void place_camera(Camera *c, float aspect) +{ + Basis b = camera_basis(c->yaw, c->pitch); + Vec2 scale = meters_per_ndc(c->depth, aspect); + Vec3 eye = c->subject; + eye = vec3_sub(eye, vec3_scale(b.right, c->framing.x * scale.x)); + eye = vec3_sub(eye, vec3_scale(b.up, c->framing.y * scale.y)); + eye = vec3_sub(eye, vec3_scale(b.forward, c->depth)); + c->position = eye; + c->target = vec3_add(eye, vec3_scale(b.forward, c->distance)); +} + +/* Rotates around the player with the right stick. Right stick RIGHT turns the VIEW right, + * so the camera swings LEFT around the player. Forward is + * (-sin(yaw), 0, -cos(yaw)), so turning it toward the camera's right + * (+X at yaw 0) needs yaw to DECREASE, hence the -=. */ +static void orbit(Camera *c, const Input *in, float dt) { - /* 1. Orbit. The yaw sign is flipped (-=): pushing the stick RIGHT should - * turn the VIEW right, which means the camera swings LEFT around the - * player. The camera's forward is (-sin(yaw), 0, -cos(yaw)), so turning it - * toward the camera's right (+X at yaw 0) needs yaw to DECREASE. */ c->yaw -= in->look.x * CAMERA_YAW_SPEED * dt; c->pitch += in->look.y * CAMERA_PITCH_SPEED * dt; c->pitch = clampf(c->pitch, CAMERA_PITCH_MIN, CAMERA_PITCH_MAX); c->yaw = wrap_angle(c->yaw); +} + +/* Step 5: the framing rules (see the prompt above). */ +static void apply_framing_rules(Camera *c, bool subject_moving, float dt) +{ + const float third = THIRDS_LINE_NDC; + const float zone = THIRDS_LINE_NDC - 2.0f * CAMERA_ZONE_INSET; /* window % -> NDC is x2 */ + + if (c->recentering) { + /* The glide is over once every part of the shot has arrived; from + * then on the normal zone rules apply, starting from center. */ + c->framing = ease_toward_vec2(c->framing, vec2_make(0.0f, 0.0f), CAMERA_RECENTER_RATE, dt); + if (c->framing.x == 0.0f && c->framing.y == 0.0f && + c->depth == c->distance && c->yaw == c->recenter_yaw) + c->recentering = false; + } else if (!subject_moving) { + /* The nearest point of the resting zone. If the player is inside the + * zone this IS the player, so nothing moves: the shot holds. If they + * stopped outside it, the camera drifts until they're on its edge. */ + Vec2 edge = vec2_make(clampf(c->framing.x, -zone, zone), + clampf(c->framing.y, -zone, zone)); + c->framing = ease_toward_vec2(c->framing, edge, CAMERA_SETTLE_RATE, dt); + } + + /* Hard limit, always. While running, the player may drift out to the + * thirds lines but never past them; past that, the camera is pushed along. */ + c->framing.x = clampf(c->framing.x, -third, third); + c->framing.y = clampf(c->framing.y, -third, third); +} + +/* The highest the player may sit on screen (NDC y) before the eye would + * drop below CAMERA_MIN_HEIGHT above their feet. The eye's height relative + * to the feet is TARGET_HEIGHT - lift*cos(pitch) + depth*sin(pitch), solved + * for lift (meters), then converted to NDC. */ +static float max_framing_y(const Camera *c, float meters_per_ndc_y) +{ + float max_lift = (CAMERA_TARGET_HEIGHT - CAMERA_MIN_HEIGHT + c->depth * sinf(c->pitch)) + / cosf(c->pitch); + return max_lift / meters_per_ndc_y; +} + +void camera_init(Camera *c, Vec3 follow_point) +{ + c->yaw = 0.0f; /* on +Z, looking toward -Z */ + c->pitch = CAMERA_PITCH_DEFAULT; + c->distance = CAMERA_DISTANCE; + c->depth = CAMERA_DISTANCE; + c->framing = vec2_make(0.0f, 0.0f); + c->subject = subject_point(follow_point); + c->recentering = false; + c->recenter_yaw = 0.0f; + /* The aspect ratio only matters for a non-zero framing, which is 0 here. */ + place_camera(c, 1.0f); +} + +void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_facing, + float aspect, float dt) +{ + /* 1. Did the player move since last tick? And start a recenter glide + * toward the shot from behind the player (opposite their facing). */ + Vec3 subject = subject_point(follow_point); + Vec3 moved_by = vec3_sub(subject, c->subject); + bool subject_moving = vec3_length(moved_by) > STILL_EPSILON; + c->subject = subject; + if (in->recenter_pressed) { + c->recentering = true; + c->recenter_yaw = wrap_angle(follow_facing + M3D_PI); + } + + /* 2. Orbit around the player. Mid-recenter, the stick turns the glide's + * goal along with the view, so the two never fight. */ + float yaw_before = c->yaw; + orbit(c, in, dt); + if (c->recentering) { + c->recenter_yaw = wrap_angle(c->recenter_yaw + wrap_angle(c->yaw - yaw_before)); + c->yaw = ease_toward_angle(c->yaw, c->recenter_yaw, CAMERA_RECENTER_RATE, dt); + } + Basis b = camera_basis(c->yaw, c->pitch); + + /* 3. Hold still: with the eye fixed, the player's motion changes where + * they sit relative to it. Work in meters, since NDC depends on depth. + * Mid-recenter the camera rides along with the player instead, so their + * running can't stop the glide from finishing. */ + Vec2 scale = meters_per_ndc(c->depth, aspect); + float side = c->framing.x * scale.x; + float lift = c->framing.y * scale.y; + if (!c->recentering) { + side += vec3_dot(moved_by, b.right); + lift += vec3_dot(moved_by, b.up); + c->depth += vec3_dot(moved_by, b.forward); + } + + /* 4. Depth lag: while the player moves, the distance catches back up a + * little late, which lets you feel them running toward or away from you. + * It freezes when they stop, so a still player means a still camera. */ + if (c->recentering) + c->depth = ease_toward(c->depth, c->distance, CAMERA_RECENTER_RATE, dt); + else if (subject_moving) + c->depth = ease_toward(c->depth, c->distance, CAMERA_FOLLOW_RATE, dt); + + scale = meters_per_ndc(c->depth, aspect); + c->framing = vec2_make(side / scale.x, lift / scale.y); - /* 2. Follow with exponential smoothing. A camera that snaps rigidly to the - * player looks jittery, and a little lag lets you see the motion. Using - * 1 - e^(-rate*dt) instead of a fixed lerp factor keeps the catch-up speed - * the same regardless of the tick rate. */ - float t = 1.0f - expf(-CAMERA_FOLLOW_RATE * dt); - c->target = vec3_lerp(c->target, desired_target(follow_point), t); + /* 5. Zone rules, then keep the eye out of the floor. */ + apply_framing_rules(c, subject_moving, dt); + float max_y = max_framing_y(c, scale.y); + if (c->framing.y > max_y) + c->framing.y = max_y; - /* 3. Place the eye on the orbit sphere around the new target. */ - recompute_position(c); + /* 6. Derive the eye from the framing. */ + place_camera(c, aspect); } float camera_yaw(const Camera *c) -- cgit v1.2.3-18-g5258