forked from Jondolf/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_broad_phase.rs
98 lines (87 loc) · 3.22 KB
/
custom_broad_phase.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use bevy::prelude::*;
use bevy_xpbd_3d::{math::*, prelude::*, PhysicsSchedule, PhysicsStepSet};
// Required for AABB intersection check. This might be abstracted away at some point.
use bevy_xpbd_3d::parry::bounding_volume::BoundingVolume;
fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins);
// Add PhysicsPlugins and replace default broad phase with our custom broad phase
app.add_plugins(
PhysicsPlugins::default()
.build()
.disable::<BroadPhasePlugin>()
.add(BruteForceBroadPhasePlugin),
);
app.add_systems(Startup, setup).run();
}
// Modified from Bevy's 3d_scene example, a cube falling to the ground with velocity
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Plane
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(8.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
},
RigidBody::Static,
Collider::cuboid(8.0, 0.002, 8.0),
));
// Cube
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 4.0, 0.0),
..default()
},
RigidBody::Dynamic,
AngularVelocity(Vector::new(2.5, 3.4, 1.6)),
Collider::cuboid(1.0, 1.0, 1.0),
));
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
// Camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-4.0, 6.5, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
// Collects pairs of potentially colliding entities into the BroadCollisionPairs resource provided by the physics engine.
// A brute force algorithm is used for simplicity.
pub struct BruteForceBroadPhasePlugin;
impl Plugin for BruteForceBroadPhasePlugin {
fn build(&self, app: &mut App) {
// Make sure the PhysicsSchedule is available
let physics_schedule = app
.get_schedule_mut(PhysicsSchedule)
.expect("add PhysicsSchedule first");
// Add the broad phase system into the broad phase set
physics_schedule.add_systems(collect_collision_pairs.in_set(PhysicsStepSet::BroadPhase));
}
}
fn collect_collision_pairs(
bodies: Query<(Entity, &ColliderAabb, &RigidBody)>,
mut broad_collision_pairs: ResMut<BroadCollisionPairs>,
) {
// Clear old collision pairs
broad_collision_pairs.0.clear();
// Loop through all entity combinations and collect pairs of bodies with intersecting AABBs
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.0.push((ent_a, ent_b));
}
}
}