forked from Jondolf/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
many_shapes.rs
159 lines (148 loc) · 4.9 KB
/
many_shapes.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
155
156
157
158
159
#![allow(clippy::unnecessary_cast)]
use avian2d::{math::*, prelude::*};
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use examples_common_2d::ExampleCommonPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
ExampleCommonPlugin,
// Add physics plugins and specify a units-per-meter scaling factor, 1 meter = 20 pixels.
// The unit allows the engine to tune its parameters for the scale of the world, improving stability.
PhysicsPlugins::default().with_length_unit(20.0),
))
.insert_resource(ClearColor(Color::srgb(0.05, 0.05, 0.1)))
.insert_resource(Gravity(Vector::NEG_Y * 1000.0))
.add_systems(Startup, setup)
.add_systems(Update, movement)
.run();
}
#[derive(Component)]
struct Controllable;
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
commands.spawn(Camera2dBundle::default());
let square_sprite = Sprite {
color: Color::srgb(0.7, 0.7, 0.8),
custom_size: Some(Vec2::splat(50.0)),
..default()
};
// Ceiling
commands.spawn((
SpriteBundle {
sprite: square_sprite.clone(),
transform: Transform::from_xyz(0.0, 50.0 * 6.0, 0.0)
.with_scale(Vec3::new(20.0, 1.0, 1.0)),
..default()
},
RigidBody::Static,
Collider::rectangle(50.0, 50.0),
));
// Floor
commands.spawn((
SpriteBundle {
sprite: square_sprite.clone(),
transform: Transform::from_xyz(0.0, -50.0 * 6.0, 0.0)
.with_scale(Vec3::new(20.0, 1.0, 1.0)),
..default()
},
RigidBody::Static,
Collider::rectangle(50.0, 50.0),
));
// Left wall
commands.spawn((
SpriteBundle {
sprite: square_sprite.clone(),
transform: Transform::from_xyz(-50.0 * 9.5, 0.0, 0.0)
.with_scale(Vec3::new(1.0, 11.0, 1.0)),
..default()
},
RigidBody::Static,
Collider::rectangle(50.0, 50.0),
));
// Right wall
commands.spawn((
SpriteBundle {
sprite: square_sprite,
transform: Transform::from_xyz(50.0 * 9.5, 0.0, 0.0)
.with_scale(Vec3::new(1.0, 11.0, 1.0)),
..default()
},
RigidBody::Static,
Collider::rectangle(50.0, 50.0),
));
let circle = Circle::new(7.5);
let rectangle = Rectangle::new(15.0, 15.0);
let capsule = Capsule2d::new(7.5, 20.0);
let triangle = Triangle2d::new(
Vec2::new(0.0, 10.0),
Vec2::new(-10.0, -10.0),
Vec2::new(10.0, -10.0),
);
let shapes = [
(
circle.collider(),
meshes.add(circle).into(),
materials.add(Color::srgb(0.29, 0.33, 0.64)),
),
(
rectangle.collider(),
meshes.add(rectangle).into(),
materials.add(Color::srgb(0.47, 0.58, 0.8)),
),
(
capsule.collider(),
meshes.add(capsule).into(),
materials.add(Color::srgb(0.63, 0.75, 0.88)),
),
(
triangle.collider(),
meshes.add(triangle).into(),
materials.add(Color::srgb(0.77, 0.87, 0.97)),
),
];
for x in -12_i32..12 {
for y in -8_i32..8 {
let (collider, mesh, material) = shapes[(x + y) as usize % 4].clone();
commands.spawn((
MaterialMesh2dBundle {
mesh,
material,
transform: Transform::from_xyz(x as f32 * 25.0, y as f32 * 25.0, 0.0),
..default()
},
collider,
RigidBody::Dynamic,
Friction::new(0.1),
Controllable,
));
}
}
}
fn movement(
time: Res<Time>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut marbles: Query<&mut LinearVelocity, With<Controllable>>,
) {
// Precision is adjusted so that the example works with
// both the `f32` and `f64` features. Otherwise you don't need this.
let delta_time = time.delta_seconds_f64().adjust_precision();
for mut linear_velocity in &mut marbles {
if keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]) {
// Use a higher acceleration for upwards movement to overcome gravity
linear_velocity.y += 2500.0 * delta_time;
}
if keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]) {
linear_velocity.y -= 500.0 * delta_time;
}
if keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]) {
linear_velocity.x -= 500.0 * delta_time;
}
if keyboard_input.any_pressed([KeyCode::KeyD, KeyCode::ArrowRight]) {
linear_velocity.x += 500.0 * delta_time;
}
}
}