-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split physics systems into plugins, refactor
The massive systems file is now gone. Systems have been organized into plugins according to the steps in the physics simulation loop. - PreparePlugin: Update and synchronize components, prepare things - BroadPhasePlugin: Collect pairs of potentially colliding entities - NarrowPhasePlugin: Detect collisions precisely, collect collision data - Integrator: Integrate positions and velocities, apply external forces - Solver: Solve constraints, i.e. joints and collision response This should also make it possible for users to replace parts of the engine with their own plugins. I also renamed contacts to collisions.
- Loading branch information
Showing
10 changed files
with
546 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::prelude::*; | ||
use bevy::prelude::*; | ||
use parry::bounding_volume::BoundingVolume; | ||
|
||
pub struct BroadPhasePlugin; | ||
|
||
impl Plugin for BroadPhasePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.init_resource::<BroadCollisionPairs>() | ||
.add_system_set_to_stage( | ||
FixedUpdateStage, | ||
SystemSet::new() | ||
.label(PhysicsStep::BroadPhase) | ||
.with_run_criteria(first_substep) | ||
.with_system(collect_collision_pairs), | ||
); | ||
} | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
pub struct BroadCollisionPairs(pub Vec<(Entity, Entity)>); | ||
|
||
/// Collects bodies that are potentially colliding. | ||
fn collect_collision_pairs( | ||
bodies: Query<(Entity, &ColliderAabb, &RigidBody)>, | ||
mut broad_collision_pairs: ResMut<BroadCollisionPairs>, | ||
) { | ||
brute_force_collision_pairs(bodies, &mut broad_collision_pairs.0); | ||
} | ||
|
||
/// Collects bodies that are potentially colliding using a brute force algorithm (better one coming soon). | ||
fn brute_force_collision_pairs( | ||
bodies: Query<(Entity, &ColliderAabb, &RigidBody)>, | ||
broad_collision_pairs: &mut Vec<(Entity, Entity)>, | ||
) { | ||
broad_collision_pairs.clear(); | ||
|
||
for [(ent_a, aabb_a, rb_a), (ent_b, aabb_b, rb_b)] in bodies.iter_combinations() { | ||
// At least one of the bodies is dynamic and their AABBs intersect | ||
if (rb_a.is_dynamic() || rb_b.is_dynamic()) && aabb_a.intersects(&aabb_b.0) { | ||
broad_collision_pairs.push((ent_a, ent_b)); | ||
} | ||
} | ||
} |
Oops, something went wrong.