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
345
346
347
use super::*;
/// Declarative physical configuration for `LiquidFluidSolver` and `GyroLiquidGameScene`.
#[derive(Clone, Copy, Data, Debug, PartialEq)]
pub struct GyroLiquidConfig {
/// Physical glass vessel interior width in SI meters (default `0.12 m`).
#[get(type(copy))]
pub(crate) vessel_width_m: f32,
/// Physical glass vessel interior height in SI meters (default `0.09 m`).
#[get(type(copy))]
pub(crate) vessel_height_m: f32,
/// Fraction of vessel area filled with liquid water (`0.20..=0.85`).
#[get(type(copy))]
pub(crate) fill_ratio: f32,
/// Relative viscosity multiplier (`1.0` = pure water at 20 °C; range `0.2..=6.0`).
#[get(type(copy))]
pub(crate) viscosity_scale: f32,
/// Relative surface tension multiplier (`1.0` = pure water `0.0728 N/m`; range `0.0..=3.0`).
#[get(type(copy))]
pub(crate) surface_tension_scale: f32,
/// Whether Archimedes buoyant floating objects (orb + ice cube) are active.
#[get(type(copy))]
pub(crate) buoyant_bodies_enabled: bool,
/// Number of PBF substeps per fixed simulation tick (`2..=4`).
#[get(type(copy))]
pub(crate) substeps: u32,
/// Number of Jacobi PBF density constraint iterations per substep (`2..=6`).
#[get(type(copy))]
pub(crate) solver_iterations: u32,
/// Target particle count at `fill_ratio == 0.50` (`240..=FLUID_MAX_PARTICLES`).
#[get(type(copy))]
pub(crate) base_particle_count: usize,
}
/// Interactive buoyant rigid body experiencing real Archimedes buoyancy, hydrodynamic drag, and wave torque.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct BuoyantBody {
/// Visual and physical classification (`FloatingOrb` or `IceCube`).
#[get(type(copy))]
pub(crate) kind: BuoyantBodyKind,
/// Center x-coordinate inside the vessel in SI meters.
#[get(type(copy))]
pub(crate) x: f32,
/// Center y-coordinate inside the vessel in SI meters.
#[get(type(copy))]
pub(crate) y: f32,
/// Linear x-velocity in `m/s`.
#[get(type(copy))]
pub(crate) vx: f32,
/// Linear y-velocity in `m/s`.
#[get(type(copy))]
pub(crate) vy: f32,
/// Orientation angle in radians.
#[get(type(copy))]
pub(crate) angle: f32,
/// Angular velocity in `rad/s`.
#[get(type(copy))]
pub(crate) omega: f32,
/// Characteristic radius (half-diagonal / sphere radius) in SI meters.
#[get(type(copy))]
pub(crate) radius: f32,
/// Material mass density in `kg/m³` (`< 1000.0` floats in water).
#[get(type(copy))]
pub(crate) density_kgm3: f32,
/// Live submerged volume fraction in `[0.0, 1.0]` evaluated from the fluid density field.
#[get(type(copy))]
pub(crate) submerged_fraction: f32,
}
/// Entrained aerated foam or rising micro-bubble sample captured for optical rendering.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct FluidBubble {
/// Bubble x-coordinate inside the vessel in SI meters.
#[get(type(copy))]
pub(crate) x: f32,
/// Bubble y-coordinate inside the vessel in SI meters.
#[get(type(copy))]
pub(crate) y: f32,
/// Bubble x-velocity in `m/s`.
#[get(type(copy))]
pub(crate) vx: f32,
/// Bubble y-velocity in `m/s`.
#[get(type(copy))]
pub(crate) vy: f32,
/// Bubble radius in SI meters.
#[get(type(copy))]
pub(crate) radius: f32,
/// Elapsed age in seconds.
#[get(type(copy))]
pub(crate) age: f32,
/// Maximum lifetime in seconds.
#[get(type(copy))]
pub(crate) lifetime: f32,
/// Surface foam intensity factor in `[0.0, 1.0]` (`1.0` = bright crest foam, `0.0` = crisp rising underwater micro-bubble).
#[get(type(copy))]
pub(crate) foam_factor: f32,
}
/// Read-only live physics, optical rendering, and sensor telemetry emitted by `GyroLiquidGameScene`.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct GyroLiquidStats {
/// Active PBF liquid particle count.
#[get(type(copy))]
pub(crate) particle_count: usize,
/// Active entrained foam and rising micro-bubble count.
#[get(type(copy))]
pub(crate) bubble_count: usize,
/// Active floating buoyant body count.
#[get(type(copy))]
pub(crate) buoyant_body_count: usize,
/// Total fluid kinetic energy in millijoules per meter tank depth (`mJ/m`).
#[get(type(copy))]
pub(crate) kinetic_energy_mj: f32,
/// Active screen-aligned gravity x-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_x: f32,
/// Active screen-aligned gravity y-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_y: f32,
/// Active screen-aligned gravity z-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_z: f32,
/// Current device/container roll tilt angle in degrees (`0.0` = upright, `+90.0` = clockwise right-down).
#[get(type(copy))]
pub(crate) tilt_angle_deg: f32,
/// Peak wave free-surface elevation amplitude relative to hydrostatic mean in meters.
#[get(type(copy))]
pub(crate) peak_wave_height_m: f32,
/// Last fixed-update simulation time in milliseconds.
#[get(type(copy))]
pub(crate) sim_time_ms: f32,
/// Last optical liquid frame render time in milliseconds.
#[get(type(copy))]
pub(crate) render_time_ms: f32,
/// Active sensor driving mode (`Auto`, `LiveHardware`, or `VirtualTilt`).
#[get(type(copy))]
pub(crate) sensor_mode: FluidSensorMode,
/// Whether live hardware motion sensor data is actively driving the simulation.
#[get(type(copy))]
pub(crate) sensor_active: bool,
/// Current physical vessel width in SI meters.
#[get(type(copy))]
pub(crate) vessel_width_m: f32,
/// Current physical vessel height in SI meters.
#[get(type(copy))]
pub(crate) vessel_height_m: f32,
/// Current liquid fill ratio (`0.20..=0.85`).
#[get(type(copy))]
pub(crate) fill_ratio: f32,
/// Last optical framebuffer width in pixels.
#[get(type(copy))]
pub(crate) render_width: u32,
/// Last optical framebuffer height in pixels.
#[get(type(copy))]
pub(crate) render_height: u32,
}
/// Zero-allocation double-buffered simulation snapshot consumed by `LiquidOpticalRenderer`.
#[derive(Clone, Data, Debug)]
pub struct FluidSimSnapshot {
/// Physical vessel width in SI meters at snapshot capture time.
#[get(type(copy))]
pub(crate) vessel_width_m: f32,
/// Physical vessel height in SI meters at snapshot capture time.
#[get(type(copy))]
pub(crate) vessel_height_m: f32,
/// Smoothing kernel radius `h` in SI meters.
#[get(type(copy))]
pub(crate) kernel_radius_m: f32,
/// Active fluid particle count (`<= pos_x.len()`).
#[get(type(copy))]
pub(crate) particle_count: usize,
/// Active bubble/foam count (`<= bubbles.len()`).
#[get(type(copy))]
pub(crate) bubble_count: usize,
/// Active buoyant body count (`<= buoyant_bodies.len()`).
#[get(type(copy))]
pub(crate) buoyant_body_count: usize,
/// Active screen-aligned gravity x-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_x: f32,
/// Active screen-aligned gravity y-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_y: f32,
/// Active screen-aligned gravity z-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_z: f32,
/// Monotonic simulation time in seconds for subtle caustic shimmer continuity.
#[get(type(copy))]
pub(crate) sim_time_secs: f32,
/// Pre-allocated particle x-coordinates in SI meters.
pub(crate) pos_x: Vec<f32>,
/// Pre-allocated particle y-coordinates in SI meters.
pub(crate) pos_y: Vec<f32>,
/// Pre-allocated particle x-velocities in `m/s`.
pub(crate) vel_x: Vec<f32>,
/// Pre-allocated particle y-velocities in `m/s`.
pub(crate) vel_y: Vec<f32>,
/// Pre-allocated particle normalized density ratios (`ρ / ρ₀`).
pub(crate) density_ratio: Vec<f32>,
/// Pre-allocated active buoyant bodies.
pub(crate) buoyant_bodies: Vec<BuoyantBody>,
/// Pre-allocated active bubbles and foam particles.
pub(crate) bubbles: Vec<FluidBubble>,
}
/// Real-time SI Position-Based Fluids (PBF) 2D liquid engine with non-inertial rotating-device dynamics, Archimedes buoyancy, and zero steady-state heap allocations.
#[derive(Clone, Data, Debug)]
pub struct LiquidFluidSolver {
/// Active physical simulation configuration.
#[get(type(copy))]
pub(crate) config: GyroLiquidConfig,
/// Active PBF particle count (`<= FLUID_MAX_PARTICLES`).
#[get(type(copy))]
pub(crate) particle_count: usize,
/// Nominal resting particle spacing `d0` in meters.
#[get(type(copy))]
pub(crate) rest_spacing_m: f32,
/// Particle area `A_p = d0²` in `m²`.
#[get(type(copy))]
pub(crate) particle_area_m2: f32,
/// Particle 2D mass `m_p = ρ₀ * A_p` in `kg/m`.
#[get(type(copy))]
pub(crate) particle_mass_kgpm: f32,
/// Smoothing kernel radius `h` in meters.
#[get(type(copy))]
pub(crate) kernel_radius_m: f32,
/// Precomputed 2D Poly6 normalization factor `4 / (π h⁸)`.
#[get(type(copy))]
pub(crate) poly6_coeff: f32,
/// Precomputed 2D Spiky gradient normalization factor `-30 / (π h⁵)`.
#[get(type(copy))]
pub(crate) spiky_grad_coeff: f32,
/// Precomputed reference Poly6 weight `W_poly6(Δq, h)` for tensile instability `s_corr`.
#[get(type(copy))]
pub(crate) w_dq: f32,
/// Current screen-aligned gravity `(gx, gy, gz)` in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_x: f32,
/// Current screen-aligned gravity y-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_y: f32,
/// Current screen-aligned gravity z-component in `m/s²`.
#[get(type(copy))]
pub(crate) gravity_z: f32,
/// Active linear frame acceleration x-component in `m/s²`.
#[get(type(copy))]
pub(crate) linear_accel_x: f32,
/// Active linear frame acceleration y-component in `m/s²`.
#[get(type(copy))]
pub(crate) linear_accel_y: f32,
/// Decaying synthetic shake acceleration x-component in `m/s²`.
#[get(type(copy))]
pub(crate) shake_accel_x: f32,
/// Decaying synthetic shake acceleration y-component in `m/s²`.
#[get(type(copy))]
pub(crate) shake_accel_y: f32,
/// Clockwise container angular velocity `Ω = -gyro_z` in `rad/s`.
#[get(type(copy))]
pub(crate) angular_vel_cw: f32,
/// Previous step clockwise angular velocity for Euler angular acceleration `α = dΩ/dt`.
#[get(type(copy))]
pub(crate) prev_angular_vel_cw: f32,
/// Smoothed clockwise angular acceleration `α` in `rad/s²`.
#[get(type(copy))]
pub(crate) angular_accel_cw: f32,
/// Virtual desktop/simulator roll angle in radians (`0` = upright, `+π/2` = rotated CW right-down).
#[get(type(copy))]
pub(crate) virtual_roll_rad: f32,
/// Virtual desktop/simulator pitch angle in radians (`0` = upright, `+π/2` = lying flat face-up).
#[get(type(copy))]
pub(crate) virtual_pitch_rad: f32,
/// Previous virtual roll angle for deriving virtual rotational velocity `Ω`.
#[get(type(copy))]
pub(crate) prev_virtual_roll_rad: f32,
/// Active container roll tilt angle in radians (derived from active gravity).
#[get(type(copy))]
pub(crate) active_roll_rad: f32,
/// Elapsed simulation time in seconds.
#[get(type(copy))]
pub(crate) sim_time_secs: f32,
/// Deterministic PRNG state word for bubble emission.
#[get(type(copy))]
pub(crate) rng_state: u64,
/// Cached kinetic energy in `mJ/m`.
#[get(type(copy))]
pub(crate) kinetic_energy_mj: f32,
/// Cached peak wave amplitude relative to resting hydrostatic level in meters.
#[get(type(copy))]
pub(crate) peak_wave_height_m: f32,
/// Active spatial hash grid width in cells.
#[get(type(copy))]
pub(crate) grid_w: usize,
/// Active spatial hash grid height in cells.
#[get(type(copy))]
pub(crate) grid_h: usize,
/// Flat SoA particle x-positions in meters.
pub(crate) pos_x: Vec<f32>,
/// Flat SoA particle y-positions in meters.
pub(crate) pos_y: Vec<f32>,
/// Flat SoA predicted x-positions during PBF substeps.
pub(crate) pred_x: Vec<f32>,
/// Flat SoA predicted y-positions during PBF substeps.
pub(crate) pred_y: Vec<f32>,
/// Flat SoA particle x-velocities in `m/s`.
pub(crate) vel_x: Vec<f32>,
/// Flat SoA particle y-velocities in `m/s`.
pub(crate) vel_y: Vec<f32>,
/// Flat SoA PBF Lagrange multipliers `λ_i`.
pub(crate) lambda: Vec<f32>,
/// Flat SoA evaluated fluid densities `ρ_i` in `kg/m³`.
pub(crate) density: Vec<f32>,
/// Flat SoA 2D scalar vorticity `ω_i` in `s⁻¹`.
pub(crate) vorticity: Vec<f32>,
/// Flat SoA PBF position delta x scratch buffer.
pub(crate) corr_x: Vec<f32>,
/// Flat SoA PBF position delta y scratch buffer.
pub(crate) corr_y: Vec<f32>,
/// Flat SoA XSPH / vorticity velocity delta x scratch buffer.
pub(crate) dvx_scratch: Vec<f32>,
/// Flat SoA XSPH / vorticity velocity delta y scratch buffer.
pub(crate) dvy_scratch: Vec<f32>,
/// Pre-allocated spatial hash cell particle counts.
pub(crate) cell_counts: Vec<u32>,
/// Pre-allocated spatial hash cell CSR start offsets.
pub(crate) cell_starts: Vec<u32>,
/// Pre-allocated spatial hash cell write cursors.
pub(crate) cell_cursors: Vec<u32>,
/// Pre-allocated particle-to-cell index map.
pub(crate) particle_cells: Vec<u32>,
/// Pre-allocated counting-sorted particle indices grouped by spatial hash cell.
pub(crate) sorted_indices: Vec<u32>,
/// Flat pre-allocated per-particle neighbor index table (`FLUID_MAX_PARTICLES * FLUID_MAX_NEIGHBORS`).
pub(crate) neighbor_indices: Vec<u16>,
/// Flat pre-allocated per-particle cached neighbor counts (`<= FLUID_MAX_NEIGHBORS`).
pub(crate) neighbor_counts: Vec<u8>,
/// Pre-allocated buoyant floating rigid bodies.
pub(crate) buoyant_bodies: Vec<BuoyantBody>,
/// Active buoyant body count.
#[get(type(copy))]
pub(crate) buoyant_body_count: usize,
/// Pre-allocated entrained micro-bubble & foam pool.
pub(crate) bubbles: Vec<FluidBubble>,
/// Active bubble/foam count in prefix `[0..bubble_count]`.
#[get(type(copy))]
pub(crate) bubble_count: usize,
}