alice-physics 1.0.0

Deterministic 128-bit Fixed-Point Physics Engine
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Fracture Simulation Modifier
//!
//! Stress accumulation from impacts and sustained loads drives crack
//! propagation through the SDF. When accumulated stress exceeds the
//! material's fracture toughness, cracks form along stress concentration
//! lines and are subtracted from the SDF via CSG.
//!
//! # Model
//!
//! 1. Impacts and contacts accumulate stress in a 3D field
//! 2. Stress above threshold triggers fracture seed points
//! 3. Cracks propagate outward from seeds (thin box CSG subtraction)
//! 4. Voronoi-like pattern from multiple seeds creates realistic fragments
//!
//! Author: Moroya Sakamoto

use crate::sim_field::ScalarField3D;
use crate::sim_modifier::PhysicsModifier;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

// ============================================================================
// Configuration
// ============================================================================

/// Fracture modifier configuration
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FractureConfig {
    /// Stress threshold for crack initiation
    pub fracture_toughness: f32,
    /// Crack width (SDF subtraction thickness)
    pub crack_width: f32,
    /// Maximum number of active cracks
    pub max_cracks: usize,
    /// Stress diffusion rate
    pub stress_diffusion: f32,
    /// Stress decay rate
    pub stress_decay: f32,
    /// Crack propagation speed
    pub propagation_speed: f32,
    /// Maximum crack length
    pub max_crack_length: f32,
}

impl Default for FractureConfig {
    fn default() -> Self {
        Self {
            fracture_toughness: 50.0,
            crack_width: 0.02,
            max_cracks: 32,
            stress_diffusion: 0.3,
            stress_decay: 0.1,
            propagation_speed: 5.0,
            max_crack_length: 3.0,
        }
    }
}

// ============================================================================
// Crack Representation
// ============================================================================

/// A single crack segment (line from start to end with width)
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Crack {
    /// Crack start point
    pub start: (f32, f32, f32),
    /// Crack end point (current tip)
    pub end: (f32, f32, f32),
    /// Crack direction (normalized)
    pub direction: (f32, f32, f32),
    /// Current crack length
    pub length: f32,
    /// Whether crack is still propagating
    pub active: bool,
}

impl Crack {
    /// SDF of a crack: capsule with given width
    ///
    /// Returns signed distance to the crack volume
    fn distance(&self, x: f32, y: f32, z: f32, width: f32) -> f32 {
        // Capsule SDF between start and end
        let (ax, ay, az) = self.start;
        let (bx, by, bz) = self.end;

        let pax = x - ax;
        let pay = y - ay;
        let paz = z - az;
        let b_ax = bx - ax;
        let b_ay = by - ay;
        let b_az = bz - az;

        let ba_len_sq = b_az.mul_add(b_az, b_ax.mul_add(b_ax, b_ay * b_ay));
        let h = if ba_len_sq > 1e-10 {
            (paz.mul_add(b_az, pax.mul_add(b_ax, pay * b_ay)) / ba_len_sq).clamp(0.0, 1.0)
        } else {
            0.0
        };

        let dx = b_ax.mul_add(-h, pax);
        let dy = b_ay.mul_add(-h, pay);
        let dz = b_az.mul_add(-h, paz);

        (dx * dx + dy * dy + dz * dz).sqrt() - width
    }
}

// ============================================================================
// Fracture Modifier
// ============================================================================

/// Fracture modifier: stress accumulation and crack propagation
pub struct FractureModifier {
    /// Configuration
    pub config: FractureConfig,
    /// Accumulated stress field
    pub stress: ScalarField3D,
    /// Active cracks
    pub cracks: Vec<Crack>,
    /// Whether modifier is enabled
    pub enabled: bool,
}

impl FractureModifier {
    /// Create a new fracture modifier
    #[must_use]
    pub fn new(
        config: FractureConfig,
        resolution: usize,
        min: (f32, f32, f32),
        max: (f32, f32, f32),
    ) -> Self {
        Self {
            config,
            stress: ScalarField3D::new(resolution, resolution, resolution, min, max),
            cracks: Vec::new(),
            enabled: true,
        }
    }

    /// Apply stress at a point (from impact or load)
    pub fn apply_stress_at(&mut self, x: f32, y: f32, z: f32, amount: f32, radius: f32) {
        self.stress.splat(x, y, z, amount, radius);
    }

    /// Get stress at a point
    #[must_use]
    pub fn stress_at(&self, x: f32, y: f32, z: f32) -> f32 {
        self.stress.sample(x, y, z)
    }

    /// Number of active cracks
    #[must_use]
    pub fn active_crack_count(&self) -> usize {
        self.cracks.iter().filter(|c| c.active).count()
    }

    /// Check for new fracture seeds where stress exceeds threshold
    fn check_fracture_seeds(&mut self) {
        if self.cracks.len() >= self.config.max_cracks {
            return;
        }

        let threshold = self.config.fracture_toughness;
        let nx = self.stress.nx;
        let ny = self.stress.ny;
        let nz = self.stress.nz;

        for iz in 0..nz {
            for iy in 0..ny {
                for ix in 0..nx {
                    if self.cracks.len() >= self.config.max_cracks {
                        return;
                    }

                    let s = self.stress.get(ix, iy, iz);
                    if s > threshold {
                        let wx = self.stress.min.0
                            + ix as f32 * (self.stress.max.0 - self.stress.min.0)
                                / (nx - 1).max(1) as f32;
                        let wy = self.stress.min.1
                            + iy as f32 * (self.stress.max.1 - self.stress.min.1)
                                / (ny - 1).max(1) as f32;
                        let wz = self.stress.min.2
                            + iz as f32 * (self.stress.max.2 - self.stress.min.2)
                                / (nz - 1).max(1) as f32;

                        // Don't seed near existing cracks
                        let too_close = self.cracks.iter().any(|c| {
                            let dx = c.start.0 - wx;
                            let dy = c.start.1 - wy;
                            let dz = c.start.2 - wz;
                            (dx * dx + dy * dy + dz * dz).sqrt() < self.config.crack_width * 10.0
                        });

                        if !too_close {
                            // Crack direction: along stress gradient (perpendicular to max stress)
                            let (gx, gy, gz) = self.stress.gradient(wx, wy, wz);
                            let glen = gz.mul_add(gz, gx.mul_add(gx, gy * gy)).sqrt();

                            // Perpendicular to gradient (crack runs along stress contour)
                            let (dx, dy, dz) = if glen > 1e-5 {
                                // Cross with up vector for horizontal crack tendency
                                let cx = gy.mul_add(0.0, -(gz * 1.0));
                                let cy = gz.mul_add(0.0, -(gx * 0.0));
                                let cz = gx.mul_add(1.0, -(gy * 0.0));
                                let clen = (cx * cx + cy * cy + cz * cz).sqrt();
                                if clen > 1e-5 {
                                    (cx / clen, cy / clen, cz / clen)
                                } else {
                                    (1.0, 0.0, 0.0)
                                }
                            } else {
                                // Random-ish direction based on position
                                let hash =
                                    ((ix * 73856093) ^ (iy * 19349663) ^ (iz * 83492791)) as f32;
                                let angle = hash * 0.0001;
                                (angle.cos(), 0.0, angle.sin())
                            };

                            self.cracks.push(Crack {
                                start: (wx, wy, wz),
                                end: (wx, wy, wz),
                                direction: (dx, dy, dz),
                                length: 0.0,
                                active: true,
                            });

                            // Consume stress at seed point
                            self.stress.set(ix, iy, iz, 0.0);
                        }
                    }
                }
            }
        }
    }

    /// Propagate active cracks
    fn propagate_cracks(&mut self, dt: f32) {
        let speed = self.config.propagation_speed;
        let max_len = self.config.max_crack_length;

        for crack in &mut self.cracks {
            if !crack.active {
                continue;
            }

            let growth = speed * dt;
            crack.length += growth;

            if crack.length >= max_len {
                crack.length = max_len;
                crack.active = false;
            }

            // Extend tip
            crack.end = (
                crack.direction.0.mul_add(crack.length, crack.start.0),
                crack.direction.1.mul_add(crack.length, crack.start.1),
                crack.direction.2.mul_add(crack.length, crack.start.2),
            );
        }
    }
}

impl PhysicsModifier for FractureModifier {
    #[inline]
    fn modify_distance(&self, x: f32, y: f32, z: f32, original_dist: f32) -> f32 {
        if !self.enabled || self.cracks.is_empty() {
            return original_dist;
        }

        let width = self.config.crack_width;
        let mut d = original_dist;

        for crack in &self.cracks {
            if crack.length < 1e-5 {
                continue;
            }
            let crack_dist = crack.distance(x, y, z, width);
            // CSG subtraction: max(original, -crack)
            // Only subtract where inside the original shape
            if d < width * 2.0 {
                d = d.max(-crack_dist);
            }
        }

        d
    }

    fn update(&mut self, dt: f32) {
        if !self.enabled {
            return;
        }

        // 1. Diffuse stress
        self.stress.diffuse(dt, self.config.stress_diffusion);

        // 2. Decay stress
        self.stress.decay(self.config.stress_decay, dt);

        // 3. Check for new fracture seeds
        self.check_fracture_seeds();

        // 4. Propagate existing cracks
        self.propagate_cracks(dt);
    }

    fn name(&self) -> &'static str {
        "fracture"
    }

    fn is_active(&self) -> bool {
        self.enabled
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fracture_no_stress() {
        let config = FractureConfig::default();
        let modifier = FractureModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));

        let d = modifier.modify_distance(0.0, 0.0, 0.0, -0.5);
        assert!((d - (-0.5)).abs() < 0.01, "No stress = no cracks");
        assert_eq!(modifier.active_crack_count(), 0);
    }

    #[test]
    fn test_fracture_crack_creation() {
        let config = FractureConfig {
            fracture_toughness: 10.0,
            ..Default::default()
        };
        let mut modifier = FractureModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));

        // Apply massive stress (radius must cover grid cells)
        modifier.apply_stress_at(0.0, 0.0, 0.0, 500.0, 1.5);

        // Step to trigger crack
        modifier.update(0.016);

        assert!(
            !modifier.cracks.is_empty(),
            "High stress should create cracks"
        );
    }

    #[test]
    fn test_crack_propagation() {
        let config = FractureConfig {
            fracture_toughness: 5.0,
            propagation_speed: 10.0,
            max_crack_length: 1.0,
            ..Default::default()
        };
        let mut modifier = FractureModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));

        modifier.apply_stress_at(0.0, 0.0, 0.0, 500.0, 1.5);
        modifier.update(0.016); // Create crack

        let initial_len = modifier.cracks.first().map_or(0.0, |c| c.length);

        // Propagate
        for _ in 0..10 {
            modifier.update(0.016);
        }

        let final_len = modifier.cracks.first().map_or(0.0, |c| c.length);
        assert!(
            final_len > initial_len,
            "Crack should grow: initial={initial_len}, final={final_len}"
        );
    }

    #[test]
    fn test_crack_sdf_subtraction() {
        let config = FractureConfig {
            crack_width: 0.1,
            ..Default::default()
        };
        let mut modifier = FractureModifier::new(config, 8, (-2.0, -2.0, -2.0), (2.0, 2.0, 2.0));

        // Manually add a crack
        modifier.cracks.push(Crack {
            start: (-1.0, 0.0, 0.0),
            end: (1.0, 0.0, 0.0),
            direction: (1.0, 0.0, 0.0),
            length: 2.0,
            active: false,
        });

        // Point near the crack should be affected
        let d = modifier.modify_distance(0.0, 0.0, 0.0, -0.05);
        // Original was inside (-0.05), crack distance at (0,0,0) should be near -0.1
        // max(-0.05, -(-0.1)) = max(-0.05, 0.1) = 0.1
        assert!(d > -0.05, "Crack should cut into the SDF, got {d}");
    }
}