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 Viewwhat 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 looksup: Vec3-- up on the screen
impl View
fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Option<View>from eye to target,upsaying which way is up (None: target at the eye, or up along the line)fn right(&self) -> Vec3right on the screen (forward x up)fn wrap_angle(a: f64) -> f64an 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 +Yfn 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 FirstPersona first-person camera: at the eye, turned by yaw and pitcheye: 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_limitpitch_limit: f64-- the largest pitch up and down (PITCH_LIMIT unless set)
impl FirstPerson
fn new(eye: Vec3) -> FirstPersonat eye, looking along -Zfn 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) -> Vec3where it looksfn rotation(&self) -> Quatthe rotation of the camera (-Z forward, +Y up)fn walk_dir(&self, forward: f64, right: f64) -> Vec3a 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) -> Viewthe view from the eyefn 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 1struct Followa third-person camera: an orbit around a target, at a distancetarget: 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_maxdistance: f64-- the eye's distance from the target, kept in min_distance ..= max_distancemin_distance: f64-- the nearest the eye comesmax_distance: f64-- the farthestpitch_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) -> Followaround 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 notchfn eye(&self) -> Vec3the eye: distance behind the target along the viewfn view(&self) -> Viewthe view from the eye to the targetfn 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 ofradiuscast from the target towards the eye throughworld(the figure's own body left out byfilter) -> the view and the distance used (at least min_distance, nearer still when the target itself sits in something)struct FreeFlya free camera: flies where it lookseye: Vec3-- the eyeyaw: f64-- the turn about +Y (radians), kept in (-pi, pi]pitch: f64-- the turn up and down, kept within pitch_limitpitch_limit: f64-- the largest pitch (PITCH_LIMIT unless set)speed: f64-- metres a second at full input
impl FreeFly
fn new(eye: Vec3) -> FreeFlyat eye, looking along -Z, 10 m/sfn 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 speedfn view(&self) -> Viewthe view from the eye