diff options
Diffstat (limited to 'crates/fparkan-runtime/src/weapon_pose.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/weapon_pose.rs | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/crates/fparkan-runtime/src/weapon_pose.rs b/crates/fparkan-runtime/src/weapon_pose.rs index b7a9f48f..f01a93e1 100644 --- a/crates/fparkan-runtime/src/weapon_pose.rs +++ b/crates/fparkan-runtime/src/weapon_pose.rs @@ -2,7 +2,7 @@ use crate::{ turret_aim::{apply_autonomous_turret_pose, has_turret_aim, TurretAim}, - MissionAssets, MissionObjectDraft, + turret_sight_points, MissionAssets, MissionObjectDraft, }; use fparkan_animation::{AnimationTime, NodePoseBuffer, Pose}; use fparkan_assets::{ @@ -276,6 +276,60 @@ impl WeaponPoses { } .try_transform_scaled_point(point, state.scale.map(f32::from_bits)) } + + pub(super) fn cpt_axis( + &self, + state: TransformState, + component: usize, + point: &PreparedControlPoint, + ) -> Option<[f32; 3]> { + let node = usize::try_from(point.raw_signed_field_04).ok()?; + let to_hull = + |point| mounted_point(&self.hierarchies, &self.mounts, component, node, point); + let origin = to_hull([0.0; 3])?; + let tip = to_hull(point.direction)?; + LegacyIron3dEulerTransform { + translation: [0.0; 3], + orientation_radians: state.orientation.map(f32::from_bits), + } + .try_transform_scaled_point( + std::array::from_fn(|axis| tip[axis] - origin[axis]), + state.scale.map(f32::from_bits), + ) + .filter(|axis| *axis != [0.0; 3]) + } + + pub(super) fn turret_sight_component(&self, mut component: usize) -> Option<usize> { + for _ in 0..self.mounts.len() { + if self.aim(component).is_some() { + return Some(component); + } + let Mount::Socket(parent, _) = *self.mounts.get(component)? else { + return None; + }; + component = parent; + } + None + } + + pub(super) fn sight_ray( + &self, + assets: &MissionAssets, + object: usize, + state: TransformState, + component: usize, + ) -> Option<[[f32; 3]; 2]> { + let control = assets + .object_component_controls + .get(object)? + .get(component)? + .as_ref()?; + let [origin, axis] = turret_sight_points(control)?; + Some([ + self.muzzle(state, component, origin)?, + self.cpt_axis(state, component, axis)?, + ]) + } } fn mounted_point( @@ -411,13 +465,17 @@ mod tests { raw_signed_field_04: 0, raw_signed_field_08: 0, position: [1.0, 2.0, 3.0], - direction: [0.0, 1.0, 0.0], + direction: [1.0, 2.0, 3.0], name: [0; 32], }; let actual = poses.muzzle(state, 0, &barrel).unwrap(); for (actual, expected) in actual.into_iter().zip([14.0, 32.0, 52.0]) { assert!((actual - expected).abs() < 0.00001); } + let axis = poses.cpt_axis(state, 0, &barrel).unwrap(); + for (actual, expected) in axis.into_iter().zip([-6.0, 2.0, 12.0]) { + assert!((actual - expected).abs() < 0.00001); + } } #[test] |
