diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 09:01:42 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 09:01:42 +0300 |
| commit | 45262e52841f23f956de8937b933ff5e59ffa94e (patch) | |
| tree | ae6a6fa5158ef54fb086fb618b8bdcb5da89dd8f /apps/fparkan-game/src/player_collision.rs | |
| parent | 0224286b0277d63f320f30df8bd2887ac596d207 (diff) | |
| download | fparkan-45262e52841f23f956de8937b933ff5e59ffa94e.tar.xz fparkan-45262e52841f23f956de8937b933ff5e59ffa94e.zip | |
fix(movement): use visible building stages for collision
Share building stage selection with the renderer and query the current slot for floor and wall contacts. Verify the same floor probe against the intact and damaged factory in a validated stock mission window.
Diffstat (limited to 'apps/fparkan-game/src/player_collision.rs')
| -rw-r--r-- | apps/fparkan-game/src/player_collision.rs | 289 |
1 files changed, 288 insertions, 1 deletions
diff --git a/apps/fparkan-game/src/player_collision.rs b/apps/fparkan-game/src/player_collision.rs index e706104b..6eba3cad 100644 --- a/apps/fparkan-game/src/player_collision.rs +++ b/apps/fparkan-game/src/player_collision.rs @@ -1,6 +1,293 @@ -use super::StaticVulkanApp; +//! Building geometry queries shared by walking and the preview camera. + +use super::{ + damage_visibility::{building_provider_rows, visible_node_stage}, + multiply_row_major, rotate_unit_vector, StaticVulkanApp, +}; +use fparkan_animation::NodePoseBuffer; +use fparkan_msh::{selected_slot, Group, Lod, ModelAsset, NodeId, SlotId}; +use fparkan_render::LegacyIron3dEulerTransform; +use fparkan_render_vulkan::{ + node38_pose, node38_pose_from_hierarchy, node38_pose_relative_to_root, + node38_pose_relative_to_root_from_hierarchy, VulkanNodePose, +}; +use fparkan_runtime::{ + loaded_mission_control_device_tables, query_building_segment_world, BuildingMeshNode, +}; +use std::sync::Arc; + +#[derive(Clone)] +pub(super) struct BuildingCollisionBinding { + pub(super) object_index: usize, + pub(super) component_index: usize, + pub(super) model_source: String, + pub(super) model: Arc<ModelAsset>, + pub(super) root_transform: LegacyIron3dEulerTransform, + pub(super) owner_scale: [f32; 3], + pub(super) mounted: bool, + pub(super) selected_nodes: Vec<(NodeId, SlotId)>, +} + +pub(super) fn building_collision_binding( + object_index: usize, + component_index: usize, + model_source: String, + model: Arc<ModelAsset>, + root_transform: LegacyIron3dEulerTransform, + owner_scale: [f32; 3], + mounted: bool, +) -> Result<BuildingCollisionBinding, String> { + let context = || { + format!( + "building collision object {object_index} component {component_index} model {model_source}" + ) + }; + if model.node_stride != 38 || model.node_count == 0 || model.animation.is_none() { + return Err(format!("{} has no complete Node38 pose data", context())); + } + if model.triangle_descriptors.is_none() { + return Err(format!("{} has no type-7 collision descriptors", context())); + } + if root_transform.try_row_major().is_none() + || !owner_scale.iter().all(|value| value.is_finite()) + || owner_scale.contains(&0.0) + { + return Err(format!("{} has an invalid world transform", context())); + } + let first_node = usize::from(mounted); + let mut selected_nodes = Vec::new(); + for node_index in first_node..model.node_count { + let node = NodeId( + u32::try_from(node_index) + .map_err(|_| format!("{} node index exceeds u32", context()))?, + ); + let Some(slot) = selected_slot(&model, node, Lod(0), Group(0)) else { + continue; + }; + let slot_index = usize::try_from(slot.0) + .map_err(|_| format!("{} node {} has an invalid slot id", context(), node.0))?; + let slot_data = model.slots.get(slot_index).ok_or_else(|| { + format!( + "{} node {} selects missing slot {}", + context(), + node.0, + slot.0 + ) + })?; + let batch_start = usize::from(slot_data.batch_start); + let batch_end = batch_start + .checked_add(usize::from(slot_data.batch_count)) + .ok_or_else(|| format!("{} node {} batch range overflows", context(), node.0))?; + let batches = model.batches.get(batch_start..batch_end).ok_or_else(|| { + format!( + "{} node {} batch range {batch_start}..{batch_end} is invalid", + context(), + node.0 + ) + })?; + if batches.iter().any(|batch| batch.index_count != 0) + || selected_slot(&model, node, Lod(1), Group(0)).is_some() + { + selected_nodes.push((node, slot)); + } + } + Ok(BuildingCollisionBinding { + object_index, + component_index, + model_source, + model, + root_transform, + owner_scale, + mounted, + selected_nodes, + }) +} + +pub(super) fn building_collision_nodes( + binding: &BuildingCollisionBinding, + pose_buffer: Option<&NodePoseBuffer>, + mount_pose: Option<VulkanNodePose>, + visible_stage: impl Fn(usize) -> u32, +) -> Result<Vec<BuildingMeshNode>, String> { + let context = || { + format!( + "building collision object {} component {} model {}", + binding.object_index, binding.component_index, binding.model_source + ) + }; + if binding.mounted != mount_pose.is_some() { + return Err(format!( + "{} has {} mount pose, expected {}", + context(), + if mount_pose.is_some() { "a" } else { "no" }, + if binding.mounted { "a" } else { "no" } + )); + } + binding + .selected_nodes + .iter() + .filter_map(|&(node, intact)| { + let slot = if selected_slot(&binding.model, node, Lod(1), Group(0)).is_some() { + let stage = u8::try_from(visible_stage(node.0 as usize)).ok()?; + selected_slot(&binding.model, node, Lod(stage), Group(0))? + } else { + intact + }; + Some((node, slot)) + }) + .map(|(node, slot)| { + let node_index = + usize::try_from(node.0).map_err(|_| format!("{} node id is invalid", context()))?; + let pose = match (pose_buffer, binding.mounted) { + (Some(hierarchy), true) => { + node38_pose_relative_to_root_from_hierarchy(hierarchy, node_index) + } + (Some(hierarchy), false) => node38_pose_from_hierarchy(hierarchy, node_index), + (None, true) => node38_pose_relative_to_root(&binding.model, None, node_index), + (None, false) => node38_pose(&binding.model, None, node_index), + } + .ok_or_else(|| format!("{} node {} has no current Node38 pose", context(), node.0))?; + let world_from_node = component_node_world_matrix( + binding.root_transform, + binding.owner_scale, + mount_pose, + pose, + ) + .ok_or_else(|| format!("{} node {} has an invalid world matrix", context(), node.0))?; + Ok(BuildingMeshNode { + node, + slot, + world_from_node, + }) + }) + .collect() +} + +fn component_node_world_matrix( + root_transform: LegacyIron3dEulerTransform, + owner_scale: [f32; 3], + mount_pose: Option<VulkanNodePose>, + node_pose: VulkanNodePose, +) -> Option<[f32; 16]> { + if !owner_scale + .iter() + .chain(node_pose.translation.iter()) + .chain(node_pose.rotation.iter()) + .chain(mount_pose.iter().flat_map(|pose| pose.translation.iter())) + .chain(mount_pose.iter().flat_map(|pose| pose.rotation.iter())) + .all(|value| value.is_finite()) + { + return None; + } + let pose_matrix = |pose: VulkanNodePose| { + let [x, y, z] = [ + rotate_unit_vector([1.0, 0.0, 0.0], pose.rotation), + rotate_unit_vector([0.0, 1.0, 0.0], pose.rotation), + rotate_unit_vector([0.0, 0.0, 1.0], pose.rotation), + ]; + [ + x[0], + y[0], + z[0], + pose.translation[0], + x[1], + y[1], + z[1], + pose.translation[1], + x[2], + y[2], + z[2], + pose.translation[2], + 0.0, + 0.0, + 0.0, + 1.0, + ] + }; + let scale = [ + owner_scale[0], + 0.0, + 0.0, + 0.0, + 0.0, + owner_scale[1], + 0.0, + 0.0, + 0.0, + 0.0, + owner_scale[2], + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + ]; + let mut world = multiply_row_major(root_transform.try_row_major()?, scale); + if let Some(mount) = mount_pose { + world = multiply_row_major(world, pose_matrix(mount)); + } + world = multiply_row_major(world, pose_matrix(node_pose)); + world.iter().all(|value| value.is_finite()).then_some(world) +} impl StaticVulkanApp { + pub(super) fn building_segment_blocked( + &self, + world_segment: [[f32; 3]; 2], + ) -> Result<bool, String> { + Ok(self.building_segment_hit(world_segment, false)?.is_some()) + } + + pub(super) fn building_segment_hit( + &self, + world_segment: [[f32; 3]; 2], + nearest: bool, + ) -> Result<Option<[f32; 3]>, String> { + let mut best = None; + let tables = self + .mission_engine + .as_ref() + .and_then(loaded_mission_control_device_tables) + .unwrap_or_default(); + for binding in &self.building_collision_bindings { + if binding.selected_nodes.is_empty() + || self.removed_mission_roots.contains(&binding.object_index) + { + continue; + } + let pose_buffer = self + .component_pose_buffers + .get(binding.object_index) + .and_then(|components| components.get(binding.component_index)) + .and_then(Option::as_ref); + let mount_pose = self + .component_mount_poses + .get(binding.object_index) + .and_then(|components| components.get(binding.component_index)) + .copied() + .flatten(); + let providers = building_provider_rows(tables, binding.object_index); + let nodes = building_collision_nodes(binding, pose_buffer, mount_pose, |node| { + visible_node_stage(providers, binding.component_index, node) + })?; + if let Some(hit) = query_building_segment_world(&binding.model, &nodes, world_segment) + .map_err(|error| { + format!( + "query building collision object {} component {} model {}: {error}", + binding.object_index, binding.component_index, binding.model_source + ) + })? { + if !nearest { + return Ok(Some(hit.position)); + } + if best.is_none_or(|(distance, _)| hit.distance_squared < distance) { + best = Some((hit.distance_squared, hit.position)); + } + } + } + Ok(best.map(|(_, position)| position)) + } + pub(super) fn resolve_player_building_step( &self, mut step: fparkan_runtime::PlayerMotionStep, |
