Skip to main content

Camera2d

Struct Camera2d 

Source
pub struct Camera2d { /* private fields */ }
Expand description

Represents a camera (“view”) positioned and oriented in a 2D scene with a 2D transformation and a 2D projection

Implementations§

Source§

impl Camera2d

Source

pub fn new(viewport_width: u16, viewport_height: u16) -> Self

Create a new camera centered at the origin looking down the positive Y axis with ‘up’ vector aligned with the Z axis.

The orthographic projection is defined such that at the initial zoom level of 1.0, one unit in world space is one pixel, so the points in the bounding rectangle defined by minimum point (-half_screen_width+1, -half_screen_height+1) and maximum point (half_screen_width, half_screen_height) are visible.

Source

pub const fn yaw(&self) -> Rad<f32>

Source

pub const fn position(&self) -> Point2<f32>

Source

pub const fn orientation(&self) -> Rotation2<f32>

Source

pub const fn viewport_width(&self) -> u16

Source

pub const fn viewport_height(&self) -> u16

Source

pub const fn transform_mat_world_to_view(&self) -> Matrix4<f32>

Source

pub const fn zoom(&self) -> f32

Source

pub const fn ortho(&self) -> FrustumPlanes<f32>

Source

pub const fn projection_mat_ortho(&self) -> Matrix4<f32>

Source

pub fn screen_to_world(&self, screen_coord: Point2<f32>) -> Point2<f32>

Convert a screen space coordinate to world space based on the current view.

let [width, height] = [320, 240];
let mut camera2d = Camera2d::new (width, height);
let screen_coord = [width as f32 / 2.0, height as f32 / 2.0].into();
assert_eq!(camera2d.screen_to_world (screen_coord).0, [0.0, 0.0].into());
Source

pub fn world_to_screen(&self, world_coord: Point2<f32>) -> Point2<f32>

Convert a world space coordinate to screen space based on the current view.

let [width, height] = [320, 240];
let mut camera2d = Camera2d::new (width, height);
let world_coord  = math::Point::origin();
assert_eq!(camera2d.world_to_screen (world_coord).0,
  [width as f32 / 2.0, height as f32 / 2.0].into());
Source

pub fn set_viewport_dimensions( &mut self, viewport_width: u16, viewport_height: u16, )

Should be called when the screen resolution changes to update the orthographic projection state.

§Panics

Panics if the viewport width or height are zero:

let mut camera2d = Camera2d::new (320, 240);
camera2d.set_viewport_dimensions (0, 0); // panics
Source

pub fn set_position(&mut self, position: Point2<f32>)

Source

pub fn set_zoom(&mut self, zoom: f32)

Set the zoom level.

§Panics

Panics if scale factor is zero or negative:

let mut camera2d = Camera2d::new (320, 240);
camera2d.set_zoom (-1.0);   // panics
Source

pub fn rotate(&mut self, delta_yaw: Rad<f32>)

Source

pub fn move_local(&mut self, delta_x: f32, delta_y: f32)

Move by delta X and Y values in local coordinates

Source

pub fn move_origin_to_bottom_left(&mut self)

Move camera so that the world-space origin will be centered on the lower left pixel

Source

pub fn scale_zoom(&mut self, scale: f32)

Multiply the current zoom by the given scale factor.

§Panics

Panics if scale factor is zero or negative:

let mut camera2d = Camera2d::new (320, 240);
camera2d.scale_zoom (-1.0);   // panics
Source

pub fn view_ortho_mats(&self) -> ([[f32; 4]; 4], [[f32; 4]; 4])

Returns the raw world to view transform and ortho projection matrix data, suitable for use as shader uniforms.

Trait Implementations§

Source§

impl Clone for Camera2d

Source§

fn clone(&self) -> Camera2d

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 Debug for Camera2d

Source§

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

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

impl PartialEq for Camera2d

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Camera2d

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> MaybeSerDes for T