Skip to main content

Camera

Struct Camera 

Source
pub struct Camera {
    pub position: Vec2,
    pub rotation: f32,
    pub zoom: f32,
    pub min_zoom: f32,
    pub max_zoom: f32,
    /* private fields */
}
Expand description

A 2D camera that controls the view transform.

By default, the camera is at the center of the screen looking “down” the Y axis (screen-space: +Y = down).

Fields§

§position: Vec2

World-space position (the “eye” point — center of the screen).

§rotation: f32

Rotation in radians.

§zoom: f32

Zoom level (1.0 = default, >1 = zoom in, <1 = zoom out).

§min_zoom: f32

Minimum zoom level.

§max_zoom: f32

Maximum zoom level.

Implementations§

Source§

impl Camera

Source

pub fn new(x: f32, y: f32) -> Self

Create a new camera at the given position.

Source

pub fn centered() -> Self

Create a camera centered at the origin.

Source

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

Set the zoom level (clamped to min/max).

Examples found in repository?
examples/basics.rs (line 99)
44    fn update(&mut self, ctx: &mut Context) {
45        let dt = ctx.time.delta() as f32;
46
47        // --- Ball physics ---
48        self.ball.velocity.y += 600.0 * dt; // Gravity
49        self.ball.position += self.ball.velocity * dt;
50
51        // Bounce off walls
52        let w = ctx.screen_width();
53        let h = ctx.screen_height();
54
55        if self.ball.position.x - self.ball.radius < 0.0 {
56            self.ball.position.x = self.ball.radius;
57            self.ball.velocity.x = self.ball.velocity.x.abs();
58        }
59        if self.ball.position.x + self.ball.radius > w {
60            self.ball.position.x = w - self.ball.radius;
61            self.ball.velocity.x = -self.ball.velocity.x.abs();
62        }
63        if self.ball.position.y - self.ball.radius < 0.0 {
64            self.ball.position.y = self.ball.radius;
65            self.ball.velocity.y = self.ball.velocity.y.abs();
66        }
67        if self.ball.position.y + self.ball.radius > h {
68            self.ball.position.y = h - self.ball.radius;
69            self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70        }
71
72        // Update trail
73        self.ball.trail.push((self.ball.position, 1.0));
74        if self.ball.trail.len() > 60 {
75            self.ball.trail.remove(0);
76        }
77        for (_, alpha) in &mut self.ball.trail {
78            *alpha -= dt * 2.0;
79        }
80        self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82        // --- Input ---
83        if ctx.input.is_action_pressed("shake") {
84            ctx.graphics.camera.shake(10.0, 0.3);
85        }
86
87        if ctx.input.mouse.is_pressed(MouseButton::Left) {
88            self.click_count += 1;
89            // Change ball color
90            self.hue = (self.hue + 30.0) % 360.0;
91            self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92            // Boost ball toward click
93            let dir = ctx.input.mouse.position - self.ball.position;
94            self.ball.velocity += dir.normalize_or_zero() * 200.0;
95        }
96
97        // Zoom with scroll
98        let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99        ctx.graphics.camera.set_zoom(zoom);
100
101        // Escape to quit
102        if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103            ctx.quit();
104        }
105
106        // Update camera follow target
107        ctx.graphics.camera.follow(self.ball.position, 0.05);
108    }
Source

pub fn set_zoom_limits(&mut self, min: f32, max: f32)

Set zoom limits.

Source

pub fn follow(&mut self, target: Vec2, lerp_speed: f32)

Make the camera follow a target position with smooth lerp.

Examples found in repository?
examples/basics.rs (line 41)
34    fn on_enter(&mut self, ctx: &mut Context) {
35        // Register input actions
36        let mut jump = InputAction::new("shake");
37        jump.bind_key(KeyCode::Space);
38        ctx.input.register_action(jump);
39
40        // Set up camera with follow
41        ctx.graphics.camera.follow(self.ball.position, 0.05);
42    }
43
44    fn update(&mut self, ctx: &mut Context) {
45        let dt = ctx.time.delta() as f32;
46
47        // --- Ball physics ---
48        self.ball.velocity.y += 600.0 * dt; // Gravity
49        self.ball.position += self.ball.velocity * dt;
50
51        // Bounce off walls
52        let w = ctx.screen_width();
53        let h = ctx.screen_height();
54
55        if self.ball.position.x - self.ball.radius < 0.0 {
56            self.ball.position.x = self.ball.radius;
57            self.ball.velocity.x = self.ball.velocity.x.abs();
58        }
59        if self.ball.position.x + self.ball.radius > w {
60            self.ball.position.x = w - self.ball.radius;
61            self.ball.velocity.x = -self.ball.velocity.x.abs();
62        }
63        if self.ball.position.y - self.ball.radius < 0.0 {
64            self.ball.position.y = self.ball.radius;
65            self.ball.velocity.y = self.ball.velocity.y.abs();
66        }
67        if self.ball.position.y + self.ball.radius > h {
68            self.ball.position.y = h - self.ball.radius;
69            self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70        }
71
72        // Update trail
73        self.ball.trail.push((self.ball.position, 1.0));
74        if self.ball.trail.len() > 60 {
75            self.ball.trail.remove(0);
76        }
77        for (_, alpha) in &mut self.ball.trail {
78            *alpha -= dt * 2.0;
79        }
80        self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82        // --- Input ---
83        if ctx.input.is_action_pressed("shake") {
84            ctx.graphics.camera.shake(10.0, 0.3);
85        }
86
87        if ctx.input.mouse.is_pressed(MouseButton::Left) {
88            self.click_count += 1;
89            // Change ball color
90            self.hue = (self.hue + 30.0) % 360.0;
91            self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92            // Boost ball toward click
93            let dir = ctx.input.mouse.position - self.ball.position;
94            self.ball.velocity += dir.normalize_or_zero() * 200.0;
95        }
96
97        // Zoom with scroll
98        let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99        ctx.graphics.camera.set_zoom(zoom);
100
101        // Escape to quit
102        if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103            ctx.quit();
104        }
105
106        // Update camera follow target
107        ctx.graphics.camera.follow(self.ball.position, 0.05);
108    }
Source

pub fn set_follow_deadzone(&mut self, deadzone: f32)

Set the deadzone for follow (camera won’t move while target is within this distance).

Source

pub fn stop_following(&mut self)

Stop following.

Source

pub fn shake(&mut self, intensity: f32, duration: f32)

Trigger screen shake.

  • intensity — maximum pixel offset in each direction
  • duration — how long the shake lasts (seconds)
Examples found in repository?
examples/basics.rs (line 84)
44    fn update(&mut self, ctx: &mut Context) {
45        let dt = ctx.time.delta() as f32;
46
47        // --- Ball physics ---
48        self.ball.velocity.y += 600.0 * dt; // Gravity
49        self.ball.position += self.ball.velocity * dt;
50
51        // Bounce off walls
52        let w = ctx.screen_width();
53        let h = ctx.screen_height();
54
55        if self.ball.position.x - self.ball.radius < 0.0 {
56            self.ball.position.x = self.ball.radius;
57            self.ball.velocity.x = self.ball.velocity.x.abs();
58        }
59        if self.ball.position.x + self.ball.radius > w {
60            self.ball.position.x = w - self.ball.radius;
61            self.ball.velocity.x = -self.ball.velocity.x.abs();
62        }
63        if self.ball.position.y - self.ball.radius < 0.0 {
64            self.ball.position.y = self.ball.radius;
65            self.ball.velocity.y = self.ball.velocity.y.abs();
66        }
67        if self.ball.position.y + self.ball.radius > h {
68            self.ball.position.y = h - self.ball.radius;
69            self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70        }
71
72        // Update trail
73        self.ball.trail.push((self.ball.position, 1.0));
74        if self.ball.trail.len() > 60 {
75            self.ball.trail.remove(0);
76        }
77        for (_, alpha) in &mut self.ball.trail {
78            *alpha -= dt * 2.0;
79        }
80        self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82        // --- Input ---
83        if ctx.input.is_action_pressed("shake") {
84            ctx.graphics.camera.shake(10.0, 0.3);
85        }
86
87        if ctx.input.mouse.is_pressed(MouseButton::Left) {
88            self.click_count += 1;
89            // Change ball color
90            self.hue = (self.hue + 30.0) % 360.0;
91            self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92            // Boost ball toward click
93            let dir = ctx.input.mouse.position - self.ball.position;
94            self.ball.velocity += dir.normalize_or_zero() * 200.0;
95        }
96
97        // Zoom with scroll
98        let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99        ctx.graphics.camera.set_zoom(zoom);
100
101        // Escape to quit
102        if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103            ctx.quit();
104        }
105
106        // Update camera follow target
107        ctx.graphics.camera.follow(self.ball.position, 0.05);
108    }
Source

pub fn set_viewport(&mut self, viewport: Rect)

Set the viewport rectangle (for split-screen).

Coordinates are in screen pixels (0,0 = top-left).

Source

pub fn clear_viewport(&mut self)

Clear the viewport (render to full screen).

Source

pub fn set_layers(&mut self, layers: u32)

Set which layers this camera renders (bitmask).

Source

pub fn set_z_order(&mut self, z: i32)

Set the z-order (lower renders first).

Source

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

Update the camera (call once per frame).

Handles smooth following and shake.

Source

pub fn view_matrix(&self, screen_size: Vec2) -> Mat4

Get the view-projection matrix for this camera.

This transforms world coordinates to screen coordinates.

Source

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

Convert screen coordinates to world coordinates.

Source

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

Convert world coordinates to screen coordinates.

Source

pub fn visible_rect(&self, screen_size: Vec2) -> Rect

Get the visible world-space rectangle (accounting for zoom and position).

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

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.