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: Vec2Current 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: f32Smooth-follow factor: 0.0 = instant snap, 1.0 = no movement at all.
zoom: f32Zoom level: 1.0 = normal, > 1.0 = zoomed in, < 1.0 = zoomed out.
viewport_size: Vec2Size of the viewport / screen in pixels.
Implementations§
Source§impl Camera
impl Camera
Sourcepub fn new(viewport_width: f32, viewport_height: f32) -> Self
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).
Sourcepub fn follow_target(&mut self, target: Vec2)
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.
Sourcepub fn stop_follow(&mut self)
pub fn stop_follow(&mut self)
Stop following any target. The camera stays at its current position.
Sourcepub fn update(&mut self, dt: f32)
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.
Sourcepub fn world_to_screen(&self, world_pos: Vec2) -> Vec2
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);Sourcepub fn screen_to_world(&self, screen_pos: Vec2) -> Vec2
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);Sourcepub fn clamp_to_bounds(&mut self, bounds: Rect)
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.
Sourcepub fn clear_bounds(&mut self)
pub fn clear_bounds(&mut self)
Remove world boundaries. The camera can scroll anywhere.
Sourcepub fn set_zoom(&mut self, zoom: f32)
pub fn set_zoom(&mut self, zoom: f32)
Set the zoom level, clamped to the valid range [0.1, 10.0].
Sourcepub fn tile_x(&self) -> i32
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.
Sourcepub fn tile_y(&self) -> i32
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.
Sourcepub fn is_visible(&self, world_pos: Vec2) -> bool
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.
Sourcepub fn is_rect_visible(&self, rect: Rect) -> bool
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.