Restore the collision pipeline
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
//! Physics pipeline structures.
|
||||
|
||||
use crate::data::{ComponentSet, ComponentSetMut};
|
||||
use crate::data::{ComponentSet, ComponentSetMut, ComponentSetOption};
|
||||
use crate::dynamics::{
|
||||
IslandManager, JointSet, RigidBodyActivation, RigidBodyColliders, RigidBodyDominance,
|
||||
RigidBodyIds, RigidBodyType, RigidBodyVelocity,
|
||||
RigidBodyActivation, RigidBodyChanges, RigidBodyColliders, RigidBodyDominance, RigidBodyHandle,
|
||||
RigidBodyIds, RigidBodyPosition, RigidBodySet, RigidBodyType, RigidBodyVelocity,
|
||||
};
|
||||
use crate::geometry::{
|
||||
BroadPhase, BroadPhasePairEvent, ColliderBroadPhaseData, ColliderChanges, ColliderGroups,
|
||||
ColliderHandle, ColliderMaterial, ColliderPair, ColliderParent, ColliderPosition, ColliderSet,
|
||||
ColliderShape, ColliderType, NarrowPhase,
|
||||
};
|
||||
use crate::geometry::{BroadPhase, BroadPhasePairEvent, ColliderPair, ColliderShape, NarrowPhase};
|
||||
use crate::math::Real;
|
||||
use crate::pipeline::{EventHandler, PhysicsHooks};
|
||||
|
||||
@@ -18,7 +22,6 @@ use crate::pipeline::{EventHandler, PhysicsHooks};
|
||||
pub struct CollisionPipeline {
|
||||
broadphase_collider_pairs: Vec<ColliderPair>,
|
||||
broad_phase_events: Vec<BroadPhasePairEvent>,
|
||||
empty_joints: JointSet,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@@ -33,30 +36,168 @@ impl CollisionPipeline {
|
||||
CollisionPipeline {
|
||||
broadphase_collider_pairs: Vec::new(),
|
||||
broad_phase_events: Vec::new(),
|
||||
empty_joints: JointSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn detect_collisions<Bodies, Colliders>(
|
||||
&mut self,
|
||||
prediction_distance: Real,
|
||||
broad_phase: &mut BroadPhase,
|
||||
narrow_phase: &mut NarrowPhase,
|
||||
bodies: &mut Bodies,
|
||||
colliders: &mut Colliders,
|
||||
modified_colliders: &[ColliderHandle],
|
||||
removed_colliders: &[ColliderHandle],
|
||||
hooks: &dyn PhysicsHooks<Bodies, Colliders>,
|
||||
events: &dyn EventHandler,
|
||||
handle_user_changes: bool,
|
||||
) where
|
||||
Bodies: ComponentSetMut<RigidBodyActivation>
|
||||
+ ComponentSet<RigidBodyType>
|
||||
+ ComponentSetMut<RigidBodyIds>
|
||||
+ ComponentSet<RigidBodyDominance>,
|
||||
Colliders: ComponentSetMut<ColliderBroadPhaseData>
|
||||
+ ComponentSet<ColliderChanges>
|
||||
+ ComponentSet<ColliderPosition>
|
||||
+ ComponentSet<ColliderShape>
|
||||
+ ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderGroups>
|
||||
+ ComponentSet<ColliderMaterial>,
|
||||
{
|
||||
// Update broad-phase.
|
||||
self.broad_phase_events.clear();
|
||||
self.broadphase_collider_pairs.clear();
|
||||
|
||||
broad_phase.update(
|
||||
prediction_distance,
|
||||
colliders,
|
||||
modified_colliders,
|
||||
removed_colliders,
|
||||
&mut self.broad_phase_events,
|
||||
);
|
||||
|
||||
// Update narrow-phase.
|
||||
if handle_user_changes {
|
||||
narrow_phase.handle_user_changes(
|
||||
None,
|
||||
modified_colliders,
|
||||
removed_colliders,
|
||||
colliders,
|
||||
bodies,
|
||||
events,
|
||||
);
|
||||
}
|
||||
|
||||
narrow_phase.register_pairs(None, colliders, bodies, &self.broad_phase_events, events);
|
||||
narrow_phase.compute_contacts(
|
||||
prediction_distance,
|
||||
bodies,
|
||||
colliders,
|
||||
modified_colliders,
|
||||
hooks,
|
||||
events,
|
||||
);
|
||||
narrow_phase.compute_intersections(bodies, colliders, modified_colliders, hooks, events);
|
||||
}
|
||||
|
||||
fn clear_modified_colliders(
|
||||
&mut self,
|
||||
colliders: &mut impl ComponentSetMut<ColliderChanges>,
|
||||
modified_colliders: &mut Vec<ColliderHandle>,
|
||||
) {
|
||||
for handle in modified_colliders.drain(..) {
|
||||
colliders.set_internal(handle.0, ColliderChanges::empty())
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes one step of the collision detection.
|
||||
pub fn step<Bodies, Colliders>(
|
||||
#[cfg(feature = "default-sets")]
|
||||
pub fn step(
|
||||
&mut self,
|
||||
_prediction_distance: Real,
|
||||
_broad_phase: &mut BroadPhase,
|
||||
_narrow_phase: &mut NarrowPhase,
|
||||
_islands: &mut IslandManager,
|
||||
_bodies: &mut Bodies,
|
||||
_colliders: &mut Colliders,
|
||||
_hooks: &dyn PhysicsHooks<Bodies, Colliders>,
|
||||
_events: &dyn EventHandler,
|
||||
prediction_distance: Real,
|
||||
broad_phase: &mut BroadPhase,
|
||||
narrow_phase: &mut NarrowPhase,
|
||||
bodies: &mut RigidBodySet,
|
||||
colliders: &mut ColliderSet,
|
||||
hooks: &dyn PhysicsHooks<RigidBodySet, ColliderSet>,
|
||||
events: &dyn EventHandler,
|
||||
) {
|
||||
let mut modified_bodies = bodies.take_modified();
|
||||
let mut modified_colliders = colliders.take_modified();
|
||||
let mut removed_colliders = colliders.take_removed();
|
||||
|
||||
self.step_generic(
|
||||
prediction_distance,
|
||||
broad_phase,
|
||||
narrow_phase,
|
||||
bodies,
|
||||
colliders,
|
||||
&mut modified_bodies,
|
||||
&mut modified_colliders,
|
||||
&mut removed_colliders,
|
||||
hooks,
|
||||
events,
|
||||
);
|
||||
}
|
||||
|
||||
/// Executes one step of the collision detection.
|
||||
pub fn step_generic<Bodies, Colliders>(
|
||||
&mut self,
|
||||
prediction_distance: Real,
|
||||
broad_phase: &mut BroadPhase,
|
||||
narrow_phase: &mut NarrowPhase,
|
||||
bodies: &mut Bodies,
|
||||
colliders: &mut Colliders,
|
||||
modified_bodies: &mut Vec<RigidBodyHandle>,
|
||||
modified_colliders: &mut Vec<ColliderHandle>,
|
||||
removed_colliders: &mut Vec<ColliderHandle>,
|
||||
hooks: &dyn PhysicsHooks<Bodies, Colliders>,
|
||||
events: &dyn EventHandler,
|
||||
) where
|
||||
Bodies: ComponentSetMut<RigidBodyIds>
|
||||
+ ComponentSetMut<RigidBodyActivation>
|
||||
+ ComponentSet<RigidBodyColliders>
|
||||
Bodies: ComponentSetMut<RigidBodyPosition>
|
||||
+ ComponentSetMut<RigidBodyVelocity>
|
||||
+ ComponentSetMut<RigidBodyIds>
|
||||
+ ComponentSetMut<RigidBodyActivation>
|
||||
+ ComponentSetMut<RigidBodyChanges>
|
||||
+ ComponentSet<RigidBodyColliders>
|
||||
+ ComponentSet<RigidBodyDominance>
|
||||
+ ComponentSet<RigidBodyType>,
|
||||
Colliders: ComponentSetMut<ColliderShape>,
|
||||
Colliders: ComponentSetMut<ColliderBroadPhaseData>
|
||||
+ ComponentSetMut<ColliderChanges>
|
||||
+ ComponentSetMut<ColliderPosition>
|
||||
+ ComponentSet<ColliderShape>
|
||||
+ ComponentSetOption<ColliderParent>
|
||||
+ ComponentSet<ColliderType>
|
||||
+ ComponentSet<ColliderGroups>
|
||||
+ ComponentSet<ColliderMaterial>,
|
||||
{
|
||||
unimplemented!()
|
||||
super::user_changes::handle_user_changes_to_colliders(
|
||||
bodies,
|
||||
colliders,
|
||||
&modified_colliders[..],
|
||||
);
|
||||
super::user_changes::handle_user_changes_to_rigid_bodies(
|
||||
None,
|
||||
bodies,
|
||||
colliders,
|
||||
&modified_bodies,
|
||||
modified_colliders,
|
||||
);
|
||||
self.detect_collisions(
|
||||
prediction_distance,
|
||||
broad_phase,
|
||||
narrow_phase,
|
||||
bodies,
|
||||
colliders,
|
||||
&modified_colliders[..],
|
||||
removed_colliders,
|
||||
hooks,
|
||||
events,
|
||||
true,
|
||||
);
|
||||
|
||||
self.clear_modified_colliders(colliders, modified_colliders);
|
||||
removed_colliders.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ impl PhysicsPipeline {
|
||||
// Update narrow-phase.
|
||||
if handle_user_changes {
|
||||
narrow_phase.handle_user_changes(
|
||||
islands,
|
||||
Some(islands),
|
||||
modified_colliders,
|
||||
removed_colliders,
|
||||
colliders,
|
||||
@@ -134,7 +134,13 @@ impl PhysicsPipeline {
|
||||
events,
|
||||
);
|
||||
}
|
||||
narrow_phase.register_pairs(islands, colliders, bodies, &self.broad_phase_events, events);
|
||||
narrow_phase.register_pairs(
|
||||
Some(islands),
|
||||
colliders,
|
||||
bodies,
|
||||
&self.broad_phase_events,
|
||||
events,
|
||||
);
|
||||
narrow_phase.compute_contacts(
|
||||
integration_parameters.prediction_distance,
|
||||
bodies,
|
||||
@@ -519,7 +525,7 @@ impl PhysicsPipeline {
|
||||
&modified_colliders[..],
|
||||
);
|
||||
super::user_changes::handle_user_changes_to_rigid_bodies(
|
||||
islands,
|
||||
Some(islands),
|
||||
bodies,
|
||||
colliders,
|
||||
&modified_bodies,
|
||||
|
||||
@@ -23,6 +23,9 @@ use parry::query::{DefaultQueryDispatcher, NonlinearRigidMotion, QueryDispatcher
|
||||
use parry::shape::{FeatureId, Shape, TypedSimdCompositeShape};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "default-sets")]
|
||||
use crate::{dynamics::RigidBodySet, geometry::ColliderSet};
|
||||
|
||||
/// A pipeline for performing queries on all the colliders of a scene.
|
||||
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
||||
#[derive(Clone)]
|
||||
@@ -147,8 +150,19 @@ impl QueryPipeline {
|
||||
&*self.query_dispatcher
|
||||
}
|
||||
|
||||
#[cfg(feature = "default-sets")]
|
||||
/// Update the acceleration structure on the query pipeline.
|
||||
pub fn update<Bodies, Colliders>(
|
||||
pub fn update(
|
||||
&mut self,
|
||||
islands: &IslandManager,
|
||||
bodies: &RigidBodySet,
|
||||
colliders: &ColliderSet,
|
||||
) {
|
||||
self.update_generic(islands, bodies, colliders);
|
||||
}
|
||||
|
||||
/// Update the acceleration structure on the query pipeline.
|
||||
pub fn update_generic<Bodies, Colliders>(
|
||||
&mut self,
|
||||
islands: &IslandManager,
|
||||
bodies: &Bodies,
|
||||
|
||||
@@ -37,7 +37,7 @@ pub(crate) fn handle_user_changes_to_colliders<Colliders>(
|
||||
}
|
||||
|
||||
pub(crate) fn handle_user_changes_to_rigid_bodies<Bodies, Colliders>(
|
||||
islands: &mut IslandManager,
|
||||
mut islands: Option<&mut IslandManager>,
|
||||
bodies: &mut Bodies,
|
||||
colliders: &mut Colliders,
|
||||
modified_bodies: &[RigidBodyHandle],
|
||||
@@ -73,65 +73,79 @@ pub(crate) fn handle_user_changes_to_rigid_bodies<Bodies, Colliders>(
|
||||
{
|
||||
// The body's status changed. We need to make sure
|
||||
// it is on the correct active set.
|
||||
if changes.contains(RigidBodyChanges::TYPE) {
|
||||
match status {
|
||||
RigidBodyType::Dynamic => {
|
||||
// Remove from the active kinematic set if it was there.
|
||||
if islands.active_kinematic_set.get(ids.active_set_id) == Some(handle) {
|
||||
islands.active_kinematic_set.swap_remove(ids.active_set_id);
|
||||
final_action =
|
||||
Some((FinalAction::UpdateActiveKinematicSetId, ids.active_set_id));
|
||||
}
|
||||
if let Some(islands) = islands.as_deref_mut() {
|
||||
if changes.contains(RigidBodyChanges::TYPE) {
|
||||
match status {
|
||||
RigidBodyType::Dynamic => {
|
||||
// Remove from the active kinematic set if it was there.
|
||||
if islands.active_kinematic_set.get(ids.active_set_id) == Some(handle) {
|
||||
islands.active_kinematic_set.swap_remove(ids.active_set_id);
|
||||
final_action = Some((
|
||||
FinalAction::UpdateActiveKinematicSetId,
|
||||
ids.active_set_id,
|
||||
));
|
||||
}
|
||||
|
||||
// Add to the active dynamic set.
|
||||
activation.wake_up(true);
|
||||
// Make sure the sleep change flag is set (even if for some
|
||||
// reasons the rigid-body was already awake) to make
|
||||
// sure the code handling sleeping change adds the body to
|
||||
// the active_dynamic_set.
|
||||
changes.set(RigidBodyChanges::SLEEP, true);
|
||||
}
|
||||
RigidBodyType::Kinematic => {
|
||||
// Remove from the active dynamic set if it was there.
|
||||
if islands.active_dynamic_set.get(ids.active_set_id) == Some(&handle) {
|
||||
islands.active_dynamic_set.swap_remove(ids.active_set_id);
|
||||
final_action =
|
||||
Some((FinalAction::UpdateActiveDynamicSetId, ids.active_set_id));
|
||||
// Add to the active dynamic set.
|
||||
activation.wake_up(true);
|
||||
// Make sure the sleep change flag is set (even if for some
|
||||
// reasons the rigid-body was already awake) to make
|
||||
// sure the code handling sleeping change adds the body to
|
||||
// the active_dynamic_set.
|
||||
changes.set(RigidBodyChanges::SLEEP, true);
|
||||
}
|
||||
RigidBodyType::Kinematic => {
|
||||
// Remove from the active dynamic set if it was there.
|
||||
if islands.active_dynamic_set.get(ids.active_set_id) == Some(&handle) {
|
||||
islands.active_dynamic_set.swap_remove(ids.active_set_id);
|
||||
final_action = Some((
|
||||
FinalAction::UpdateActiveDynamicSetId,
|
||||
ids.active_set_id,
|
||||
));
|
||||
}
|
||||
|
||||
// Add to the active kinematic set.
|
||||
if islands.active_kinematic_set.get(ids.active_set_id) != Some(&handle) {
|
||||
ids.active_set_id = islands.active_kinematic_set.len();
|
||||
islands.active_kinematic_set.push(*handle);
|
||||
// Add to the active kinematic set.
|
||||
if islands.active_kinematic_set.get(ids.active_set_id) != Some(&handle)
|
||||
{
|
||||
ids.active_set_id = islands.active_kinematic_set.len();
|
||||
islands.active_kinematic_set.push(*handle);
|
||||
}
|
||||
}
|
||||
RigidBodyType::Static => {}
|
||||
}
|
||||
RigidBodyType::Static => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Update the positions of the colliders.
|
||||
if changes.contains(RigidBodyChanges::POSITION)
|
||||
|| changes.contains(RigidBodyChanges::COLLIDERS)
|
||||
{
|
||||
rb_colliders.update_positions(colliders, modified_colliders, &poss.position);
|
||||
|
||||
if status.is_kinematic()
|
||||
&& islands.active_kinematic_set.get(ids.active_set_id) != Some(handle)
|
||||
// Update the positions of the colliders.
|
||||
if changes.contains(RigidBodyChanges::POSITION)
|
||||
|| changes.contains(RigidBodyChanges::COLLIDERS)
|
||||
{
|
||||
ids.active_set_id = islands.active_kinematic_set.len();
|
||||
islands.active_kinematic_set.push(*handle);
|
||||
}
|
||||
}
|
||||
rb_colliders.update_positions(colliders, modified_colliders, &poss.position);
|
||||
|
||||
// Push the body to the active set if it is not
|
||||
// sleeping and if it is not already inside of the active set.
|
||||
if changes.contains(RigidBodyChanges::SLEEP)
|
||||
&& !activation.sleeping // May happen if the body was put to sleep manually.
|
||||
&& status.is_dynamic() // Only dynamic bodies are in the active dynamic set.
|
||||
&& islands.active_dynamic_set.get(ids.active_set_id) != Some(handle)
|
||||
{
|
||||
ids.active_set_id = islands.active_dynamic_set.len(); // This will handle the case where the activation_channel contains duplicates.
|
||||
islands.active_dynamic_set.push(*handle);
|
||||
if status.is_kinematic()
|
||||
&& islands.active_kinematic_set.get(ids.active_set_id) != Some(handle)
|
||||
{
|
||||
ids.active_set_id = islands.active_kinematic_set.len();
|
||||
islands.active_kinematic_set.push(*handle);
|
||||
}
|
||||
}
|
||||
|
||||
// Push the body to the active set if it is not
|
||||
// sleeping and if it is not already inside of the active set.
|
||||
if changes.contains(RigidBodyChanges::SLEEP)
|
||||
&& !activation.sleeping // May happen if the body was put to sleep manually.
|
||||
&& status.is_dynamic() // Only dynamic bodies are in the active dynamic set.
|
||||
&& islands.active_dynamic_set.get(ids.active_set_id) != Some(handle)
|
||||
{
|
||||
ids.active_set_id = islands.active_dynamic_set.len(); // This will handle the case where the activation_channel contains duplicates.
|
||||
islands.active_dynamic_set.push(*handle);
|
||||
}
|
||||
} else {
|
||||
// We don't use islands. So just update the colliders' positions.
|
||||
if changes.contains(RigidBodyChanges::POSITION)
|
||||
|| changes.contains(RigidBodyChanges::COLLIDERS)
|
||||
{
|
||||
rb_colliders.update_positions(colliders, modified_colliders, &poss.position);
|
||||
}
|
||||
}
|
||||
|
||||
bodies.set_internal(handle.0, RigidBodyChanges::empty());
|
||||
@@ -140,16 +154,18 @@ pub(crate) fn handle_user_changes_to_rigid_bodies<Bodies, Colliders>(
|
||||
}
|
||||
|
||||
// Adjust some ids, if needed.
|
||||
if let Some((action, id)) = final_action {
|
||||
let active_set = match action {
|
||||
FinalAction::UpdateActiveKinematicSetId => &mut islands.active_kinematic_set,
|
||||
FinalAction::UpdateActiveDynamicSetId => &mut islands.active_dynamic_set,
|
||||
};
|
||||
if let Some(islands) = islands.as_deref_mut() {
|
||||
if let Some((action, id)) = final_action {
|
||||
let active_set = match action {
|
||||
FinalAction::UpdateActiveKinematicSetId => &mut islands.active_kinematic_set,
|
||||
FinalAction::UpdateActiveDynamicSetId => &mut islands.active_dynamic_set,
|
||||
};
|
||||
|
||||
if id < active_set.len() {
|
||||
bodies.map_mut_internal(active_set[id].0, |ids2: &mut RigidBodyIds| {
|
||||
ids2.active_set_id = id;
|
||||
});
|
||||
if id < active_set.len() {
|
||||
bodies.map_mut_internal(active_set[id].0, |ids2: &mut RigidBodyIds| {
|
||||
ids2.active_set_id = id;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user