-
-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathchange_contexts3.rs
More file actions
126 lines (110 loc) · 3.64 KB
/
change_contexts3.rs
File metadata and controls
126 lines (110 loc) · 3.64 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
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
const N_CONTEXTS: usize = 5;
const WORLD_CHANGE_DELAY_SEC: f32 = 3.0;
#[derive(Component)]
/// Denotes which object(s) to change the world of
struct ChangeWorld;
#[derive(Resource, Debug)]
struct Contexts(Vec<Entity>);
fn main() {
App::new()
.insert_resource(ClearColor(Color::linear_rgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (setup_graphics, setup_physics))
.add_systems(Update, change_world)
.run();
}
fn change_world(
mut query: Query<&mut RapierContextEntityLink, With<ChangeWorld>>,
time: Res<Time>,
worlds: Res<Contexts>,
) {
for mut context_entity_link in query.iter_mut() {
let idx = (worlds.0.len() - 1).min((time.elapsed_secs() / WORLD_CHANGE_DELAY_SEC) as usize);
// Prevent needless change detection
if context_entity_link.0 != worlds.0[idx] {
context_entity_link.0 = worlds.0[idx];
info!(
"Changing context to {:?} ({}/{N_CONTEXTS})",
context_entity_link.0,
idx + 1
)
}
}
}
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),
));
}
pub fn setup_physics(
mut commands: Commands,
q_default_context: Query<Entity, With<DefaultRapierContext>>,
) {
let default_context = q_default_context
.single()
.expect("This will automatically be created");
let mut contexts = Contexts(vec![default_context]);
for i in 1..N_CONTEXTS {
// Each context can have its own rapier configuration
let mut config = RapierConfiguration::new(1.0);
if i + 1 == N_CONTEXTS {
info!("The last context will have opposite gravity");
config.gravity = -config.gravity;
}
let context_ent = commands
.spawn((RapierContextSimulation::default(), config))
.id();
contexts.0.push(context_ent);
}
for (i, &world_ent) in contexts.0.iter().enumerate() {
let color = [
Color::hsl(220.0, 1.0, 0.3),
Color::hsl(180.0, 1.0, 0.3),
Color::hsl(260.0, 1.0, 0.7),
][i % 3];
/*
* Ground
*/
let ground_size = 5.1;
let ground_height = 0.1;
commands.spawn((
Transform::from_xyz(0.0, (i as f32) * -0.5 - ground_height, 0.0),
Collider::cuboid(ground_size, ground_height, ground_size),
ColliderDebugColor(color.into()),
RigidBody::Fixed,
RapierContextEntityLink(world_ent),
));
}
/*
* Create the cube
*
* The child is just there to show that physics world changes will also change the children.
*/
commands
.spawn((
// This will spawn in the default world, since no RapierContextEntityLink was added
Transform::from_xyz(0.0, 3.0, 0.0),
RigidBody::Dynamic,
ChangeWorld,
))
.with_children(|p| {
p.spawn((
Transform::from_xyz(0.0, 0.0, 0.0),
Collider::cuboid(0.5, 0.5, 0.5),
ColliderDebugColor(Color::hsl(260.0, 1.0, 0.7).into()),
));
});
info!("Spawning cube in default world (1/{N_CONTEXTS})");
commands.insert_resource(contexts);
}