Skip to main content

Camera

Struct Camera 

Source
pub struct Camera {
    pub position: Vec2,
    pub target: Option<Vec2>,
    pub bounds: Option<Rect>,
    pub smooth_factor: f32,
    pub zoom: f32,
    pub viewport_size: Vec2,
}
Expand description

Camera controlling which portion of the world is visible on screen.

The camera’s position represents the top-left corner of the viewport in world-space pixels. All coordinate conversions account for zoom.

Fields§

§position: Vec2

Current camera position in world pixels (top-left corner of the viewport).

§target: Option<Vec2>

Desired position for smooth follow (world pixels). None means no follow target.

§bounds: Option<Rect>

World boundaries that clamp the camera. None means unbounded.

§smooth_factor: f32

Smooth-follow factor: 0.0 = instant snap, 1.0 = no movement at all.

§zoom: f32

Zoom level: 1.0 = normal, > 1.0 = zoomed in, < 1.0 = zoomed out.

§viewport_size: Vec2

Size of the viewport / screen in pixels.

Implementations§

Source§

impl Camera

Source

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

Create a new camera with default settings.

Position starts at (0, 0), zoom is 1.0, and smooth follow is disabled (smooth_factor = 0.0 — instant snap).

Source

pub fn follow_target(&mut self, target: Vec2)

Set (or replace) the target world position for smooth follow.

Call update every frame to interpolate position toward this target.

Source

pub fn stop_follow(&mut self)

Stop following any target. The camera stays at its current position.

Source

pub fn update(&mut self, dt: f32)

Advance the camera by dt seconds.

If a target is set, position is interpolated toward it using exponential decay that is frame-rate-independent. After interpolation, position is clamped to bounds (if set).

When smooth_factor is 0.0 the position snaps to the target instantly.

§Panics

Panics if dt is negative.

Source

pub fn world_to_screen(&self, world_pos: Vec2) -> Vec2

Convert a world-space position to a screen-space position, accounting for the camera’s current position and zoom.

let cam = Camera::new(160.0, 144.0);
let screen = cam.world_to_screen(Vec2::new(80.0, 72.0));
assert_eq!(screen.x, 80.0);
assert_eq!(screen.y, 72.0);
Source

pub fn screen_to_world(&self, screen_pos: Vec2) -> Vec2

Convert a screen-space position to a world-space position.

This is the inverse of world_to_screen.

let cam = Camera::new(160.0, 144.0);
let world = cam.screen_to_world(Vec2::new(160.0, 144.0));
assert_eq!(world.x, 160.0);
assert_eq!(world.y, 144.0);
Source

pub fn clamp_to_bounds(&mut self, bounds: Rect)

Set the world boundary rectangle and immediately clamp the camera to it.

The camera will never show content outside these bounds. The visible area (viewport_size / zoom) must fit within the bounds; if the viewport is larger than the bounds, the camera is centered on the bounds area.

Source

pub fn clear_bounds(&mut self)

Remove world boundaries. The camera can scroll anywhere.

Source

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

Set the zoom level, clamped to the valid range [0.1, 10.0].

Source

pub fn tile_x(&self) -> i32

Which tile column is at the camera’s left edge.

Divides the camera’s world x-position by TILE_SIZE (8 px) and floors.

Source

pub fn tile_y(&self) -> i32

Which tile row is at the camera’s top edge.

Divides the camera’s world y-position by TILE_SIZE (8 px) and floors.

Source

pub fn is_visible(&self, world_pos: Vec2) -> bool

Returns true if a world-space point is visible on screen (within the viewport), accounting for the camera position and zoom.

Source

pub fn is_rect_visible(&self, rect: Rect) -> bool

Returns true if any part of a world-space axis-aligned rectangle is visible on screen.

Trait Implementations§

Source§

impl Clone for Camera

Source§

fn clone(&self) -> Camera

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 Camera

Source§

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

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

impl PartialEq for Camera

Source§

fn eq(&self, other: &Camera) -> 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 Camera

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, 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 = 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.