/* * ============================================================ * 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. * ============================================================ */ #include "camera.h" #include "config.h" /* The point the camera wants to look at: a bit above the player's feet. */ static Vec3 desired_target(Vec3 follow_point) { return vec3_add(follow_point, vec3_make(0.0f, CAMERA_TARGET_HEIGHT, 0.0f)); } /* The YAW CONVENTION from camera.h. player.c depends on this exact formula. */ static void recompute_position(Camera *c) { Vec3 offset = vec3_make(sinf(c->yaw) * cosf(c->pitch), sinf(c->pitch), cosf(c->yaw) * cosf(c->pitch)); c->position = vec3_add(c->target, vec3_scale(offset, c->distance)); } void camera_init(Camera *c, Vec3 follow_point) { c->target = desired_target(follow_point); /* snap, no smoothing */ c->yaw = 0.0f; /* on +Z, looking toward -Z */ c->pitch = CAMERA_PITCH_DEFAULT; c->distance = CAMERA_DISTANCE; recompute_position(c); } void camera_update(Camera *c, const Input *in, Vec3 follow_point, float dt) { /* 1. Orbit. The yaw sign is flipped (-=): pushing the stick RIGHT should * turn the VIEW right, which means the camera swings LEFT around the * player. The camera's forward is (-sin(yaw), 0, -cos(yaw)), so turning it * toward the camera's right (+X at yaw 0) needs yaw to DECREASE. */ c->yaw -= in->look.x * CAMERA_YAW_SPEED * dt; c->pitch += in->look.y * CAMERA_PITCH_SPEED * dt; c->pitch = clampf(c->pitch, CAMERA_PITCH_MIN, CAMERA_PITCH_MAX); c->yaw = wrap_angle(c->yaw); /* 2. Follow with exponential smoothing. A camera that snaps rigidly to the * player looks jittery, and a little lag lets you see the motion. Using * 1 - e^(-rate*dt) instead of a fixed lerp factor keeps the catch-up speed * the same regardless of the tick rate. */ float t = 1.0f - expf(-CAMERA_FOLLOW_RATE * dt); c->target = vec3_lerp(c->target, desired_target(follow_point), t); /* 3. Place the eye on the orbit sphere around the new target. */ recompute_position(c); } float camera_yaw(const Camera *c) { return c->yaw; } Mat4 camera_view(const Camera *c) { return mat4_look_at(c->position, c->target, vec3_make(0.0f, 1.0f, 0.0f)); } Mat4 camera_projection(const Camera *c, float aspect) { (void)c; /* every camera shares the same lens for now */ float fovy_rad = CAMERA_FOV_DEG * (M3D_PI / 180.0f); return mat4_perspective(fovy_rad, aspect, CAMERA_NEAR, CAMERA_FAR); }