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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use super::*;
/// Default radial spark burst emitter configuration.
impl Default for ParticleEmitterConfig {
/// Creates a radial neon spark burst configuration.
///
/// # Returns
///
/// - `Self` - Default particle emitter config.
fn default() -> Self {
Self {
base_angle: 0.0,
spread_rad: TAU,
min_speed: 60.0,
max_speed: 220.0,
min_life: 0.25,
max_life: 0.65,
start_size: 4.5,
end_size: 0.8,
start_color: Color::rgba(56, 189, 248, 255),
end_color: Color::rgba(99, 102, 241, 0),
drag: 2.2,
}
}
}
/// Default 1,024-slot lazy particle system pool.
impl Default for ParticleSystem {
/// Creates a lazily allocated `ParticleSystem` configured for 1,024 pooled slots.
///
/// # Returns
///
/// - `Self` - Ready zero-allocation particle system.
fn default() -> Self {
Self::with_capacity(1024)
}
}
/// Particle emission, deterministic PRNG, and active-prefix SoA kinematics for `ParticleSystem`.
impl ParticleSystem {
/// Creates a fixed-capacity `ParticleSystem` whose SoA lanes allocate lazily on first `emit_burst`.
///
/// # Arguments
///
/// - `usize` - Maximum concurrent particles.
///
/// # Returns
///
/// - `Self` - Zero-allocation particle system ready for lazy emission.
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
let cap: usize = capacity.max(16);
Self {
capacity: cap,
pos_x: Vec::new(),
pos_y: Vec::new(),
vel_x: Vec::new(),
vel_y: Vec::new(),
age: Vec::new(),
lifetime: Vec::new(),
drag: Vec::new(),
start_size: Vec::new(),
end_size: Vec::new(),
start_color: Vec::new(),
end_color: Vec::new(),
min_remaining_life: f32::INFINITY,
cursor: 0,
rng_state: 0x9E37_79B9_7F4A_7C15,
gravity: Point::new(0.0, 0.0),
active_count: 0,
}
}
/// Generates a deterministic pseudo-random `f32` in `[0.0, 1.0)` advancing `state`.
///
/// # Arguments
///
/// - `&mut u64` - Mutable PRNG state word.
///
/// # Returns
///
/// - `f32` - Normalized random scalar.
#[inline(always)]
fn next_unit_f32_from(state: &mut u64) -> f32 {
let next: u64 = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
*state = next;
((next >> 40) as f32) * (1.0 / ((1_u32 << 24) as f32))
}
/// Emits `count` particles at `origin` using `config` into the compacted active prefix without heap allocations.
///
/// # Arguments
///
/// - `&Self` - Mutable particle system.
/// - `Point` - World-space emission origin.
/// - `usize` - Number of particles to emit.
/// - `ParticleEmitterConfig` - Emission parameters.
pub fn emit_burst(&mut self, origin: Point, count: usize, config: ParticleEmitterConfig) {
let Self {
capacity,
pos_x,
pos_y,
vel_x,
vel_y,
age,
lifetime,
drag,
start_size,
end_size,
start_color,
end_color,
min_remaining_life,
cursor,
rng_state,
active_count,
..
} = self;
let cap: usize = *capacity;
if cap == 0 || count == 0 {
return;
}
if pos_x.len() < cap {
pos_x.resize(cap, 0.0);
pos_y.resize(cap, 0.0);
vel_x.resize(cap, 0.0);
vel_y.resize(cap, 0.0);
age.resize(cap, 0.0);
lifetime.resize(cap, 0.0);
drag.resize(cap, 0.0);
start_size.resize(cap, 0.0);
end_size.resize(cap, 0.0);
start_color.resize(cap, Color::default());
end_color.resize(cap, Color::default());
}
let base_angle: f32 = config.get_base_angle();
let spread_rad: f32 = config.get_spread_rad();
let min_speed: f32 = config.get_min_speed();
let speed_span: f32 = config.get_max_speed() - min_speed;
let min_life: f32 = config.get_min_life();
let life_span: f32 = config.get_max_life() - min_life;
let s_size: f32 = config.get_start_size();
let e_size: f32 = config.get_end_size();
let s_col: Color = config.get_start_color();
let e_col: Color = config.get_end_color();
let p_drag: f32 = config.get_drag();
let ox: f32 = origin.get_x();
let oy: f32 = origin.get_y();
let mut cur_rng: u64 = *rng_state;
let mut live: usize = *active_count;
let mut cur_slot: usize = *cursor;
let mut min_rem: f32 = *min_remaining_life;
for _ in 0..count {
let r_ang: f32 = Self::next_unit_f32_from(&mut cur_rng);
let r_spd: f32 = Self::next_unit_f32_from(&mut cur_rng);
let r_life: f32 = Self::next_unit_f32_from(&mut cur_rng);
let angle: f32 = base_angle + (r_ang - 0.5) * spread_rad;
let speed: f32 = min_speed + speed_span * r_spd;
let life: f32 = (min_life + life_span * r_life).max(0.02);
let slot_idx: usize = if live < cap {
let s: usize = live;
live += 1;
s
} else {
let s: usize = cur_slot % cap;
cur_slot = (s + 1) % cap;
s
};
let (sin_a, cos_a): (f32, f32) = angle.sin_cos();
pos_x[slot_idx] = ox;
pos_y[slot_idx] = oy;
vel_x[slot_idx] = cos_a * speed;
vel_y[slot_idx] = sin_a * speed;
age[slot_idx] = 0.0;
lifetime[slot_idx] = life;
drag[slot_idx] = p_drag;
start_size[slot_idx] = s_size;
end_size[slot_idx] = e_size;
start_color[slot_idx] = s_col;
end_color[slot_idx] = e_col;
min_rem = min_rem.min(life);
}
*rng_state = cur_rng;
*active_count = live;
*cursor = cur_slot;
*min_remaining_life = min_rem;
}
/// Advances all active particles in `[0..active_count)` via auto-vectorized SoA kinematics and swap-removes expired slots.
///
/// # Arguments
///
/// - `&Self` - Mutable particle system.
/// - `f32` - Elapsed timestep in seconds.
///
/// # Returns
///
/// - `usize` - Number of particles still active after the update.
pub fn update(&mut self, dt_secs: f32) -> usize {
let Self {
pos_x,
pos_y,
vel_x,
vel_y,
age,
lifetime,
drag,
start_size,
end_size,
start_color,
end_color,
min_remaining_life,
gravity,
active_count,
..
} = self;
let mut n: usize = *active_count;
if n == 0 {
return 0;
}
let gx_dt: f32 = gravity.get_x() * dt_secs;
let gy_dt: f32 = gravity.get_y() * dt_secs;
let neg_dt: f32 = -dt_secs;
let next_min_bound: f32 = *min_remaining_life - dt_secs;
if next_min_bound > 0.0 {
let px: &mut [f32] = &mut pos_x[..n];
let py: &mut [f32] = &mut pos_y[..n];
let vx: &mut [f32] = &mut vel_x[..n];
let vy: &mut [f32] = &mut vel_y[..n];
let ag: &mut [f32] = &mut age[..n];
let dr: &[f32] = &drag[..n];
for i in 0..n {
ag[i] += dt_secs;
let damp: f32 = dr[i].mul_add(neg_dt, 1.0).clamp(0.0, 1.0);
let nvx: f32 = (vx[i] + gx_dt) * damp;
let nvy: f32 = (vy[i] + gy_dt) * damp;
vx[i] = nvx;
vy[i] = nvy;
px[i] = nvx.mul_add(dt_secs, px[i]);
py[i] = nvy.mul_add(dt_secs, py[i]);
}
*min_remaining_life = next_min_bound;
return n;
}
let mut i: usize = 0;
let mut recomputed_min_rem: f32 = f32::INFINITY;
while i < n {
let next_age: f32 = age[i] + dt_secs;
let life: f32 = lifetime[i];
if next_age >= life {
n -= 1;
if i < n {
pos_x[i] = pos_x[n];
pos_y[i] = pos_y[n];
vel_x[i] = vel_x[n];
vel_y[i] = vel_y[n];
age[i] = age[n];
lifetime[i] = lifetime[n];
drag[i] = drag[n];
start_size[i] = start_size[n];
end_size[i] = end_size[n];
start_color[i] = start_color[n];
end_color[i] = end_color[n];
}
continue;
}
let damp: f32 = drag[i].mul_add(neg_dt, 1.0).clamp(0.0, 1.0);
let nvx: f32 = (vel_x[i] + gx_dt) * damp;
let nvy: f32 = (vel_y[i] + gy_dt) * damp;
age[i] = next_age;
vel_x[i] = nvx;
vel_y[i] = nvy;
pos_x[i] = nvx.mul_add(dt_secs, pos_x[i]);
pos_y[i] = nvy.mul_add(dt_secs, pos_y[i]);
recomputed_min_rem = recomputed_min_rem.min(life - next_age);
i += 1;
}
*active_count = n;
*min_remaining_life = recomputed_min_rem;
n
}
/// Returns a snapshot of the active particle at compacted slot `index`, or `None` if `index >= active_count`.
///
/// # Arguments
///
/// - `&Self` - Particle system reference.
/// - `usize` - Active particle index in `0..active_count`.
///
/// # Returns
///
/// - `Option<Particle>` - Reconstructed `Particle` record for inspection.
#[must_use]
pub fn active_particle(&self, index: usize) -> Option<Particle> {
let Self {
pos_x,
pos_y,
vel_x,
vel_y,
age,
lifetime,
drag,
start_size,
end_size,
start_color,
end_color,
active_count,
..
} = self;
if index >= *active_count {
return None;
}
Some(Particle {
active: true,
position: Point::new(pos_x[index], pos_y[index]),
velocity: Point::new(vel_x[index], vel_y[index]),
age: age[index],
lifetime: lifetime[index],
start_size: start_size[index],
end_size: end_size[index],
start_color: start_color[index],
end_color: end_color[index],
drag: drag[index],
})
}
/// Clears all active particles in O(1) while retaining pre-allocated SoA lane capacities.
///
/// # Arguments
///
/// - `&Self` - Mutable particle system.
pub fn clear(&mut self) {
let Self {
active_count,
cursor,
min_remaining_life,
..
} = self;
*active_count = 0;
*cursor = 0;
*min_remaining_life = f32::INFINITY;
}
}