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: Vec2World-space position (the “eye” point — center of the screen).
rotation: f32Rotation in radians.
zoom: f32Zoom level (1.0 = default, >1 = zoom in, <1 = zoom out).
min_zoom: f32Minimum zoom level.
max_zoom: f32Maximum zoom level.
Implementations§
Source§impl Camera
impl Camera
Sourcepub fn set_zoom(&mut self, zoom: f32)
pub fn set_zoom(&mut self, zoom: f32)
Set the zoom level (clamped to min/max).
Examples found in repository?
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 }Sourcepub fn set_zoom_limits(&mut self, min: f32, max: f32)
pub fn set_zoom_limits(&mut self, min: f32, max: f32)
Set zoom limits.
Sourcepub fn follow(&mut self, target: Vec2, lerp_speed: f32)
pub fn follow(&mut self, target: Vec2, lerp_speed: f32)
Make the camera follow a target position with smooth lerp.
Examples found in repository?
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 }Sourcepub fn set_follow_deadzone(&mut self, deadzone: f32)
pub fn set_follow_deadzone(&mut self, deadzone: f32)
Set the deadzone for follow (camera won’t move while target is within this distance).
Sourcepub fn stop_following(&mut self)
pub fn stop_following(&mut self)
Stop following.
Sourcepub fn shake(&mut self, intensity: f32, duration: f32)
pub fn shake(&mut self, intensity: f32, duration: f32)
Trigger screen shake.
intensity— maximum pixel offset in each directionduration— how long the shake lasts (seconds)
Examples found in repository?
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 }Sourcepub fn set_viewport(&mut self, viewport: Rect)
pub fn set_viewport(&mut self, viewport: Rect)
Set the viewport rectangle (for split-screen).
Coordinates are in screen pixels (0,0 = top-left).
Sourcepub fn clear_viewport(&mut self)
pub fn clear_viewport(&mut self)
Clear the viewport (render to full screen).
Sourcepub fn set_layers(&mut self, layers: u32)
pub fn set_layers(&mut self, layers: u32)
Set which layers this camera renders (bitmask).
Sourcepub fn set_z_order(&mut self, z: i32)
pub fn set_z_order(&mut self, z: i32)
Set the z-order (lower renders first).
Sourcepub fn update(&mut self, dt: f32)
pub fn update(&mut self, dt: f32)
Update the camera (call once per frame).
Handles smooth following and shake.
Sourcepub fn view_matrix(&self, screen_size: Vec2) -> Mat4
pub fn view_matrix(&self, screen_size: Vec2) -> Mat4
Get the view-projection matrix for this camera.
This transforms world coordinates to screen coordinates.
Sourcepub fn screen_to_world(&self, screen_pos: Vec2, screen_size: Vec2) -> Vec2
pub fn screen_to_world(&self, screen_pos: Vec2, screen_size: Vec2) -> Vec2
Convert screen coordinates to world coordinates.
Sourcepub fn world_to_screen(&self, world_pos: Vec2, screen_size: Vec2) -> Vec2
pub fn world_to_screen(&self, world_pos: Vec2, screen_size: Vec2) -> Vec2
Convert world coordinates to screen coordinates.
Sourcepub fn visible_rect(&self, screen_size: Vec2) -> Rect
pub fn visible_rect(&self, screen_size: Vec2) -> Rect
Get the visible world-space rectangle (accounting for zoom and position).