summaryrefslogtreecommitdiff
path: root/platformer
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-24 14:31:10 -0400
committerSchark <jordan@schark.online>2026-09-24 14:31:10 -0400
commit077dfb2774e29cc49b9d602bb3a2f5e81e4a9a63 (patch)
treecd590ba45d7854f2eecedcdca0c682818334d7a5 /platformer
parentddbaf66281986ed50133b7eaa92c37ef9dbb5636 (diff)
downloadgamedev-077dfb2774e29cc49b9d602bb3a2f5e81e4a9a63.tar.gz
gamedev-077dfb2774e29cc49b9d602bb3a2f5e81e4a9a63.zip
Camera horizon reset
Diffstat (limited to 'platformer')
-rw-r--r--platformer/README.md3
-rw-r--r--platformer/src/camera.c60
-rw-r--r--platformer/src/camera.h7
-rw-r--r--platformer/src/config.h15
4 files changed, 79 insertions, 6 deletions
diff --git a/platformer/README.md b/platformer/README.md
index 26f6fc2..370d263 100644
--- a/platformer/README.md
+++ b/platformer/README.md
@@ -52,7 +52,8 @@ game until the controls feel great.
sideways is different: the camera works ahead of the player, carrying them to the *trailing* thirds
line so there's open screen in the direction they're heading, and slowly swings toward ~30° off
directly behind them. Controls stay camera-relative, so holding sideways through the swing makes the
- player arc, SM64-style. When they stop, the camera eases until they sit on the zone's edge.
+ player arc, SM64-style. When they stop, the camera eases until they sit on the zone's edge. After
+ 3 s without touching the camera, it also slowly eases into looking down at the player at ~25°.
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 3375585..4511d47 100644
--- a/platformer/src/camera.c
+++ b/platformer/src/camera.c
@@ -61,6 +61,18 @@
* sideways while it swings makes the player arc, like
* Mario circling in SM64. That's intentional.
*
+ * - Auto-pitch: once the camera has gone CAMERA_AUTO_PITCH_DELAY seconds
+ * without being moved by hand (right stick, or a recenter click), pitch
+ * slowly heads to CAMERA_AUTO_PITCH_DEG (looking down at the player). The
+ * move eases in AND out: its speed (auto_pitch_speed) starts at 0, grows
+ * by CAMERA_AUTO_PITCH_ACCEL, is capped at CAMERA_AUTO_PITCH_SPEED, and is
+ * also capped by the braking curve sqrt(2 * accel * remaining angle), the
+ * fastest speed that can still slow to a stop exactly on the target. It
+ * only touches pitch, so it runs at the same time as the back-quarter swing
+ * (yaw) instead of taking turns with it. Touching the camera restarts the
+ * countdown and stops the tilt at once, and a pitch you set by hand is
+ * respected until the countdown runs out again.
+ *
* - 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).
@@ -73,7 +85,8 @@
* 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.
+ * so the swing is slow and even). Then apply the auto-pitch (see
+ * above). Framing is kept through all of these.
* 3. Hold still (except while recentering): the camera does NOT follow
* 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
@@ -128,7 +141,9 @@
* 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.
+ * straight toward/away from the camera behave as before. After
+ * CAMERA_AUTO_PITCH_DELAY seconds hands-off, the pitch slowly tilts
+ * to CAMERA_AUTO_PITCH_DEG, alongside any swing in progress.
* ============================================================
*/
@@ -216,6 +231,41 @@ static void orbit(Camera *c, const Input *in, float dt)
c->yaw = wrap_angle(c->yaw);
}
+/* Auto-pitch: after CAMERA_AUTO_PITCH_DELAY seconds of the camera not being
+ * moved by hand, slowly tilt to the preferred downward viewing angle. The
+ * timer is capped at the delay so it can't grow forever. */
+static void auto_pitch(Camera *c, const Input *in, float dt)
+{
+ bool touched = in->look.x != 0.0f || in->look.y != 0.0f || in->recenter_pressed;
+ if (touched) {
+ c->hands_off_time = 0.0f;
+ c->auto_pitch_speed = 0.0f; /* the player took over: stop at once */
+ return;
+ }
+ c->hands_off_time = fminf(c->hands_off_time + dt, CAMERA_AUTO_PITCH_DELAY);
+ if (c->hands_off_time < CAMERA_AUTO_PITCH_DELAY)
+ return;
+
+ float goal = clampf(CAMERA_AUTO_PITCH_DEG * (M3D_PI / 180.0f),
+ CAMERA_PITCH_MIN, CAMERA_PITCH_MAX);
+ float remaining = fabsf(goal - c->pitch);
+
+ /* Ease in: speed up gradually. Ease out: never go faster than the speed
+ * we could still brake from and stop exactly at the goal. (From physics:
+ * stopping from speed v at deceleration a takes a distance of v^2 / 2a.) */
+ float brake_limit = sqrtf(2.0f * CAMERA_AUTO_PITCH_ACCEL * remaining);
+ c->auto_pitch_speed = fminf(c->auto_pitch_speed + CAMERA_AUTO_PITCH_ACCEL * dt,
+ fminf(CAMERA_AUTO_PITCH_SPEED, brake_limit));
+
+ float step = c->auto_pitch_speed * dt;
+ if (step >= remaining) {
+ c->pitch = goal; /* arrived: settle exactly, at rest */
+ c->auto_pitch_speed = 0.0f;
+ } else {
+ c->pitch += goal > c->pitch ? step : -step;
+ }
+}
+
/* A rectangle on screen, in NDC. */
typedef struct {
Vec2 min, max;
@@ -338,6 +388,8 @@ void camera_init(Camera *c, Vec3 follow_point)
c->subject = subject_point(follow_point);
c->recentering = false;
c->recenter_yaw = 0.0f;
+ c->hands_off_time = 0.0f; /* the countdown starts when the game does */
+ c->auto_pitch_speed = 0.0f;
/* The aspect ratio only matters for a non-zero framing, which is 0 here. */
place_camera(c, 1.0f);
}
@@ -358,7 +410,8 @@ 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. Otherwise a
- * sideways run slowly swings the camera toward the back quarter. */
+ * sideways run slowly swings the camera toward the back quarter. After
+ * a while hands-off, the pitch also tilts to its preferred angle. */
float yaw_before = c->yaw;
orbit(c, in, dt);
float lateral = 0.0f; /* sideways effects are off during a recenter glide */
@@ -370,6 +423,7 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f
} else {
swing_toward_back_quarter(c, moved_by, lateral, dt);
}
+ auto_pitch(c, in, dt);
Basis b = camera_basis(c->yaw, c->pitch);
/* 3. Hold still: with the eye fixed, the player's motion changes where
diff --git a/platformer/src/camera.h b/platformer/src/camera.h
index 84897d7..60134ed 100644
--- a/platformer/src/camera.h
+++ b/platformer/src/camera.h
@@ -27,6 +27,11 @@
* 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
+ * float hands_off_time; seconds since the camera was last moved by hand
+ * (right stick or recenter), capped at
+ * CAMERA_AUTO_PITCH_DELAY; drives the auto-pitch
+ * float auto_pitch_speed; current auto-pitch speed, rad/s (>= 0), so the
+ * tilt can ease in and out instead of moving linearly
* } Camera;
*
* void camera_init(Camera *c, Vec3 follow_point);
@@ -78,6 +83,8 @@ typedef struct Camera {
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 */
+ float hands_off_time; /* s since the camera was last moved by hand (capped) */
+ float auto_pitch_speed; /* current auto-pitch speed, rad/s (>= 0), for easing */
} Camera;
void camera_init(Camera *c, Vec3 follow_point);
diff --git a/platformer/src/config.h b/platformer/src/config.h
index ba2cdf1..807a6be 100644
--- a/platformer/src/config.h
+++ b/platformer/src/config.h
@@ -75,6 +75,12 @@
* 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_AUTO_PITCH_DELAY 3.0f s without touching the camera before it tilts
+ * to its preferred angle on its own
+ * CAMERA_AUTO_PITCH_DEG 25.0f degrees; the preferred look-down angle
+ * CAMERA_AUTO_PITCH_SPEED 0.06f rad/s; top speed of that tilt
+ * CAMERA_AUTO_PITCH_ACCEL 0.05f rad/s^2; how gently the tilt speeds up
+ * and slows down (ease in/out)
* CAMERA_FOV_DEG 60.0f vertical field of view
* CAMERA_NEAR 0.1f
* CAMERA_FAR 500.0f
@@ -84,8 +90,9 @@
* GRID_SPACING 1.0f m between grid lines
* GRID_MAJOR_EVERY 10 every Nth line drawn brighter
*
- * CONVENTIONS: 1 unit = 1 meter, seconds, radians (degrees only for the FOV,
- * converted where it's used). Include guard: CONFIG_H.
+ * CONVENTIONS: 1 unit = 1 meter, seconds, radians (degrees only for names
+ * ending in _DEG, which are easier to tune by eye; converted where
+ * they're used). Include guard: CONFIG_H.
*
* NOT IN SCOPE: Runtime config files, command-line parsing, hot-reload.
*
@@ -149,6 +156,10 @@
#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_AUTO_PITCH_DELAY 3.0f /* s hands-off before the camera tilts to CAMERA_AUTO_PITCH_DEG on its own */
+#define CAMERA_AUTO_PITCH_DEG 25.0f /* degrees above horizontal the camera prefers to look down at the player */
+#define CAMERA_AUTO_PITCH_SPEED 0.06f /* rad/s top tilt speed toward that angle; lower = slower, subtler tilt */
+#define CAMERA_AUTO_PITCH_ACCEL 0.05f /* rad/s^2 ease in/out of the tilt; lower = softer start and stop */
#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) */