1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use super::*;
/// Default camera centered at `(0, 0)` with `1.0x` zoom.
impl Default for Camera2D {
/// Creates a default `Camera2D`.
///
/// # Returns
///
/// - `Self` - Default 2D camera.
fn default() -> Self {
Self {
position: Point::new(0.0, 0.0),
zoom: 1.0,
trauma: 0.0,
max_shake: 14.0,
shake_time: 0.0,
world_bounds: None,
}
}
}
/// Camera projection, smooth follow, frustum culling, and trauma shake operations.
impl Camera2D {
/// Creates a `Camera2D` positioned at `center` with `zoom`.
///
/// # Arguments
///
/// - `Point` - Initial world center.
/// - `f32` - Initial zoom multiplier clamped to `[0.25, 4.0]`.
///
/// # Returns
///
/// - `Self` - Configured 2D camera.
#[must_use]
pub fn at(center: Point, zoom: f32) -> Self {
Self {
position: center,
zoom: zoom.clamp(0.25, 4.0),
..Self::default()
}
}
/// Adds impact trauma in `[0.0, 1.0]` to drive damped screen shake.
///
/// # Arguments
///
/// - `&Self` - Mutable camera.
/// - `f32` - Trauma impulse amount.
pub fn add_trauma(&mut self, amount: f32) {
let next: f32 = (self.get_trauma() + amount.max(0.0)).clamp(0.0, 1.0);
self.set_trauma(next);
}
/// Smoothly follows `target` using frame-rate-independent exponential damping and decays screen shake.
///
/// # Arguments
///
/// - `&Self` - Mutable camera.
/// - `Point` - Desired world target point.
/// - `f32` - Responsiveness factor (`6.0..=16.0` recommended).
/// - `f32` - Elapsed frame delta in seconds.
/// - `Size` - Viewport logical size for optional world-bounds clamping.
pub fn follow(&mut self, target: Point, responsiveness: f32, dt_secs: f32, viewport: Size) {
let alpha: f32 =
(1.0 - (-responsiveness.max(0.1) * dt_secs.max(0.0)).exp()).clamp(0.0, 1.0);
let cur: Point = self.get_position();
let mut nx: f32 = cur.get_x() + (target.get_x() - cur.get_x()) * alpha;
let mut ny: f32 = cur.get_y() + (target.get_y() - cur.get_y()) * alpha;
let Self { world_bounds, .. } = *self;
if let Some(bounds) = world_bounds {
let half_w: f32 = (viewport.get_width() * 0.5) / self.get_zoom().max(0.25);
let half_h: f32 = (viewport.get_height() * 0.5) / self.get_zoom().max(0.25);
let min_x: f32 = bounds.get_x() + half_w;
let max_x: f32 = bounds.right() - half_w;
let min_y: f32 = bounds.get_y() + half_h;
let max_y: f32 = bounds.bottom() - half_h;
nx = if min_x <= max_x {
nx.clamp(min_x, max_x)
} else {
bounds.get_x() + bounds.get_width() * 0.5
};
ny = if min_y <= max_y {
ny.clamp(min_y, max_y)
} else {
bounds.get_y() + bounds.get_height() * 0.5
};
}
self.set_position(Point::new(nx, ny));
let next_trauma: f32 = (self.get_trauma() - dt_secs * 1.8).max(0.0);
self.set_trauma(next_trauma);
self.set_shake_time(self.get_shake_time() + dt_secs * 38.0);
}
/// Computes the current screen-shake offset in world coordinates.
///
/// # Arguments
///
/// - `&Self` - Camera reference.
///
/// # Returns
///
/// - `Point` - Shake offset `(dx, dy)`.
#[must_use]
pub fn shake_offset(&self) -> Point {
let t: f32 = self.get_trauma();
if t <= 0.001 {
return Point::new(0.0, 0.0);
}
let mag: f32 = t * t * self.get_max_shake();
let phase: f32 = self.get_shake_time();
Point::new((phase * 1.7).sin() * mag, (phase * 2.3).cos() * mag)
}
/// Precomputes hoisted camera screen-projection parameters `(effective_cam_x, effective_cam_y, zoom, half_vp_w, half_vp_h)` once per frame so tile/entity/particle loops avoid redundant per-item `sin`/`cos` shake math.
///
/// # Arguments
///
/// - `&Self` - Camera reference.
/// - `Size` - Viewport logical size.
///
/// # Returns
///
/// - `(f32, f32, f32, f32, f32)` - Hoisted `(cam_x_plus_shake, cam_y_plus_shake, zoom, half_vp_w, half_vp_h)` tuple.
#[inline(always)]
#[must_use]
pub fn projection_params(&self, viewport: Size) -> (f32, f32, f32, f32, f32) {
let zoom: f32 = self.get_zoom().max(0.25);
let shake: Point = self.shake_offset();
let cam: Point = self.get_position();
(
cam.get_x() + shake.get_x(),
cam.get_y() + shake.get_y(),
zoom,
viewport.get_width() * 0.5,
viewport.get_height() * 0.5,
)
}
/// Projects a world-space point into viewport-local screen coordinates using a pre-hoisted `shake` offset.
///
/// # Arguments
///
/// - `&Self` - Camera reference.
/// - `Point` - World-space point.
/// - `Size` - Viewport logical size.
/// - `Point` - Precomputed per-frame `shake_offset()` value.
///
/// # Returns
///
/// - `Point` - Viewport-local logical pixel coordinates.
#[inline(always)]
#[must_use]
pub fn world_to_screen_with_shake(&self, world: Point, viewport: Size, shake: Point) -> Point {
let zoom: f32 = self.get_zoom().max(0.25);
let cam: Point = self.get_position();
Point::new(
(world.get_x() - (cam.get_x() + shake.get_x())) * zoom + viewport.get_width() * 0.5,
(world.get_y() - (cam.get_y() + shake.get_y())) * zoom + viewport.get_height() * 0.5,
)
}
/// Projects a world-space point into viewport-local screen coordinates.
///
/// # Arguments
///
/// - `&Self` - Camera reference.
/// - `Point` - World-space point.
/// - `Size` - Viewport logical size.
///
/// # Returns
///
/// - `Point` - Viewport-local logical pixel coordinates.
#[must_use]
pub fn world_to_screen(&self, world: Point, viewport: Size) -> Point {
let shake: Point = self.shake_offset();
self.world_to_screen_with_shake(world, viewport, shake)
}
/// Unprojects a viewport-local screen point back into world coordinates.
///
/// # Arguments
///
/// - `&Self` - Camera reference.
/// - `Point` - Viewport-local logical pixel point.
/// - `Size` - Viewport logical size.
///
/// # Returns
///
/// - `Point` - Corresponding world-space point.
#[must_use]
pub fn screen_to_world(&self, screen: Point, viewport: Size) -> Point {
let zoom: f32 = self.get_zoom().max(0.25);
let cam: Point = self.get_position();
Point::new(
(screen.get_x() - viewport.get_width() * 0.5) / zoom + cam.get_x(),
(screen.get_y() - viewport.get_height() * 0.5) / zoom + cam.get_y(),
)
}
/// Linearly interpolates position, zoom, and shake state between `self` and `other` at `alpha` in `[0.0, 1.0]`.
///
/// # Arguments
///
/// - `Self` - Previous fixed-step camera state.
/// - `Self` - Current fixed-step camera state.
/// - `f32` - Normalized interpolation factor in `[0.0, 1.0]`.
///
/// # Returns
///
/// - `Self` - Render-time interpolated 2D camera.
#[must_use]
pub fn lerp(self, other: Self, alpha: f32) -> Self {
let k: f32 = alpha.clamp(0.0, 1.0);
let p0: Point = self.get_position();
let p1: Point = other.get_position();
let Self { world_bounds, .. } = other;
Self {
position: Point::new(
p0.get_x() + (p1.get_x() - p0.get_x()) * k,
p0.get_y() + (p1.get_y() - p0.get_y()) * k,
),
zoom: self.get_zoom() + (other.get_zoom() - self.get_zoom()) * k,
trauma: self.get_trauma() + (other.get_trauma() - self.get_trauma()) * k,
max_shake: self.get_max_shake() + (other.get_max_shake() - self.get_max_shake()) * k,
shake_time: self.get_shake_time()
+ (other.get_shake_time() - self.get_shake_time()) * k,
world_bounds,
}
}
/// Computes the world-space visible frustum rectangle for culling offscreen sprites and tiles.
///
/// # Arguments
///
/// - `&Self` - Camera reference.
/// - `Size` - Viewport logical size.
///
/// # Returns
///
/// - `Rect` - Visible world-space frustum bounds.
#[must_use]
pub fn visible_world_rect(&self, viewport: Size) -> Rect {
let zoom: f32 = self.get_zoom().max(0.25);
let w: f32 = viewport.get_width() / zoom;
let h: f32 = viewport.get_height() / zoom;
let cam: Point = self.get_position();
let pad: f32 = self.get_max_shake();
Rect::new(
cam.get_x() - w * 0.5 - pad,
cam.get_y() - h * 0.5 - pad,
w + pad * 2.0,
h + pad * 2.0,
)
}
}