summaryrefslogtreecommitdiff
path: root/platformer/src/camera.c
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/src/camera.c
parentddbaf66281986ed50133b7eaa92c37ef9dbb5636 (diff)
downloadgamedev-077dfb2774e29cc49b9d602bb3a2f5e81e4a9a63.tar.gz
gamedev-077dfb2774e29cc49b9d602bb3a2f5e81e4a9a63.zip
Camera horizon reset
Diffstat (limited to 'platformer/src/camera.c')
-rw-r--r--platformer/src/camera.c60
1 files changed, 57 insertions, 3 deletions
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