From 8af1210d45c3fa3f41a174d2906ab597dcc22f1c Mon Sep 17 00:00:00 2001 From: Schark Date: Tue, 29 Sep 2026 23:36:45 -0400 Subject: Final state of the game day 3 --- platformer/src/camera.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) (limited to 'platformer/src/camera.c') diff --git a/platformer/src/camera.c b/platformer/src/camera.c index 7663003..b11f6bb 100644 --- a/platformer/src/camera.c +++ b/platformer/src/camera.c @@ -130,7 +130,18 @@ * Framing is in NDC so it matches what you see on screen, and it * converts to meters via depth * tan(fov/2) (times aspect for x). * - * NOT IN SCOPE: Camera collision, snapping directly behind the player on its + * - Step 7, objects (keep_eye_clear): the eye never sits INSIDE an object + * (world_object_at; zones never count). Objects BETWEEN the player and the + * eye are fine: a platform in the shot stays in the shot. Only when the + * spot the eye wants is inside an object does the eye jump THROUGH it, the + * same tick, to the object's near side, along its line to the player (so + * the player stays on the same screen spot and the framing model is + * untouched). When the wanted spot is free again, ease the eye back out: + * speed up at CAMERA_RETURN_ACCEL, cruise at CAMERA_RETURN_SPEED, brake to + * arrive exactly (like the auto-tilt). If that path would take it into an + * object, it jumps through to the far side instead of stopping inside. + * + * NOT IN SCOPE: Snapping directly behind the player on its * own (only the recenter button does that), shake, vertical * look-ahead. (Good candidates for later tuning, not for now.) * @@ -151,6 +162,7 @@ #include "camera.h" #include "config.h" +#include "world.h" #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" */ @@ -236,6 +248,90 @@ static void orbit(Camera *c, const Input *in, float dt) /* 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. */ +/* Where the line from `from` along `dir` enters and leaves box b grown by + * `radius` (the slab test: along each axis the line is inside the box's + * range between two distances; it's inside the box where all three ranges + * overlap). */ +static void line_through_box(Vec3 from, Vec3 dir, Box b, float radius, float *enter, float *leave) +{ + float lo[3] = { b.min.x - radius, b.min.y - radius, b.min.z - radius }; + float hi[3] = { b.max.x + radius, b.max.y + radius, b.max.z + radius }; + float o[3] = { from.x, from.y, from.z }; + float d[3] = { dir.x, dir.y, dir.z }; + *enter = -1e9f; + *leave = 1e9f; + for (int axis = 0; axis < 3; axis++) { + if (fabsf(d[axis]) < 1e-6f) + continue; /* parallel: the point is inside this slab (it was found inside the box) */ + float t0 = (lo[axis] - o[axis]) / d[axis]; + float t1 = (hi[axis] - o[axis]) / d[axis]; + *enter = fmaxf(*enter, fminf(t0, t1)); + *leave = fminf(*leave, fmaxf(t0, t1)); + } +} + +/* Moves a distance along the eye line out of any object it's inside: toward + * the player (`toward_player`), to that object's near side, or away from + * them, to its far side. A few passes, since objects can touch or overlap. */ +static float step_out_of_objects(Vec3 subject, Vec3 dir, float distance, bool toward_player) +{ + for (int pass = 0; pass < 4; pass++) { + Box hit; + if (!world_object_at(vec3_add(subject, vec3_scale(dir, distance)), CAMERA_COLLISION_RADIUS, &hit)) + break; + float enter, leave; + line_through_box(subject, dir, hit, CAMERA_COLLISION_RADIUS, &enter, &leave); + distance = toward_player ? fmaxf(enter, 0.0f) : leave; + } + return distance; +} + +/* The eye never sits inside an object. An object may be between the player + * and the eye (a platform in the shot is fine); only when the eye itself + * would end up inside one does it jump THROUGH it, the same tick, to the + * object's near side (the player's side), along its line to the player, so + * the player stays on the same screen spot and the framing is untouched. + * When the wanted spot is free again, the eye eases back out (speed up, + * cruise, brake, like the auto-tilt) instead of popping; if that path takes + * it into an object, it jumps through to the far side rather than stopping + * inside. Zones never count. */ +static void keep_eye_clear(Camera *c, float dt) +{ + Vec3 to_eye = vec3_sub(c->position, c->subject); + float wanted = vec3_length(to_eye); + if (wanted < 1e-4f) + return; + Vec3 dir = vec3_scale(to_eye, 1.0f / wanted); + float allowed = step_out_of_objects(c->subject, dir, wanted, true); + bool blocked = allowed < wanted - 1e-4f; + + if (allowed <= c->eye_distance || !c->pulled_in) { + /* The wanted spot is inside an object nearer than we are (jump in + * now), or we're not recovering: simply sit where allowed. */ + c->eye_distance = allowed; + c->return_speed = 0.0f; + c->pulled_in = blocked; + } else { + /* Free further out again: ease back toward it. */ + float remaining = allowed - c->eye_distance; + float brake_limit = sqrtf(2.0f * CAMERA_RETURN_ACCEL * remaining); + c->return_speed = fminf(c->return_speed + CAMERA_RETURN_ACCEL * dt, + fminf(CAMERA_RETURN_SPEED, brake_limit)); + float next = c->eye_distance + c->return_speed * dt; + next = fminf(step_out_of_objects(c->subject, dir, next, false), allowed); + if (next >= allowed) { + next = allowed; + c->return_speed = 0.0f; + c->pulled_in = blocked; + } + c->eye_distance = next; + } + + Vec3 look = vec3_sub(c->target, c->position); /* keep looking the same way */ + c->position = vec3_add(c->subject, vec3_scale(dir, c->eye_distance)); + c->target = vec3_add(c->position, look); +} + 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; @@ -394,6 +490,10 @@ void camera_init(Camera *c, Vec3 follow_point) 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); + c->eye_distance = vec3_length(vec3_sub(c->position, c->subject)); + c->pulled_in = false; + c->return_speed = 0.0f; + keep_eye_clear(c, 0.0f); } void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_facing, @@ -460,8 +560,9 @@ void camera_update(Camera *c, const Input *in, Vec3 follow_point, float follow_f if (c->framing.y > max_y) c->framing.y = max_y; - /* 6. Derive the eye from the framing. */ + /* 6. Derive the eye from the framing, then keep it out of objects. */ place_camera(c, aspect); + keep_eye_clear(c, dt); } float camera_yaw(const Camera *c) -- cgit v1.2.3-18-g5258