summaryrefslogtreecommitdiff
path: root/platformer/src/camera.c
blob: 00f8d4aaeaa8e63df530739cbd85f317c9ec059f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * ============================================================
 * FILE:        camera.c
 *
 * PURPOSE:     A player-controlled third-person orbit camera, in the spirit of
 *              SM64's "Lakitu" camera but much simpler. A good camera matters as
 *              much for movement feel as the movement code itself.
 *
 * FITS IN:     Implements camera.h. Reads Input.look (controls.h) and the
 *              player's position (passed in by main.c). Produces matrices for
 *              render.c and a yaw for player.c.
 *
 * IMPLEMENT:
 *   - camera_init(c, follow_point):
 *       target   = follow_point + (0, CAMERA_TARGET_HEIGHT, 0)   (snap, no smoothing)
 *       yaw      = 0   (camera sits on +Z looking toward -Z)
 *       pitch    = CAMERA_PITCH_DEFAULT
 *       distance = CAMERA_DISTANCE
 *       compute position.
 *
 *   - camera_update(c, in, follow_point, dt):
 *       1. Orbit:  yaw   += in->look.x * CAMERA_YAW_SPEED   * dt
 *                  pitch += in->look.y * CAMERA_PITCH_SPEED * dt
 *          Clamp pitch to [CAMERA_PITCH_MIN, CAMERA_PITCH_MAX], wrap yaw with
 *          wrap_angle(). Check the sign by feel: pushing the right stick RIGHT
 *          should swing the camera so the view turns right (the camera moves
 *          left around the player). Flip the sign if it feels backwards and
 *          leave a comment.
 *       2. Follow: move target toward (follow_point + target height) with
 *          frame-rate-independent exponential smoothing:
 *            t = 1 - expf(-CAMERA_FOLLOW_RATE * dt)
 *            target = vec3_lerp(target, desired, t)
 *          Comment WHY: a camera that snaps rigidly to the player looks jittery,
 *          and a little lag lets you see the motion.
 *       3. Recompute position with the YAW CONVENTION in camera.h.
 *
 *   - camera_yaw(): return c->yaw.
 *   - camera_view(): mat4_look_at(position, target, (0,1,0)).
 *   - camera_projection(c, aspect): mat4_perspective(CAMERA_FOV_DEG converted to
 *     radians, aspect, CAMERA_NEAR, CAMERA_FAR).
 *
 * CONVENTIONS: Radians internally; FOV converted once from degrees.
 *
 * NOT IN SCOPE: Camera collision, auto-recenter, shake, look-ahead.
 *              (Good candidates for later tuning, not for now.)
 *
 * DONE WHEN:   The right stick orbits smoothly all the way around the player,
 *              pitch stops at its limits, and the camera trails the running
 *              player slightly without jitter.
 * ============================================================
 */