Struct tetra::graphics::Camera

source ·
pub struct Camera {
    pub position: Vec2<f32>,
    pub rotation: f32,
    pub scale: Vec2<f32>,
    pub viewport_width: f32,
    pub viewport_height: f32,
    /* private fields */
}
Expand description

A camera that can be used to transform the player’s view of the scene.

To apply the transformation, call the as_matrix method and pass the resulting Mat4 to graphics::set_transform_matrix. To disable it, call graphics::reset_transform_matrix.

The camera’s matrix is cached internally as an optimization. After adjusting parameters on the camera, you can call the update method to recalculate the matrix.

Examples

The camera example demonstrates how a camera can be used to transform a simple scene.

Fields§

§position: Vec2<f32>

The position of the camera.

Note that this defines the center point of the view, rather than the top-left. This makes it easy to position the camera relative to your game objects - for example, to focus the camera on the player, you can just set the camera position to match the player’s position.

You may need to take this behaviour into account when positioning the camera, however. For example, if the viewport width or height is an odd number, setting the position to a whole number will mean that the view will not be aligned with the pixel grid, which may cause issues for pixel-perfect rendering.

§rotation: f32

The rotation of the camera, in radians.

§scale: Vec2<f32>

The scaling applied by the camera.

§viewport_width: f32

The width of the camera’s viewport.

This is primarily used for calculating where the center of the screen is, and usually should match the size of the target you’re currently rendering to (e.g. the screen, or a Canvas).

§viewport_height: f32

The height of the camera’s viewport.

This is primarily used for calculating where the center of the screen is, and usually should match the size of the target you’re currently rendering to (e.g. the screen, or a Canvas).

Implementations§

source§

impl Camera

source

pub fn new(viewport_width: f32, viewport_height: f32) -> Camera

Creates a new camera with the given viewport size.

The provided size usually should match the size of the target you’re currently rendering to (e.g. the screen, or a Canvas).

source

pub fn with_window_size(ctx: &Context) -> Camera

Creates a new camera, with the viewport size set to match the size of the window.

This is a useful shortcut if your game renders at a 1:1 ratio with the game window. If you’re rendering to a differently sized target (e.g. a Canvas or a ScreenScaler), then you should use call new with the target size instead.

Note that if the window is resized, the camera’s viewport size will not automatically update. If you need to keep the window size and the viewport size in sync, then call set_viewport_size in State::event when Event::Resized is fired.

source

pub fn set_viewport_size(&mut self, width: f32, height: f32)

Sets the size of the camera’s viewport.

The provided size usually should match the size of the target you’re currently rendering to (e.g. the screen, or a Canvas).

source

pub fn update(&mut self)

Recalculates the transformation matrix, based on the data currently contained within the camera.

source

pub fn as_matrix(&self) -> Mat4<f32>

Returns the current transformation matrix.

Pass this to graphics::set_transform_matrix to apply the transformation to your scene. To disable the transformation, call graphics::reset_transform_matrix.

The matrix is cached internally, so calling this method multiple times will not cause it to be recalculated from scratch.

source

pub fn project(&self, point: Vec2<f32>) -> Vec2<f32>

Projects a point from world co-ordinates to camera co-ordinates.

source

pub fn unproject(&self, point: Vec2<f32>) -> Vec2<f32>

Projects a point from camera co-ordinates to world co-ordinates.

source

pub fn mouse_position(&self, ctx: &Context) -> Vec2<f32>

Returns the mouse’s position in camera co-ordinates.

This is a shortcut for calling project(input::get_mouse_position(ctx)). As such, it does not take into account any other transformations being made to the view (e.g. screen scaling).

source

pub fn mouse_x(&self, ctx: &Context) -> f32

Returns the X co-ordinate of the mouse’s position in camera co-ordinates.

This is a shortcut for calling project(input::get_mouse_position(ctx)).x. As such, it does not take into account any other transformations being made to the view (e.g. screen scaling).

source

pub fn mouse_y(&self, ctx: &Context) -> f32

Returns the Y co-ordinate of the mouse’s position in camera co-ordinates.

This is a shortcut for calling project(input::get_mouse_position(ctx)).y. As such, it does not take into account any other transformations being made to the view (e.g. screen scaling).

source

pub fn visible_rect(&self) -> Rectangle

Calculates the visible rectangle of the camera.

When used on a rotated camera, this will return the smallest rectangle that contains the full camera viewport.

Note that this method does not take into account any other transformations being made to the view (e.g. screen scaling).

Trait Implementations§

source§

impl Clone for Camera

source§

fn clone(&self) -> Camera

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Camera

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

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

const: unstable · 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for Twhere T: Clone,

§

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
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,