aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/debris_motion.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-26 06:21:39 +0300
committerValentin Popov <valentin@popov.link>2026-11-26 06:21:39 +0300
commitf0a92313d0628eda8e163ab0608ce8fcbedaad95 (patch)
tree203c15e66a577e621b88c28428e01a55d9fac37c /crates/fparkan-runtime/src/debris_motion.rs
parent8f1ba615bf1e393698eae7624073c1ad9f54c02b (diff)
downloadfparkan-f0a92313d0628eda8e163ab0608ce8fcbedaad95.tar.xz
fparkan-f0a92313d0628eda8e163ab0608ce8fcbedaad95.zip
feat(combat): show detached assemblies after chassis destruction
Diffstat (limited to 'crates/fparkan-runtime/src/debris_motion.rs')
-rw-r--r--crates/fparkan-runtime/src/debris_motion.rs85
1 files changed, 85 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/debris_motion.rs b/crates/fparkan-runtime/src/debris_motion.rs
new file mode 100644
index 00000000..286bc2b1
--- /dev/null
+++ b/crates/fparkan-runtime/src/debris_motion.rs
@@ -0,0 +1,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);
+ }
+}