JFLib

game/src/camera.rs#

Source: lib/game/src/camera.rs

camera -- camera controllers in f64: first person (yaw and pitch, the pitch held within a limit),
follow (an orbit around a target at a distance, pulled in front of what is in the way) and free fly

  View              eye, forward, up (unit vectors at right angles): what a renderer needs besides the
                    lens (the bridge makes a render3d::Camera of it)
  FirstPerson       eye, yaw, pitch; look(dx, dy) from Motion actions, walk_dir for moving on the
                    ground, view()
  Follow            target, yaw, pitch, distance (min..max); look, zoom, eye, view, view_in (a sphere
                    cast through a physics World from the target to the eye)
  FreeFly           eye, yaw, pitch, speed; look, fly(forward, right, up, dt), view
  basis(yaw, pitch) (forward, right, up) of a turn; wrap_angle into (-pi, pi]

Conventions as lib/physics (Quat::from_yaw_pitch): right-handed, Y up; yaw 0 and pitch 0 look along -Z;
yaw turns about +Y (positive: to the left), pitch about the turned +X (positive: up).  look(dx, dy)
takes what the mouse did, right and down positive (the values of Motion actions), and turns right and
down: yaw -= dx, pitch -= dy.  sin and cos are physics::math's (rt's portable ones): the same bits on
every target, as the server's simulation needs.
/// the pitch limit unless set: 89 degrees (straight up or down would leave the yaw undefined)
PITCH_LIMIT: f64 = 89.0 * core::f64::consts::PI / 180.0
  • struct View what a camera sees from: the eye, the unit vectors forward and up (at right angles)
    • eye: Vec3 -- the eye's position (metres, f64)
    • forward: Vec3 -- where it looks
    • up: Vec3 -- up on the screen

impl View

  • fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Option<View> from eye to target, up saying which way is up (None: target at the eye, or up along the line)

  • fn right(&self) -> Vec3 right on the screen (forward x up)

  • fn wrap_angle(a: f64) -> f64 an angle in (-pi, pi]

  • fn basis(yaw: f64, pitch: f64) -> (Vec3, Vec3, Vec3) (forward, right, up) of a camera turned by yaw and pitch (see the module header): the columns of Quat::from_yaw_pitch(yaw, pitch) applied to -Z, +X and +Y

  • fn yaw_pitch_of(dir: Vec3) -> Option<(f64, f64)> yaw and pitch of a direction (the inverse of basis' forward; None for a zero vector)

  • struct FirstPerson a first-person camera: at the eye, turned by yaw and pitch

    • eye: Vec3 -- the eye (a figure's position plus its eye height)
    • yaw: f64 -- the turn about +Y (radians, positive to the left), kept in (-pi, pi]
    • pitch: f64 -- the turn up and down (radians, positive up), kept within pitch_limit
    • pitch_limit: f64 -- the largest pitch up and down (PITCH_LIMIT unless set)

impl FirstPerson

  • fn new(eye: Vec3) -> FirstPerson at eye, looking along -Z

  • fn look(&mut self, dx: f64, dy: f64) turn by what the mouse did (radians, right and down positive: the values of Motion actions)

  • fn forward(&self) -> Vec3 where it looks

  • fn rotation(&self) -> Quat the rotation of the camera (-Z forward, +Y up)

  • fn walk_dir(&self, forward: f64, right: f64) -> Vec3 a direction on the ground (y 0) from two Axis values: forward along the yaw, right across it; its length at most 1 (diagonal walking is not faster)

  • fn view(&self) -> View the view from the eye

  • fn walk_dir(yaw: f64, forward: f64, right: f64) -> Vec3 (FirstPerson::walk_dir) a direction on the ground from the yaw and two Axis values, length at most 1

  • struct Follow a third-person camera: an orbit around a target, at a distance

    • target: Vec3 -- what it looks at (a figure's head)
    • yaw: f64 -- the turn about +Y (radians), kept in (-pi, pi]
    • pitch: f64 -- the turn up and down (positive: looking up, so the eye sinks), kept in pitch_min ..= pitch_max
    • distance: f64 -- the eye's distance from the target, kept in min_distance ..= max_distance
    • min_distance: f64 -- the nearest the eye comes
    • max_distance: f64 -- the farthest
    • pitch_min: f64 -- the lowest pitch (looking down most: the eye above the target)
    • pitch_max: f64 -- the highest pitch

impl Follow

  • fn new(target: Vec3, distance: f64) -> Follow around target at distance (kept in 0.5 ..= 50), slightly from above (pitch -0.3)

  • fn look(&mut self, dx: f64, dy: f64) turn around the target by what the mouse did (radians, right and down positive)

  • fn zoom(&mut self, notches: f64) nearer (notches > 0, the wheel up) or farther: 10 % of the distance a notch

  • fn eye(&self) -> Vec3 the eye: distance behind the target along the view

  • fn view(&self) -> View the view from the eye to the target

  • fn view_in(&self, world: &PhysicsWorld, filter: &Filter, radius: f64) -> (View, f64) the view with the eye pulled in front of what is in the way: a sphere of radius cast from the target towards the eye through world (the figure's own body left out by filter) -> the view and the distance used (at least min_distance, nearer still when the target itself sits in something)

  • struct FreeFly a free camera: flies where it looks

    • eye: Vec3 -- the eye
    • yaw: f64 -- the turn about +Y (radians), kept in (-pi, pi]
    • pitch: f64 -- the turn up and down, kept within pitch_limit
    • pitch_limit: f64 -- the largest pitch (PITCH_LIMIT unless set)
    • speed: f64 -- metres a second at full input

impl FreeFly

  • fn new(eye: Vec3) -> FreeFly at eye, looking along -Z, 10 m/s
  • fn look(&mut self, dx: f64, dy: f64) turn by what the mouse did (radians, right and down positive)
  • fn fly(&mut self, forward: f64, right: f64, up: f64, dt: f64) fly for dt seconds: forward along the view (pitch included), right across it, up along +Y; each input in -1 ..= 1, the three together at most speed
  • fn view(&self) -> View the view from the eye

Source: docs/API.md in the JFLib repository.