[][src]Struct tetra::graphics::Camera

pub struct Camera {
    pub position: Vec2<f32>,
    pub rotation: f32,
    pub zoom: f32,
    pub viewport_width: f32,
    pub viewport_height: f32,
    // some fields omitted
}

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.

rotation: f32

The rotation of the camera, in radians.

zoom: f32

The zoom level of the camera.

viewport_width: f32

The width of the camera's viewport.

viewport_height: f32

The height of the camera's viewport.

Implementations

impl Camera[src]

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

Creates a new camera with the given viewport size.

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

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 your State's event method when Event::Resized is fired.

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

Sets the size of the camera's viewport.

pub fn update(&mut self)[src]

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

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

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.

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

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

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

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

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

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).

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

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).

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

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).

pub fn visible_rect(&self) -> Rectangle[src]

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

impl Clone for Camera[src]

impl Debug for Camera[src]

Auto Trait Implementations

impl RefUnwindSafe for Camera

impl Send for Camera

impl Sync for Camera

impl Unpin for Camera

impl UnwindSafe for Camera

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SetParameter for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.