-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
events2.rs
59 lines (52 loc) · 1.67 KB
/
events2.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
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins(DefaultPlugins)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
.add_plugin(RapierDebugRenderPlugin::default())
.add_startup_system(setup_graphics)
.add_startup_system(setup_physics)
.add_system(display_events.in_base_set(CoreSet::PostUpdate))
.run();
}
fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
fn display_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
) {
for collision_event in collision_events.iter() {
println!("Received collision event: {collision_event:?}");
}
for contact_force_event in contact_force_events.iter() {
println!("Received contact force event: {contact_force_event:?}");
}
}
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, -24.0, 0.0)),
Collider::cuboid(80.0, 20.0),
));
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 100.0, 0.0)),
Collider::cuboid(80.0, 30.0),
Sensor,
));
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 260.0, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(10.0, 10.0),
ActiveEvents::COLLISION_EVENTS,
ContactForceEventThreshold(10.0),
));
}