summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-24 00:38:44 -0400
committerSchark <jordan@schark.online>2026-09-24 00:38:44 -0400
commit651fa5ef62cb9e8e1f47c07cc8d6b3abc4d70cd5 (patch)
treef50f9fb96ed129a32ae09d76e8a87d6593c0be03
parent5d078ec17b27afd159f8b9a8c1298fcac15d695d (diff)
downloadgamedev-651fa5ef62cb9e8e1f47c07cc8d6b3abc4d70cd5.tar.gz
gamedev-651fa5ef62cb9e8e1f47c07cc8d6b3abc4d70cd5.zip
Further camera adjustments, feeling great
-rw-r--r--platformer/README.md6
-rw-r--r--platformer/src/camera.c73
-rw-r--r--platformer/src/config.h9
3 files changed, 67 insertions, 21 deletions
diff --git a/platformer/README.md b/platformer/README.md
index 758fa1a..f97c9dd 100644
--- a/platformer/README.md
+++ b/platformer/README.md
@@ -45,8 +45,10 @@ game until the controls feel great.
- **Camera-relative controls.** "Up" on the stick means "away from the camera". The player orbits
the camera with the right stick.
- **A framing camera, not a leash.** Like Lakitu, the camera doesn't pin the player to the center of
- the screen. A player standing inside the resting zone (5% inside the rule-of-thirds lines) leaves
- the camera completely still. While running, they can drift out to the thirds lines. When they stop,
+ the screen. A player standing inside the resting zone leaves the camera completely still. The zone
+ sits 5% inside the rule-of-thirds lines, except that its top edge is at half the window height, which
+ keeps the view ahead open for upcoming obstacles. While running, they can drift 5% past the zone
+ (to the thirds lines at the sides and bottom, and just above halfway at the top). When they stop,
the camera eases until they sit on the zone's edge. Off-center shots are intentional: they're
where the cinematic feel comes from. Clicking the right stick glides the camera behind the player
with them centered, and the zone rules pick up again from there.
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
diff --git a/platformer/src/config.h b/platformer/src/config.h
index b615c4c..c71ae35 100644
--- a/platformer/src/config.h
+++ b/platformer/src/config.h
@@ -61,8 +61,10 @@
* CAMERA_PITCH_SPEED 1.5f rad/s at full right-stick tilt
* 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_ZONE_INSET 0.05f fraction of the window between the resting
+ * zone and the running bound, on every side
+ * CAMERA_ZONE_TOP 0.50f window fraction (0 = bottom) of the resting
+ * zone's top edge; lower = more view ahead
* 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
@@ -134,7 +136,8 @@
#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; 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_ZONE_INSET 0.05f /* window fraction between resting zone and running bound; higher = tighter zone */
+#define CAMERA_ZONE_TOP 0.50f /* resting zone's top edge (0 = bottom, 1 = top); lower = more view ahead of the player */
#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) */