aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/debris_motion.rs
blob: 286bc2b1285064b28da1c6ff3c4d1488059b6a09 (plain) (blame)
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
//! Ballistic presentation of a detached mesh assembly.

use fparkan_animation::Pose;

const GRAVITY: f32 = 9.81;

/// A visual fragment travelling from its last attached center.
pub struct DebrisMotion {
    center: [f32; 3],
    outward: [f32; 2],
    launch_speed: f32,
    angular_speed: f32,
    landing_seconds: f32,
}

impl DebrisMotion {
    /// Launches a fragment outward with enough energy to lift it one radius.
    pub fn new(center: [f32; 3], hull: [f32; 3], radius: f32, floor_center: f32) -> Option<Self> {
        if !center
            .into_iter()
            .chain(hull)
            .chain([radius, floor_center])
            .all(f32::is_finite)
            || radius <= 0.0
        {
            return None;
        }
        let [x, y] = [center[0] - hull[0], center[1] - hull[1]];
        let length = x.hypot(y);
        let outward = if length > f32::EPSILON {
            [x / length, y / length]
        } else {
            [0.0, 1.0]
        };
        let launch_speed = (2.0 * GRAVITY * radius).sqrt();
        let height = (center[2] - floor_center).max(0.0);
        let landing_seconds =
            (launch_speed + (launch_speed.powi(2) + 2.0 * GRAVITY * height).sqrt()) / GRAVITY;
        Some(Self {
            center,
            outward,
            launch_speed,
            angular_speed: launch_speed / radius,
            landing_seconds,
        })
    }

    /// Returns the new center and rotation. Rotation and travel stop at landing.
    pub fn pose(&self, elapsed_seconds: f32) -> Pose {
        let time = elapsed_seconds.clamp(0.0, self.landing_seconds);
        let travel = self.launch_speed * time;
        let [x, y] = self.outward;
        let (sin, cos) = (0.5 * self.angular_speed * time).sin_cos();
        Pose {
            translation: [
                self.center[0] + x * travel,
                self.center[1] + y * travel,
                self.center[2] + travel - 0.5 * GRAVITY * time.powi(2),
            ],
            rotation: [y * sin, -x * sin, 0.0, cos],
        }
    }
}

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

    #[test]
    fn fragment_begins_attached_and_stops_on_landing() {
        let center = [2.0, 5.0, 4.0];
        let motion = DebrisMotion::new(center, [0.0, 5.0, 0.0], 1.0, 1.0).unwrap();
        assert_eq!(
            motion.pose(0.0),
            Pose {
                translation: center,
                ..Pose::default()
            }
        );
        assert!(motion.pose(0.2).translation[0] > center[0]);
        assert!(motion.pose(0.2).translation[2] > center[2]);
        assert_eq!(motion.pose(4.0), motion.pose(6.0));
        assert!((motion.pose(4.0).translation[2] - 1.0).abs() < 0.00001);
    }
}