diff options
| author | Schark <jordan@schark.online> | 2026-09-24 00:06:00 -0400 |
|---|---|---|
| committer | Schark <jordan@schark.online> | 2026-09-24 00:06:00 -0400 |
| commit | 5d078ec17b27afd159f8b9a8c1298fcac15d695d (patch) | |
| tree | 59c2d29d64933e849f17bb50d34ea71b88c13602 /platformer/src | |
| parent | 23ef8bc20310bdfad52b07a3f1e4e61c98896841 (diff) | |
| download | gamedev-5d078ec17b27afd159f8b9a8c1298fcac15d695d.tar.gz gamedev-5d078ec17b27afd159f8b9a8c1298fcac15d695d.zip | |
Fix recentering, default button
Diffstat (limited to 'platformer/src')
| -rw-r--r-- | platformer/src/camera.c | 319 | ||||
| -rw-r--r-- | platformer/src/camera.h | 57 | ||||
| -rw-r--r-- | platformer/src/config.h | 16 | ||||
| -rw-r--r-- | platformer/src/controls.c | 42 | ||||
| -rw-r--r-- | platformer/src/controls.h | 17 | ||||
| -rw-r--r-- | platformer/src/main.c | 22 |
6 files changed, 360 insertions, 113 deletions
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) diff --git a/platformer/src/camera.h b/platformer/src/camera.h index cdceaec..84897d7 100644 --- a/platformer/src/camera.h +++ b/platformer/src/camera.h @@ -2,9 +2,10 @@ * ============================================================ * FILE: camera.h * - * PURPOSE: Public interface for the third-person orbit camera: it sits - * on a sphere around the player and the right stick moves it - * around that sphere. + * PURPOSE: Public interface for the third-person "framing" camera: it + * orbits the player (right stick), and instead of pinning the player + * to the center of the screen it lets them wander inside a screen- + * space zone, SM64-style. Off-center framing is intentional. * * FITS IN: main.c calls camera_update() each tick AFTER player_update(). * player_update() receives camera_yaw() so "stick up" means "away @@ -13,17 +14,29 @@ * INTERFACE: Use these exact names: * * typedef struct Camera { - * Vec3 target; the smoothed point we look at (near the player) - * float yaw; orbit angle around the target, radians - * float pitch; angle above horizontal, radians - * float distance; meters from target - * Vec3 position; derived eye position (recomputed in camera_update) + * Vec3 target; derived look-at point, `distance` ahead of the eye + * float yaw; orbit angle around the player, radians + * float pitch; angle above horizontal, radians + * float distance; resting distance from eye to player, meters + * Vec3 position; derived eye position (recomputed in camera_update) + * Vec2 framing; where the player sits on screen, in NDC: + * (0,0) = center, +x = right, +y = up, edges = +-1 + * float depth; current eye-to-player distance along the view + * direction; eases toward `distance` (follow lag) + * Vec3 subject; the point we frame (player feet + target height) + * as of last tick, to detect whether they moved + * bool recentering; true during the recenter glide + * float recenter_yaw; where the glide is headed: behind the player * } Camera; * * void camera_init(Camera *c, Vec3 follow_point); - * void camera_update(Camera *c, const Input *in, Vec3 follow_point, float dt); + * void camera_update(Camera *c, const Input *in, Vec3 follow_point, + * float follow_facing, float aspect, float dt); * follow_point = the player's feet position; the camera adds - * CAMERA_TARGET_HEIGHT itself. + * CAMERA_TARGET_HEIGHT itself. follow_facing = the player's + * facing_yaw, used by recenter to swing behind them. aspect = + * framebuffer width/height, needed because the framing zone is + * defined in screen space. * float camera_yaw(const Camera *c); * Mat4 camera_view(const Camera *c); * Mat4 camera_projection(const Camera *c, float aspect); @@ -36,12 +49,12 @@ * forward (toward target) = (-sin(yaw), 0, -cos(yaw)) * right = ( cos(yaw), 0, -sin(yaw)) * - * Include "math3d.h" and "controls.h". + * Include <stdbool.h>, "math3d.h" and "controls.h". * * CONVENTIONS: Include guard: CAMERA_H. * * NOT IN SCOPE: Collision with geometry, automatic "behind the player" - * recentering, cutscene/fixed cameras, zoom levels. + * rotation, cutscene/fixed cameras, zoom levels. * * DONE WHEN: player.c and render.c can be written against this header alone. * ============================================================ @@ -49,19 +62,27 @@ #ifndef CAMERA_H #define CAMERA_H +#include <stdbool.h> + #include "controls.h" #include "math3d.h" typedef struct Camera { - Vec3 target; /* the smoothed point we look at (near the player) */ - float yaw; /* orbit angle around the target, radians */ - float pitch; /* angle above horizontal, radians */ - float distance; /* meters from target */ - Vec3 position; /* derived eye position (recomputed in camera_update) */ + Vec3 target; /* derived look-at point, `distance` ahead of the eye */ + float yaw; /* orbit angle around the player, radians */ + float pitch; /* angle above horizontal, radians */ + float distance; /* resting distance from eye to player, meters */ + Vec3 position; /* derived eye position (recomputed in camera_update) */ + Vec2 framing; /* player's screen position in NDC: (0,0) = center, edges = +-1 */ + float depth; /* current eye-to-player distance along the view; eases to `distance` */ + Vec3 subject; /* last tick's framed point (feet + target height) */ + bool recentering; /* gliding to "centered, behind the player" */ + float recenter_yaw; /* the yaw that glide is headed for */ } Camera; void camera_init(Camera *c, Vec3 follow_point); -void camera_update(Camera *c, const Input *in, Vec3 follow_point, float dt); +void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_facing, + float aspect, float dt); float camera_yaw(const Camera *c); Mat4 camera_view(const Camera *c); Mat4 camera_projection(const Camera *c, float aspect); diff --git a/platformer/src/config.h b/platformer/src/config.h index 5f1e3db..b615c4c 100644 --- a/platformer/src/config.h +++ b/platformer/src/config.h @@ -59,7 +59,15 @@ * CAMERA_PITCH_MAX 1.20f rad * CAMERA_YAW_SPEED 2.5f rad/s at full right-stick tilt * CAMERA_PITCH_SPEED 1.5f rad/s at full right-stick tilt - * CAMERA_FOLLOW_RATE 8.0f 1/s; higher = camera catches up faster + * CAMERA_FOLLOW_RATE 8.0f 1/s; how fast the camera's DISTANCE catches up + * when the player runs toward/away from it + * CAMERA_ZONE_INSET 0.05f fraction of the window the resting zone sits + * inside the thirds lines + * CAMERA_SETTLE_RATE 3.0f 1/s; how fast the camera re-frames a player + * who stopped outside the resting zone + * CAMERA_RECENTER_RATE 6.0f 1/s; how fast the recenter button swings + * behind the player and centers them + * CAMERA_MIN_HEIGHT 0.5f m; lowest the eye may go above the player's feet * CAMERA_FOV_DEG 60.0f vertical field of view * CAMERA_NEAR 0.1f * CAMERA_FAR 500.0f @@ -125,7 +133,11 @@ #define CAMERA_PITCH_MAX 1.20f /* rad; highest the camera can swing (near top-down) */ #define CAMERA_YAW_SPEED 2.5f /* rad/s orbit at full right-stick tilt; higher = faster spin */ #define CAMERA_PITCH_SPEED 1.5f /* rad/s tilt at full right-stick tilt */ -#define CAMERA_FOLLOW_RATE 8.0f /* 1/s; higher = camera catches up to the player faster */ +#define CAMERA_FOLLOW_RATE 8.0f /* 1/s; how fast the distance catches up when running toward/away; lower = more lag */ +#define CAMERA_ZONE_INSET 0.05f /* window fraction the resting zone sits inside the thirds lines; higher = tighter zone */ +#define CAMERA_SETTLE_RATE 3.0f /* 1/s; re-framing after stopping outside the zone; lower = lazier, more cinematic */ +#define CAMERA_RECENTER_RATE 6.0f /* 1/s; how fast recenter swings behind the player and centers them */ +#define CAMERA_MIN_HEIGHT 0.5f /* m; lowest the eye may sit above the player's feet (keeps it out of the floor) */ #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/controls.c b/platformer/src/controls.c index f9f55c0..3bcf7b7 100644 --- a/platformer/src/controls.c +++ b/platformer/src/controls.c @@ -14,7 +14,8 @@ * * IMPLEMENT: * - Private state: the previous tick's button states (for the "pressed" - * edges), e.g. `static bool s_prev_jump, s_prev_reset, s_prev_quit;` + * edges), e.g. `static bool s_prev_jump, s_prev_reset, s_prev_recenter, + * s_prev_quit;` * * - Finding a gamepad (keep it simple; this also handles hot-plugging): * each poll, loop GLFW_JOYSTICK_1..GLFW_JOYSTICK_LAST and use the first @@ -48,6 +49,7 @@ * look.y = -GLFW_GAMEPAD_AXIS_RIGHT_Y * jump = GLFW_GAMEPAD_BUTTON_A * reset = GLFW_GAMEPAD_BUTTON_BACK + * recenter = GLFW_GAMEPAD_BUTTON_RIGHT_THUMB (click the right stick) * quit = GLFW_GAMEPAD_BUTTON_START * * - RADIAL deadzone (write a small static helper, used for both sticks): @@ -65,7 +67,7 @@ * - Keyboard fallback, used ONLY when no gamepad is connected: * W/S/A/D -> move (normalize diagonals so the length is <= 1) * Arrow keys -> look - * Space -> jump, R -> reset, Esc -> quit + * Space -> jump, R -> reset, C -> recenter camera, Esc -> quit * Read keys with glfwGetKey(window_handle(), key). * Esc also quits when a gamepad IS connected (so you can always exit). * @@ -115,7 +117,7 @@ static const char *EXTRA_GAMEPAD_MAPPINGS = "dpleft:h0.8,platform:Linux,\n"; /* Last tick's button states, for turning "down" into "pressed this tick". */ -static bool s_prev_jump, s_prev_reset, s_prev_quit; +static bool s_prev_jump, s_prev_reset, s_prev_recenter, s_prev_quit; /* The joystick id we're reading, or NO_GAMEPAD. Only used to notice (and * print) when a pad connects or disconnects. */ @@ -123,7 +125,7 @@ static int s_active_jid = NO_GAMEPAD; /* Raw buttons for this tick, before edge detection. */ typedef struct { - bool jump, reset, quit; + bool jump, reset, recenter, quit; } Buttons; /* Radial deadzone. WHY radial and not per-axis: a per-axis deadzone snaps @@ -168,7 +170,7 @@ static void report_gamepad_change(int jid) static Buttons read_gamepad(int jid, Input *out) { - Buttons buttons = { false, false, false }; + Buttons buttons = { false, false, false, false }; GLFWgamepadstate state; if (!glfwGetGamepadState(jid, &state)) return buttons; @@ -181,9 +183,10 @@ static Buttons read_gamepad(int jid, Input *out) -state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y], LOOK_DEADZONE); - buttons.jump = state.buttons[GLFW_GAMEPAD_BUTTON_A] == GLFW_PRESS; - buttons.reset = state.buttons[GLFW_GAMEPAD_BUTTON_BACK] == GLFW_PRESS; - buttons.quit = state.buttons[GLFW_GAMEPAD_BUTTON_START] == GLFW_PRESS; + buttons.jump = state.buttons[GLFW_GAMEPAD_BUTTON_A] == GLFW_PRESS; + buttons.reset = state.buttons[GLFW_GAMEPAD_BUTTON_BACK] == GLFW_PRESS; + buttons.recenter = state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB] == GLFW_PRESS; + buttons.quit = state.buttons[GLFW_GAMEPAD_BUTTON_START] == GLFW_PRESS; out->using_gamepad = true; return buttons; } @@ -217,9 +220,10 @@ static Buttons read_keyboard(Input *out) look = vec2_make(look.x / len, look.y / len); out->look = look; - buttons.jump = key_down(GLFW_KEY_SPACE); - buttons.reset = key_down(GLFW_KEY_R); - buttons.quit = false; /* Esc is handled for every device in controls_poll */ + buttons.jump = key_down(GLFW_KEY_SPACE); + buttons.reset = key_down(GLFW_KEY_R); + buttons.recenter = key_down(GLFW_KEY_C); + buttons.quit = false; /* Esc is handled for every device in controls_poll */ out->using_gamepad = false; return buttons; } @@ -264,12 +268,14 @@ void controls_poll(Input *out) now.quit = true; /* Edges: "pressed" is true only on the first tick the button is down. */ - out->jump_held = now.jump; - out->jump_pressed = now.jump && !s_prev_jump; - out->reset_pressed = now.reset && !s_prev_reset; - out->quit_pressed = now.quit && !s_prev_quit; + out->jump_held = now.jump; + out->jump_pressed = now.jump && !s_prev_jump; + out->reset_pressed = now.reset && !s_prev_reset; + out->recenter_pressed = now.recenter && !s_prev_recenter; + out->quit_pressed = now.quit && !s_prev_quit; - s_prev_jump = now.jump; - s_prev_reset = now.reset; - s_prev_quit = now.quit; + s_prev_jump = now.jump; + s_prev_reset = now.reset; + s_prev_recenter = now.recenter; + s_prev_quit = now.quit; } diff --git a/platformer/src/controls.h b/platformer/src/controls.h index fc2ac4d..972ce9e 100644 --- a/platformer/src/controls.h +++ b/platformer/src/controls.h @@ -20,6 +20,8 @@ * bool jump_held; A is down this tick * bool jump_pressed; A went down THIS tick (true for one tick only) * bool reset_pressed; Back/Select went down this tick (one tick only) + * bool recenter_pressed; right stick click went down this tick (one tick + * only): swing the camera behind the player, centered * bool quit_pressed; Start (or Esc) went down this tick (one tick only) * bool using_gamepad; true if a gamepad produced this Input (debug info) * } Input; @@ -48,13 +50,14 @@ #include "math3d.h" typedef struct Input { - Vec2 move; /* left stick after deadzone, length 0..1. +x = right, +y = forward */ - Vec2 look; /* right stick after deadzone, length 0..1. +x = orbit right, +y = tilt up */ - bool jump_held; /* A is down this tick */ - bool jump_pressed; /* A went down THIS tick (one tick only) */ - bool reset_pressed; /* Back/Select went down this tick (one tick only) */ - bool quit_pressed; /* Start (or Esc) went down this tick (one tick only) */ - bool using_gamepad; /* true if a gamepad produced this Input (debug info) */ + Vec2 move; /* left stick after deadzone, length 0..1. +x = right, +y = forward */ + Vec2 look; /* right stick after deadzone, length 0..1. +x = orbit right, +y = tilt up */ + bool jump_held; /* A is down this tick */ + bool jump_pressed; /* A went down THIS tick (one tick only) */ + bool reset_pressed; /* Back/Select went down this tick (one tick only) */ + bool recenter_pressed; /* right stick click went down this tick (one tick only) */ + bool quit_pressed; /* Start (or Esc) went down this tick (one tick only) */ + bool using_gamepad; /* true if a gamepad produced this Input (debug info) */ } Input; void controls_init(void); diff --git a/platformer/src/main.c b/platformer/src/main.c index 5b11772..8f5e510 100644 --- a/platformer/src/main.c +++ b/platformer/src/main.c @@ -32,9 +32,12 @@ * while accumulator >= TICK_DT: // 0, 1 or several ticks * controls_poll(&input); * if input.quit_pressed: window_request_close(); - * if input.reset_pressed: player_reset(&player, world_spawn_point()); + * if input.reset_pressed: + * player_reset(&player, world_spawn_point()); + * camera_init(&camera, player.body.pos); // see WHY below * player_update(&player, &input, camera_yaw(&camera), TICK_DT); - * camera_update(&camera, &input, player.body.pos, TICK_DT); + * camera_update(&camera, &input, player.body.pos, player.facing_yaw, + * window_aspect(), TICK_DT); * accumulator -= TICK_DT; * * render_frame(&camera, &player, window_aspect()); @@ -46,6 +49,9 @@ * ran logic at 30 Hz; we use TICK_HZ (60). * WHY player before camera: the camera then follows where the player IS, * not where it was a tick ago. SM64 updates Mario before the camera too. + * WHY reset the camera too: the camera frames the player by tracking how + * far they moved each tick, so a teleport back to spawn would look like a + * 100 m sprint. Re-initializing gives a clean, centered start instead. * * - Debug readout for tuning (a few times per second, not every frame): * window_set_title() with something like @@ -61,7 +67,8 @@ * NOT IN SCOPE: Menus, pause, command-line args, state interpolation. * * DONE WHEN: `make run` opens the window. The player can run, turn and jump - * with a gamepad, the camera orbits, Back resets, Start quits, and + * with a gamepad, the camera orbits and frames them (right stick + * click recenters behind them), Back resets, Start quits, and * the title bar shows live state/speed. * ============================================================ */ @@ -138,12 +145,17 @@ int main(void) controls_poll(&input); if (input.quit_pressed) window_request_close(); - if (input.reset_pressed) + if (input.reset_pressed) { + /* The camera tracks per-tick movement, so a teleport needs a + * fresh camera rather than a 100 m "sprint" to follow. */ player_reset(&player, world_spawn_point()); + camera_init(&camera, player.body.pos); + } /* Player before camera, so the camera follows where the player IS, * not where it was a tick ago. SM64 updates Mario before the camera too. */ player_update(&player, &input, camera_yaw(&camera), TICK_DT); - camera_update(&camera, &input, player.body.pos, TICK_DT); + camera_update(&camera, &input, player.body.pos, player.facing_yaw, + window_aspect(), TICK_DT); accumulator -= TICK_DT; } |
