forked from dimforge/bevy_rapier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_world3.rs
103 lines (92 loc) · 2.93 KB
/
change_world3.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
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
const N_WORLDS: usize = 5;
const WORLD_CHANGE_DELAY_SEC: f32 = 3.0;
#[derive(Component)]
/// Denotes which object(s) to change the world of
struct ChangeWorld;
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,
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 PhysicsWorld, With<ChangeWorld>>, time: Res<Time>) {
for mut bw in query.iter_mut() {
if time.elapsed_seconds() > (bw.world_id.0 as f32 + 1.0) * WORLD_CHANGE_DELAY_SEC {
let new_world_id = bw.world_id.0 + 1;
if new_world_id != N_WORLDS {
println!("Changing world to {new_world_id}.");
bw.world_id = WorldId::new(new_world_id);
}
}
}
}
fn setup_graphics(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 3.0, -10.0)
.looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..Default::default()
});
}
pub fn setup_physics(mut context: ResMut<RapierContext>, mut commands: Commands) {
for _ in 1..N_WORLDS {
context.add_world(RapierWorld::default());
}
for world_id in 0..N_WORLDS {
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),
][world_id % 3];
/*
* Ground
*/
let ground_size = 5.1;
let ground_height = 0.1;
commands.spawn((
TransformBundle::from(Transform::from_xyz(
0.0,
(world_id as f32) * -0.5 - ground_height,
0.0,
)),
Collider::cuboid(ground_size, ground_height, ground_size),
ColliderDebugColor(color),
RigidBody::Fixed,
PhysicsWorld {
world_id: WorldId::new(world_id),
},
));
}
/*
* Create the cube
*
* The child is just there to show that physics world changes will also change the children.
*/
commands
.spawn((
TransformBundle::from(Transform::from_xyz(0.0, 3.0, 0.0)),
RigidBody::Dynamic,
PhysicsWorld {
world_id: DEFAULT_WORLD_ID,
},
ChangeWorld,
))
.with_children(|p| {
p.spawn((
TransformBundle::from_transform(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)),
));
});
}