pub struct OrbitCamera { /* private fields */ }Expand description
A camera that turns about a pivot: orbit, pan, zoom, frame.
Holds a pivot, a distance and two angles rather than a Camera, for the
same reason crate::fly::Flyer holds angles: an eye and a target
integrated directly drift apart, and “orbit” would depend on how far away the
target happened to be.
use crcbl_render::{Camera, OrbitCamera, Projection};
use glam::Vec3;
let mut orbit = OrbitCamera::new(Vec3::ZERO, 4.0, Projection::default());
// Zero yaw stands where the engine's default camera stands: back along +Z.
assert_eq!(orbit.camera().eye, Vec3::new(0.0, 0.0, 4.0));
// A quarter turn to the eye's right puts it on +X, still looking at the pivot.
orbit.orbit(core::f32::consts::FRAC_PI_2, 0.0);
let camera = orbit.camera();
assert!((camera.eye - Vec3::new(4.0, 0.0, 0.0)).length() < 1e-5);
assert_eq!(camera.target, Vec3::ZERO);Implementations§
Source§impl OrbitCamera
impl OrbitCamera
Sourcepub fn new(pivot: Vec3, distance: f32, projection: Projection) -> Self
pub fn new(pivot: Vec3, distance: f32, projection: Projection) -> Self
A camera distance metres from pivot, looking at it from +Z.
Zero yaw and zero pitch put the eye where Camera::default puts it —
back along +Z, looking down -Z — so an orbit camera nobody has
dragged yet frames the world the same way the rest of the engine does.
§Panics
If projection is Projection::Orthographic, which this controller
does not support (see the module docs), if distance is not
finite, or on the same field of view and near plane
Projection::matrix refuses — each of which otherwise turns into a
NaN matrix and a blank screen with no error anywhere.
Sourcepub fn orbit(&mut self, yaw: f32, pitch: f32)
pub fn orbit(&mut self, yaw: f32, pitch: f32)
Turns the eye about the pivot by yaw and pitch radians.
The pivot does not move and the distance does not change; this is the
operation that makes it a turntable rather than a look-around. Positive
yaw swings the eye toward the camera’s own right, positive pitch
lifts it — see the module docs — and the pitch is clamped to
PITCH_LIMIT short of vertical, where the view matrix is degenerate.
§Panics
If either delta is not finite. An angle that goes NaN here stays NaN
for the rest of the session, and the panic it eventually causes is in
Camera::view, several frames from the input that caused it.
Sourcepub fn pan(&mut self, right: f32, up: f32)
pub fn pan(&mut self, right: f32, up: f32)
Slides the pivot across the view plane, in fractions of the viewport height.
Positive right moves the pivot toward the camera’s own right and
positive up toward its own up, so the scene appears to move the other
way — an app implementing grab-and-drag negates both. The eye follows
rigidly: panning changes what the camera looks at, never how far away it
is or which way it faces.
One unit is the whole viewport height at the pivot’s depth, which is what makes a drag track the pointer: the point under the cursor when the drag began is still under it when the drag ends, as long as that point was near the pivot’s plane. Points nearer or further than the pivot move by more or less, which is parallax and is correct.
§Panics
If either delta is not finite, for the reason Self::orbit gives.
Sourcepub fn zoom(&mut self, delta: f32)
pub fn zoom(&mut self, delta: f32)
Moves the eye along its own view ray. Positive delta moves closer.
Multiplicative, and the field of view never changes. Each unit of
delta divides the distance by e, so a wheel notch covers the same
fraction of the way in whether the camera is a metre out or a kilometre —
which is the only behaviour that is usable across the range a model
viewer sees. Changing the field of view instead would be a dolly zoom:
the framing would match and the perspective would not.
The result is clamped to MIN_DISTANCE..=MAX_DISTANCE, so no
amount of zooming reaches the pivot, passes through it, or runs off to
infinity.
§Panics
If delta is not finite, for the reason Self::orbit gives.
Sourcepub fn frame(&mut self, bounds: Aabb, aspect: f32)
pub fn frame(&mut self, bounds: Aabb, aspect: f32)
Centres bounds and backs off far enough to see all of it, keeping the
direction the camera is already looking from.
The fit is on the bounding sphere and on both axes. A sphere because
a box’s silhouette changes as the camera orbits, so a box fit would frame
the object at one angle and clip it at the next; the sphere’s does not
change at all, at the cost of a little air around a flat object. Both
axes because the horizontal half-angle is
atan(tan(fov_y / 2) · aspect), which in a window narrower than it is
tall is smaller than the vertical one — so framing a wide object by
height alone leaves it hanging off both sides of a narrow window. The
limiting half-angle θ is whichever is smaller, and a sphere of radius
r fits inside it at r / sin θ, times FRAME_MARGIN.
The distance is then held to at least near + r, so the whole sphere is
in front of the near plane rather than half of it behind — which matters
for a small object, where the fit distance alone can be shorter than the
near plane.
§Panics
If aspect is not finite and positive, as Projection::matrix, or if
bounds is not finite.
Sourcepub fn camera(&self) -> Camera
pub fn camera(&self) -> Camera
The camera this controller is.
up is the world’s +Y rather than the view’s own up vector: the pitch
clamp is what keeps the two from becoming parallel, and a turntable whose
up drifted with the view would roll.
Sourcepub fn walk_direction(yaw: f64, ahead: f64, strafe: f64) -> DVec3
pub fn walk_direction(yaw: f64, ahead: f64, strafe: f64) -> DVec3
The world-space direction a stick means, at the yaw the view is at.
ahead is positive away from the camera and strafe positive to the
camera’s right; both are expected in -1..=1. The result is a unit
direction, or zero where nothing was asked for — the caller multiplies
by a speed and a timestep to get the displacement a character controller
takes.
An associated function rather than a method because a follow camera
rebuilds its OrbitCamera every frame from a pivot that has moved,
and holds only the two angles between frames: it has a yaw before it has
a rig. What makes this the rig’s arithmetic and not the caller’s is the
measure — the yaw is the one this module turns, so ahead is the
direction camera looks with the vertical taken out and
right is the cross product this camera’s own basis is built from.
It is f64 where the rest of this module is f32 because a character
controller integrates a position in metres over a whole session, which
is where single precision runs out first.
Normalised rather than passed through, so holding two keys does not walk
√2 times faster than holding one — which is the oldest bug in this
conversion.