forked from dimforge/bevy_rapier
-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_system_setup2.rs
136 lines (115 loc) · 3.75 KB
/
custom_system_setup2.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use bevy::{core::FrameCount, prelude::*};
use bevy_rapier2d::prelude::*;
#[derive(Resource)]
struct SpecialSchedule {
schedule: Schedule,
}
fn main() {
let mut app = App::new();
app.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(RapierDebugRenderPlugin::default())
.add_startup_system(setup_graphics)
.add_startup_system(setup_physics)
.add_system(run_schedule.in_base_set(CoreSet::PostUpdate));
// Do the setup however we want, maybe in its very own schedule
let mut schedule = Schedule::new();
schedule.configure_sets(
(
PhysicsSet::SyncBackend,
PhysicsSet::SyncBackendFlush,
PhysicsSet::StepSimulation,
PhysicsSet::Writeback,
)
.chain(),
);
schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackend)
.in_base_set(PhysicsSet::SyncBackend),
);
// schedule.add_systems(
// RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::SyncBackendFlush)
// .in_base_set(PhysicsSet::SyncBackendFlush),
// );
app.add_system(apply_system_buffers.in_base_set(PhysicsSet::SyncBackendFlush));
schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::StepSimulation)
.in_base_set(PhysicsSet::StepSimulation),
);
schedule.add_system(despawn_one_box.in_base_set(PhysicsSet::StepSimulation));
schedule.add_systems(
RapierPhysicsPlugin::<NoUserData>::get_systems(PhysicsSet::Writeback)
.in_base_set(PhysicsSet::Writeback),
);
app.insert_resource(SpecialSchedule { schedule })
.add_plugin(
RapierPhysicsPlugin::<NoUserData>::default()
.with_physics_scale(100.)
.with_default_system_setup(false),
)
.run();
}
fn run_schedule(world: &mut World) {
world.resource_scope(|world, mut special: Mut<SpecialSchedule>| {
special.schedule.run(world);
})
}
fn despawn_one_box(
mut commands: Commands,
frame_count: ResMut<FrameCount>,
query: Query<Entity, (With<Collider>, With<RigidBody>)>,
) {
// Delete a box every 10 frames
if frame_count.0 % 10 == 0 && !query.is_empty() {
let len = query.iter().len();
if let Some(entity) = query
.iter()
.skip(frame_count.0 as usize % len) // Get a "random" box to make sim interesting
.next()
{
commands.entity(entity).despawn();
}
}
}
fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0.0, 20.0, 0.0),
..default()
});
}
pub fn setup_physics(mut commands: Commands) {
/*
* Ground
*/
let ground_size = 500.0;
let ground_height = 10.0;
commands.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 0.0 * -ground_height, 0.0)),
Collider::cuboid(ground_size, ground_height),
));
/*
* Create the cubes
*/
let num = 8;
let rad = 10.0;
let shift = rad * 2.0 + rad;
let centerx = shift * (num / 2) as f32;
let centery = shift / 2.0;
let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5;
for j in 0usize..20 {
for i in 0..num {
let x = i as f32 * shift - centerx + offset;
let y = j as f32 * shift + centery + 30.0;
commands.spawn((
TransformBundle::from(Transform::from_xyz(x, y, 0.0)),
RigidBody::Dynamic,
Collider::cuboid(rad, rad),
));
}
offset -= 0.05 * rad * (num as f32 - 1.0);
}
}