-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcube_stacks.rs
154 lines (138 loc) · 4.81 KB
/
cube_stacks.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
mod common;
use bevy::prelude::*;
use bevy_mod_physx::prelude::{self as bpx, *};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PhysicsPlugins.set(
PhysicsCore::new().with_pvd()
))
.add_plugins(common::DemoUtils) // optional
.add_systems(Startup, (
spawn_plane,
spawn_stacks,
spawn_dynamic,
spawn_camera_and_light,
))
.run();
}
fn spawn_plane(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut physics: ResMut<Physics>,
mut px_geometries: ResMut<Assets<bpx::Geometry>>,
mut px_materials: ResMut<Assets<bpx::Material>>,
) {
let primitive = Plane3d::default();
let mesh = meshes.add(primitive.mesh().size(500., 500.));
let material = materials.add(Color::srgb(0.3, 0.5, 0.3));
let px_geometry = px_geometries.add(primitive);
let px_material = px_materials.add(bpx::Material::new(&mut physics, 0.5, 0.5, 0.6));
commands.spawn_empty()
.insert((
Mesh3d::from(mesh.clone()),
MeshMaterial3d::from(material.clone()),
))
.insert(bpx::RigidBody::Static)
.insert(bpx::Shape {
geometry: px_geometry,
material: px_material,
..default()
})
.insert(Name::new("Plane"));
}
fn spawn_stacks(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut px_geometries: ResMut<Assets<bpx::Geometry>>,
) {
const WIDTH: f32 = 0.5;
const SIZE: usize = 10;
let primitive = Cuboid::from_size(Vec3::splat(WIDTH));
let mesh = meshes.add(primitive);
let material = materials.add(Color::srgb(0.8, 0.7, 0.6));
let px_geometry = px_geometries.add(primitive);
for i in 0..5 {
commands.spawn((
Name::new(format!("Stack {i}")),
Transform::from_xyz(0., 0., -1.25 * i as f32),
Visibility::default(),
))
.with_children(|builder| {
for i in 0..SIZE {
for j in 0..SIZE-i {
let transform = Transform::from_xyz(
((j * 2) as f32 - (SIZE - i) as f32) / 2. * WIDTH,
(i * 2 + 1) as f32 / 2. * WIDTH,
0.,
);
builder.spawn_empty()
.insert((
Mesh3d::from(mesh.clone()),
MeshMaterial3d::from(material.clone()),
transform,
))
.insert(bpx::RigidBody::Dynamic)
.insert(MassProperties::density(10.))
.insert(bpx::Shape {
geometry: px_geometry.clone(),
..default()
});
}
}
})
.insert(Name::new(format!("Stack {i}")));
}
}
fn spawn_dynamic(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut physics: ResMut<Physics>,
mut px_geometries: ResMut<Assets<bpx::Geometry>>,
mut px_materials: ResMut<Assets<bpx::Material>>,
) {
const RADIUS: f32 = 1.25;
let primitive = Sphere::new(RADIUS);
let mesh = meshes.add(primitive);
let material = materials.add(Color::srgb(0.8, 0.7, 0.6));
let px_geometry = px_geometries.add(primitive);
let px_material = px_materials.add(bpx::Material::new(&mut physics, 0.5, 0.5, 0.6));
let transform = Transform::from_xyz(0., 5., 12.5);
commands.spawn_empty()
.insert((
Mesh3d::from(mesh.clone()),
MeshMaterial3d::from(material.clone()),
transform,
))
.insert(bpx::RigidBody::Dynamic)
.insert(MassProperties::density(10.))
.insert(bpx::Shape {
material: px_material,
geometry: px_geometry,
..default()
})
.insert(Velocity::linear(Vec3::new(0., -6.25, -12.5)))
.insert(Name::new("Ball"));
}
fn spawn_camera_and_light(mut commands: Commands) {
commands
.spawn((
Name::new("Camera"),
Transform::from_xyz(0., 0., 0.),
Visibility::default(),
))
.with_children(|builder| {
builder.spawn((
Camera3d::default(),
Transform::from_xyz(-32.5, 13.6, 18.8).looking_at(Vec3::ZERO, Vec3::Y),
));
});
commands.spawn((
Name::new("Light"),
DirectionalLight::default(),
Transform::from_rotation(Quat::from_euler(EulerRot::XYZ, -1.2, -0.2, 0.)),
));
}