diff options
Diffstat (limited to '')
| -rw-r--r-- | platformer/src/camera.c | 102 | ||||
| -rw-r--r-- | platformer/src/config.h | 8 |
2 files changed, 96 insertions, 14 deletions
diff --git a/platformer/src/camera.c b/platformer/src/camera.c index 8f436f5..3375585 100644 --- a/platformer/src/camera.c +++ b/platformer/src/camera.c @@ -45,6 +45,22 @@ * 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). * + * - Sideways runs get two extra behaviors, both scaled by the "lateral + * share": how much of the player's horizontal motion goes across the + * screen (-1 = straight left, +1 = straight right, 0 = straight toward/away + * from the camera or not moving horizontally). Up/down runs are untouched. + * lead room the camera works AHEAD of the player so they drift to + * the TRAILING side of the running bound (running right + * -> left thirds line), leaving open screen in the + * direction they're heading, for upcoming obstacles. + * back-quarter swing the camera slowly turns toward CAMERA_SWING_OFFSET + * off directly-behind the player's motion, on the side + * it's already on, so a side-on view becomes a view down + * the run. Inside that back quarter it doesn't swing. + * Controls stay camera-relative, so holding the stick + * sideways while it swings makes the player arc, like + * Mario circling in SM64. That's intentional. + * * - 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). @@ -55,10 +71,17 @@ * 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. + * Otherwise, while moving, apply the back-quarter swing: approach its + * goal yaw at CAMERA_SWING_SPEED * |lateral share| rad/s (constant rate, + * so the swing is slow and even). Framing is kept here too. * 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`. + * vertically or in depth by default. Project how far the subject moved + * onto the camera's up/forward axes. Up changes where they sit on + * screen, and forward changes `depth`. Sideways motion is NOT turned + * into screen drift: horizontally the camera keeps pace with the + * player, and the lead-room rule alone decides where they sit across + * the screen (a raw drift would push them to the LEADING side, which is + * exactly what lead room avoids). * 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 @@ -70,8 +93,11 @@ * 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 running bound. While moving, - * that's what pushes the camera along with the player. + * moving -> lead room: ease framing.x toward the running bound's + * trailing edge at CAMERA_LEAD_RATE * |lateral share|. + * always -> hard clamp framing to the running bound. On runs + * toward/away from the camera, 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). @@ -89,7 +115,8 @@ * 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, AUTOMATIC rotate-behind-the-player, shake, + * NOT IN SCOPE: Camera collision, 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.) * * DONE WHEN: A player standing anywhere inside the resting zone never moves @@ -99,6 +126,9 @@ * 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. + * A sideways run slowly carries the player to the trailing thirds + * line and swings the camera to ~30 deg off behind them, while runs + * straight toward/away from the camera behave as before. * ============================================================ */ @@ -226,10 +256,38 @@ static Vec2 clamp_to_box(Vec2 p, ScreenBox box) clampf(p.y, box.min.y, box.max.y)); } -/* Step 5: the framing rules (see the prompt above). */ -static void apply_framing_rules(Camera *c, bool subject_moving, float dt) +/* How much of the player's horizontal motion goes across the screen: + * -1 = straight left, +1 = straight right, 0 = straight toward/away from the + * camera (or no horizontal motion at all, like a jump in place). */ +static float lateral_share(Vec3 moved_by, Vec3 right) +{ + Vec3 flat = vec3_make(moved_by.x, 0.0f, moved_by.z); + float len = vec3_length(flat); + if (len < STILL_EPSILON) + return 0.0f; + return vec3_dot(flat, right) / len; /* right is horizontal and unit length */ +} + +/* Back-quarter swing: on a sideways run, slowly turn the camera toward + * CAMERA_SWING_OFFSET off directly-behind the player's motion, staying on the + * side it's already on. A side-on view becomes a view down the run, without + * ever snapping squarely behind. Inside that back quarter, nothing happens. */ +static void swing_toward_back_quarter(Camera *c, Vec3 moved_by, float lateral, float dt) { + if (lateral == 0.0f) + return; + float heading = atan2f(moved_by.x, moved_by.z); /* same convention as facing_yaw */ + float behind = wrap_angle(heading + M3D_PI); /* camera yaw directly behind */ + float off = wrap_angle(c->yaw - behind); /* signed: which side we're on */ + if (fabsf(off) <= CAMERA_SWING_OFFSET) + return; + float goal = behind + (off > 0.0f ? CAMERA_SWING_OFFSET : -CAMERA_SWING_OFFSET); + c->yaw = approach_angle(c->yaw, goal, CAMERA_SWING_SPEED * fabsf(lateral) * dt); +} +/* Step 5: the framing rules (see the prompt above). */ +static void apply_framing_rules(Camera *c, bool subject_moving, float lateral, float dt) +{ 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. */ @@ -243,10 +301,19 @@ static void apply_framing_rules(Camera *c, bool subject_moving, float dt) * stopped outside it, the camera drifts until they're on its edge. */ Vec2 edge = clamp_to_box(c->framing, resting_zone()); c->framing = ease_toward_vec2(c->framing, edge, CAMERA_SETTLE_RATE, dt); + } else if (lateral != 0.0f) { + /* Lead room: on a sideways run, work the player toward the TRAILING + * edge (running right -> left thirds line), so the open screen is in + * the direction they're heading and upcoming obstacles stay visible. */ + ScreenBox bound = running_bound(); + float trailing_edge = lateral > 0.0f ? bound.min.x : bound.max.x; + c->framing.x = ease_toward(c->framing.x, trailing_edge, + CAMERA_LEAD_RATE * fabsf(lateral), dt); } - /* Hard limit, always. While running, the player may drift out to the - * running bound but never past it; past that, the camera is pushed along. */ + /* Hard limit, always. Running toward/away from the camera, the player may + * drift out to the running bound but never past it; past that, the camera + * is pushed along. (Across the screen, lead room already stays inside.) */ c->framing = clamp_to_box(c->framing, running_bound()); } @@ -290,24 +357,31 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f } /* 2. Orbit around the player. Mid-recenter, the stick turns the glide's - * goal along with the view, so the two never fight. */ + * goal along with the view, so the two never fight. Otherwise a + * sideways run slowly swings the camera toward the back quarter. */ float yaw_before = c->yaw; orbit(c, in, dt); + float lateral = 0.0f; /* sideways effects are off during a recenter glide */ + if (!c->recentering) + lateral = lateral_share(moved_by, camera_basis(c->yaw, c->pitch).right); 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); + } else { + swing_toward_back_quarter(c, moved_by, lateral, 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. */ + * running can't stop the glide from finishing. Sideways motion is left + * out on purpose: horizontally the camera keeps pace, and the lead-room + * rule (step 5) decides where the player sits across the screen. */ 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); } @@ -324,7 +398,7 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f c->framing = vec2_make(side / scale.x, lift / scale.y); /* 5. Zone rules, then keep the eye out of the floor. */ - apply_framing_rules(c, subject_moving, dt); + apply_framing_rules(c, subject_moving, lateral, dt); float max_y = max_framing_y(c, scale.y); if (c->framing.y > max_y) c->framing.y = max_y; diff --git a/platformer/src/config.h b/platformer/src/config.h index c71ae35..ba2cdf1 100644 --- a/platformer/src/config.h +++ b/platformer/src/config.h @@ -70,6 +70,11 @@ * 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_LEAD_RATE 1.0f 1/s; how fast a sideways run carries the player + * to the trailing thirds line (lead room ahead) + * CAMERA_SWING_OFFSET 0.52f rad (~30 deg) off directly-behind that a + * sideways run swings the camera toward + * CAMERA_SWING_SPEED 0.5f rad/s max swing rate on a fully sideways run * CAMERA_FOV_DEG 60.0f vertical field of view * CAMERA_NEAR 0.1f * CAMERA_FAR 500.0f @@ -141,6 +146,9 @@ #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_LEAD_RATE 1.0f /* 1/s; sideways runs push the player to the trailing thirds line; higher = faster lead */ +#define CAMERA_SWING_OFFSET 0.52f /* rad (~30 deg) off directly-behind that sideways runs swing toward; 0 = fully behind */ +#define CAMERA_SWING_SPEED 0.5f /* rad/s swing rate on a fully sideways run; lower = slower, subtler swing */ #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) */ |
