diff options
| author | Schark <jordan@schark.online> | 2026-09-24 00:38:44 -0400 |
|---|---|---|
| committer | Schark <jordan@schark.online> | 2026-09-24 00:38:44 -0400 |
| commit | 651fa5ef62cb9e8e1f47c07cc8d6b3abc4d70cd5 (patch) | |
| tree | f50f9fb96ed129a32ae09d76e8a87d6593c0be03 /platformer/src/camera.c | |
| parent | 5d078ec17b27afd159f8b9a8c1298fcac15d695d (diff) | |
| download | gamedev-651fa5ef62cb9e8e1f47c07cc8d6b3abc4d70cd5.tar.gz gamedev-651fa5ef62cb9e8e1f47c07cc8d6b3abc4d70cd5.zip | |
Further camera adjustments, feeling great
Diffstat (limited to '')
| -rw-r--r-- | platformer/src/camera.c | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/platformer/src/camera.c b/platformer/src/camera.c index de87078..8f436f5 100644 --- a/platformer/src/camera.c +++ b/platformer/src/camera.c @@ -20,12 +20,17 @@ * simple clamps on `framing`, and it makes orbiting keep the player at the * same screen spot (the camera orbits the PLAYER, not empty space). * - * - 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. + * - Two screen boxes, given in window fractions (0 = left/bottom, 1 = + * right/top) and converted with NDC = 2 * fraction - 1: + * resting zone where a stopped player may sit without the camera moving + * x: 1/3 + CAMERA_ZONE_INSET .. 2/3 - CAMERA_ZONE_INSET + * y: 1/3 + CAMERA_ZONE_INSET .. CAMERA_ZONE_TOP + * running bound the resting zone grown by CAMERA_ZONE_INSET on every side + * x: 1/3 .. 2/3 (the thirds lines) + * y: 1/3 .. CAMERA_ZONE_TOP + CAMERA_ZONE_INSET + * The top is lower than the thirds on purpose: it keeps the view AHEAD of + * a player running away from the camera open, so upcoming obstacles stay + * visible. 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 @@ -65,7 +70,7 @@ * 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, + * always -> hard clamp framing to the running bound. 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 @@ -89,7 +94,7 @@ * * 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 + * running bound. 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 @@ -100,7 +105,7 @@ #include "camera.h" #include "config.h" -#define THIRDS_LINE_NDC (1.0f / 3.0f) /* the thirds lines, 1/3 and 2/3 of the window */ +#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" */ #define EASE_SNAP 1e-3f /* eases jump to their goal when this close */ @@ -181,11 +186,49 @@ static void orbit(Camera *c, const Input *in, float dt) c->yaw = wrap_angle(c->yaw); } +/* A rectangle on screen, in NDC. */ +typedef struct { + Vec2 min, max; +} ScreenBox; + +/* Window fraction (0 = left/bottom, 1 = right/top) -> NDC (-1 .. 1). */ +static float window_to_ndc(float fraction) +{ + return 2.0f * fraction - 1.0f; +} + +static ScreenBox screen_box(float left, float right, float bottom, float top) +{ + ScreenBox box; + box.min = vec2_make(window_to_ndc(left), window_to_ndc(bottom)); + box.max = vec2_make(window_to_ndc(right), window_to_ndc(top)); + return box; +} + +/* Where a stopped player may sit without the camera moving. */ +static ScreenBox resting_zone(void) +{ + return screen_box(THIRD + CAMERA_ZONE_INSET, 2.0f * THIRD - CAMERA_ZONE_INSET, + THIRD + CAMERA_ZONE_INSET, CAMERA_ZONE_TOP); +} + +/* How far a moving player may drift before pushing the camera along: the + * resting zone grown by CAMERA_ZONE_INSET on every side. */ +static ScreenBox running_bound(void) +{ + return screen_box(THIRD, 2.0f * THIRD, + THIRD, CAMERA_ZONE_TOP + CAMERA_ZONE_INSET); +} + +static Vec2 clamp_to_box(Vec2 p, ScreenBox box) +{ + return vec2_make(clampf(p.x, box.min.x, box.max.x), + 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) { - 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 @@ -198,15 +241,13 @@ static void apply_framing_rules(Camera *c, bool subject_moving, float dt) /* 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)); + Vec2 edge = clamp_to_box(c->framing, resting_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); + * running bound but never past it; past that, the camera is pushed along. */ + c->framing = clamp_to_box(c->framing, running_bound()); } /* The highest the player may sit on screen (NDC y) before the eye would |
