GizStackGizStack
View .mdOpen in ChatGPTOpen in Claude

gizgame/camera

The eye that follows a being.

gizlang
import * as Camera from "gizgame/camera";

Types

Follow

gizlang
type Follow = { back: f64; up: f64; ahead: f64; lift: f64; stiffness: f64; fov?: f64; far?: f64; side?: f64; }

How a Chase camera follows: BACK meters behind and UP above the target, looking AHEAD meters past it at LIFT above it, reaching where it should be at STIFFNESS (higher is tighter), through FOV radians. SIDE meters to the right, it looks over a shoulder: the target stands beside the middle and what it faces is open. With no BACK it is the target's own eye, LIFT above its feet (a first-person view).

  • back: f64
  • up: f64
  • ahead: f64
  • lift: f64
  • stiffness: f64
  • fov: _gizlang_sdk_core.Option<f64>
  • far: _gizlang_sdk_core.Option<f64>
  • side: _gizlang_sdk_core.Option<f64>

Chase

gizlang
class Chase { rig: Follow; floor: (x: f64, z: f64) => f64; private eye: Point3 = point3(0, 0, 0); private looking: Point3 = point3(0, 0, 0); private time: f64 = -1; private frame: f64 = -1; private seen: Point3 = point3(0, 0, 0); private aimed: Point3 = point3(0, 0, 0); private snap: bool = true; clear: ((from: Point3, to: Point3) => f64 throws string) | undefined = undefined; private room: f64 = 1; constructor(rig: Follow, floor: (x: f64, z: f64) => f64) { this.rig = rig; this.floor = floor; } private toward(eye: Point3, looking: Point3, time: f64, stiffness: f64, shake: f64, pivot: Point3 | undefined = undefined): Camera throws string { if (time !== this.frame) { this.snap = this.frame <0 || time <this.frame || time-this.frame> 0.5; this.time = this.frame; this.frame = time; this.eye = this.seen; this.looking = this.aimed; } const k = this.snap ? 1.0: Math.eased(Math.min(0.1, time-this.time), 1.0/stiffness); let seen = lerp(this.eye, eye, k); if (pivot === undefined || this.clear === undefined) { seen.y = Math.max(seen.y, this.floor(seen.x, seen.z)+1.2); } this.seen = seen; this.aimed = lerp(this.looking, looking, k); if (pivot !== undefined && this.clear !== undefined) { const check = this.clear!; const free = check(pivot!, seen); this.room = this.snap || free <this.room ? free: this.room+(free-this.room)*Math.eased(Math.min(0.1, time-this.time), 0.2); seen = lerp(pivot!, seen, this.room); } const jolt = shake*0.25; return { eye: point3(seen.x+Math.sin(time*53)*jolt, seen.y+Math.cos(time*47)*jolt, seen.z), target: this.aimed, fov: this.rig.fov ?? 1.05, near: 0.1, far: this.rig.far ?? 300 }; } follow(target: Point3, yaw: f64, time: f64, shake: f64 = 0.0, pitch: f64 = 0.0, zoom: f64 = 1.0, shoulder: f64 = 1.0): Camera throws string { const ahead = facing(yaw); const r = this.rig; const level = Math.cos(pitch); const rise = Math.sin(pitch); const far = Math.sqrt(r.back*r.back+(r.up-r.lift)*(r.up-r.lift))*zoom; const tilt = Math.atan2(r.up-r.lift, r.back)-pitch; const head = point3(target.x, target.y+r.lift, target.z); if (r.back <= 0) { return this.toward(add(head, scale(ahead, 0.12)), add(head, scale(facing(yaw, pitch), 4)), time, 60, shake); } const side = scale(facing(yaw-Math.PI*0.5), (r.side ?? 0)*shoulder*zoom); const back = far*Math.cos(tilt); const up = r.lift+far*Math.sin(tilt); let eye = point3(target.x-ahead.x*back+side.x, target.y+up, target.z-ahead.z*back+side.z); let aim = point3(target.x+ahead.x*r.ahead*level+side.x, target.y+r.lift+r.ahead*rise, target.z+ahead.z*r.ahead*level+side.z); const ground = this.floor(eye.x, eye.z)+0.5; if (eye.y <ground) { const dy = Math.min(ground-head.y, far*0.95); const flat = Math.sqrt(Math.max(0.0, far*far-dy*dy))/Math.max(1e-6, Math.sqrt(back*back)); const raised = point3(head.x+(eye.x-head.x)*flat, head.y+dy, head.z+(eye.z-head.z)*flat); aim = add(raised, sub(aim, eye)); eye = raised; } return this.toward(eye, aim, time, r.stiffness, shake, head); } orbit(center: Point3, radius: f64, height: f64, speed: f64, time: f64): Camera throws string { return this.toward(point3(center.x+Math.cos(time*speed)*radius, center.y+height, center.z+Math.sin(time*speed)*radius), center, time, 1, 0); } }

A camera that follows a target smoothly from frame to frame, and starts over where time jumps (a restore). FLOOR keeps the eye above the ground.

  • rig: M198.Follow
  • floor: (arg0: f64, arg1: f64) => f64
  • eye: gizlang_geometry.Point3
  • looking: gizlang_geometry.Point3
  • time: f64
  • frame: f64
  • seen: gizlang_geometry.Point3
  • aimed: gizlang_geometry.Point3
  • snap: bool
  • clear: _gizlang_sdk_core.Option<(arg0: gizlang_geometry.Point3, arg1: gizlang_geometry.Point3) => f64 throws string> — How much of the way from the target to the eye is free of the world (1: all): with it, the eye comes in before a wall at once and goes back out slowly, and never looks from behind one.
  • room: f64

Functions

constructor

gizlang
constructor(rig: Follow, floor: (x: f64, z: f64) => f64)

A camera of RIG over the ground FLOOR gives at X, Z.

Effects: fail, read, write, alloc.

Methods

Chase.follow

gizlang
follow(target: Point3, yaw: f64, time: f64, shake: f64 = 0.0, pitch: f64 = 0.0, zoom: f64 = 1.0, shoulder: f64 = 1.0): Camera throws string

The camera behind TARGET, which looks along YAW and up by PITCH radians (a mouse look's), at TIME; SHAKE jolts it; ZOOM moves it nearer or farther (Game.zoom). A look up swings the eye down round the target's head and raises its aim, as a third-person camera orbits: the eye stays as far from the head, so only ZOOM changes the distance.

Effects: fail, read, write, console, io, alloc.

Chase.orbit

gizlang
orbit(center: Point3, radius: f64, height: f64, speed: f64, time: f64): Camera throws string

The camera circling CENTER at RADIUS and HEIGHT, a turn in 2 pi / SPEED seconds, as a title shows a world.

Effects: fail, read, write, console, io, alloc.

Constants

DRIVING

gizlang
const DRIVING: Follow = { back: 8.5, up: 2.6, ahead: 6, lift: 1.0, stiffness: 5, far: 3000 }

The rig of the eye of one who drives a car: back past its tail, above its roof, looking down the road.