Repository navigation
Expand file tree
/
Copy pathmultiple_contexts.rs
More file actions
151 lines (135 loc) · 4.74 KB
/
Copy pathmultiple_contexts.rs
File metadata and controls
151 lines (135 loc) · 4.74 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
use bevy_rapier2d::prelude::*;
const N_CONTEXTS: usize = 2;
fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
// DOCUSAURUS: MultipleContexts_no_default start
RapierPhysicsPlugin::<NoUserData>::default()
.with_custom_initialization(RapierContextInitialization::NoAutomaticRapierContext),
// DOCUSAURUS: MultipleContexts_no_default stop
RapierDebugRenderPlugin::default(),
))
.add_systems(
Startup,
((create_contexts, setup_physics).chain(), setup_graphics),
)
.add_systems(Update, move_platforms)
.add_systems(PostUpdate, print_contexts.after(PhysicsSet::Writeback))
.add_systems(
Update,
change_context.run_if(input_just_pressed(KeyCode::KeyC)),
)
.run();
}
fn create_contexts(mut commands: Commands) {
for i in 0..N_CONTEXTS {
// DOCUSAURUS: MultipleContexts_new start
let mut context = commands.spawn(RapierContextSimulation::default());
// DOCUSAURUS: MultipleContexts_new stop
context.insert(ContextId(i));
// DOCUSAURUS: MultipleContexts_configuration start
// Each context has its own configuration, e.g., its own gravity.
context.insert(RapierConfiguration {
gravity: Vec2::new(0.0, -9.81 * (i + 1) as f32),
..RapierConfiguration::new(1.0)
});
// DOCUSAURUS: MultipleContexts_configuration stop
if i == 0 {
// DOCUSAURUS: MultipleContexts_custom_default start
context.insert(DefaultRapierContext);
// DOCUSAURUS: MultipleContexts_custom_default stop
}
}
}
fn setup_graphics(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 3.0, -10.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
));
}
#[derive(Component)]
pub struct ContextId(pub usize);
#[derive(Component)]
struct Platform {
starting_y: f32,
}
fn move_platforms(time: Res<Time>, mut query: Query<(&mut Transform, &Platform)>) {
for (mut transform, platform) in query.iter_mut() {
transform.translation.y = platform.starting_y + -time.elapsed_secs().sin();
}
}
// DOCUSAURUS: MultipleContexts_query start
fn print_contexts(contexts: Query<(RapierContext, &ContextId)>) {
for (context, id) in contexts.iter() {
println!(
"Context {} has {} colliders.",
id.0,
context.colliders.colliders.len()
);
}
}
// DOCUSAURUS: MultipleContexts_query stop
// DOCUSAURUS: MultipleContexts_change start
/// Demonstrates how easy it is to move one entity to another context.
fn change_context(
query_context: Query<Entity, With<DefaultRapierContext>>,
mut query_links: Query<(Entity, &mut RapierContextEntityLink)>,
) {
let default_context = query_context.single().unwrap();
for (e, mut link) in query_links.iter_mut() {
if link.0 == default_context {
continue;
}
link.0 = default_context;
println!("changing context of {} for context {}", e, link.0);
}
}
// DOCUSAURUS: MultipleContexts_change stop
pub fn setup_physics(
context: Query<(Entity, &ContextId), With<RapierContextSimulation>>,
mut commands: Commands,
) {
for (context_entity, id) in context.iter() {
let id = id.0;
let color = [
Hsla::hsl(220.0, 1.0, 0.3),
Hsla::hsl(180.0, 1.0, 0.3),
Hsla::hsl(260.0, 1.0, 0.7),
][id % 3];
/*
* Ground
*/
let ground_size = 5.1;
let ground_height = 0.1;
let starting_y = (id as f32) * -0.5 - ground_height;
let mut platforms = commands.spawn((
Transform::from_xyz(0.0, starting_y, 0.0),
Collider::cuboid(ground_size, ground_height),
ColliderDebugColor(color),
RapierContextEntityLink(context_entity),
));
if id == 1 {
platforms.insert(Platform { starting_y });
}
/*
* Create the cube
*/
// DOCUSAURUS: MultipleContexts_link start
commands.spawn((
Transform::from_xyz(0.0, 1.0 + id as f32 * 5.0, 0.0),
RigidBody::Dynamic,
Collider::cuboid(0.5, 0.5),
ColliderDebugColor(color),
// This rigid-body and its collider are simulated by the context `context_entity`.
RapierContextEntityLink(context_entity),
));
// DOCUSAURUS: MultipleContexts_link stop
}
}