Skip to main content

OrbitCamera

Struct OrbitCamera 

Source
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

Source

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.

Source

pub const fn pivot(&self) -> Vec3

What the camera turns around and looks at.

Source

pub const fn distance(&self) -> f32

How far the eye is from the pivot, in metres.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for OrbitCamera

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for OrbitCamera

Source§

impl Debug for OrbitCamera

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for OrbitCamera

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for OrbitCamera

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HalThreadSafe for T
where T: Send + Sync,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.