forked from avianphysics/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicking_2d_in_perspective.rs
More file actions
182 lines (170 loc) · 5.92 KB
/
picking_2d_in_perspective.rs
File metadata and controls
182 lines (170 loc) · 5.92 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use avian2d::{math::*, prelude::*};
use bevy::prelude::*;
use examples_common_2d::ExampleCommonPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
ExampleCommonPlugin,
PhysicsPlugins::default(),
PhysicsPickingPlugin,
))
.insert_resource(ClearColor(Color::srgb(0.05, 0.05, 0.1)))
.insert_resource(Gravity(Vector::NEG_Y))
.add_systems(Startup, setup)
.add_systems(Update, move_camera)
.run();
}
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
// 2D camera with a perspective projection; completely unheard of!
commands.spawn((
Camera2d,
Projection::Perspective(PerspectiveProjection::default()),
Transform::from_xyz(0.0, 0.0, 1.0), // We have to start slightly away from the Z axis, since now everything is drawing at Z = 0
));
let square_sprite = Sprite {
color: Color::srgb(0.7, 0.7, 0.8),
custom_size: Some(Vec2::splat(0.05)),
..default()
};
// Ceiling
commands.spawn((
square_sprite.clone(),
Transform::from_xyz(0.0, 0.05 * 6.0, 0.0).with_scale(Vec3::new(20.0, 1.0, 1.0)),
RigidBody::Static,
Collider::rectangle(0.05, 0.05),
));
// Floor
commands.spawn((
square_sprite.clone(),
Transform::from_xyz(0.0, -0.05 * 6.0, 0.0).with_scale(Vec3::new(20.0, 1.0, 1.0)),
RigidBody::Static,
Collider::rectangle(0.05, 0.05),
));
// Left wall
commands.spawn((
square_sprite.clone(),
Transform::from_xyz(-0.05 * 9.5, 0.0, 0.0).with_scale(Vec3::new(1.0, 11.0, 1.0)),
RigidBody::Static,
Collider::rectangle(0.05, 0.05),
));
// Right wall
commands.spawn((
square_sprite,
Transform::from_xyz(0.05 * 9.5, 0.0, 0.0).with_scale(Vec3::new(1.0, 11.0, 1.0)),
RigidBody::Static,
Collider::rectangle(0.05, 0.05),
));
for flip in [-1.0, 1.0] {
// Static anchor for the churner
let velocity_anchor = commands
.spawn((
Sprite {
color: Color::srgb(0.5, 0.5, 0.5),
custom_size: Some(Vec2::splat(0.01)),
..default()
},
Transform::from_xyz(-0.3 * flip, -0.15, 0.0),
RigidBody::Static,
))
.id();
// Spinning churner
let velocity_wheel = commands
.spawn((
Sprite {
color: Color::srgb(0.9, 0.3, 0.3),
custom_size: Some(vec2(0.24, 0.02)),
..default()
},
Transform::from_xyz(-0.3 * flip, -0.15, 0.0),
RigidBody::Dynamic,
Mass(1.0),
AngularInertia(1.0),
SleepingDisabled, // Prevent sleeping so motor can always control it
Collider::rectangle(0.24, 0.02),
))
.id();
// Revolute joint with velocity-controlled motor
// Default anchors are at body centers (Vector::ZERO)
commands.spawn((
RevoluteJoint::new(velocity_anchor, velocity_wheel).with_motor(AngularMotor {
target_velocity: 5.0 * flip.adjust_precision(),
max_torque: 1000.0,
motor_model: MotorModel::AccelerationBased {
stiffness: 0.0,
damping: 1.0,
},
..default()
}),
));
}
let circle = Circle::new(0.0075);
let collider = circle.collider();
let mesh = meshes.add(circle);
let ball_color = Color::srgb(0.29, 0.33, 0.64);
for x in -12i32..=12 {
for y in -1_i32..=8 {
commands
.spawn((
Mesh2d(mesh.clone()),
MeshMaterial2d(materials.add(ball_color)),
Transform::from_xyz(x as f32 * 0.025, y as f32 * 0.025, 0.0),
collider.clone(),
RigidBody::Dynamic,
Friction::new(0.1),
))
.observe(
|over: On<Pointer<Over>>,
mut materials: ResMut<Assets<ColorMaterial>>,
balls: Query<&MeshMaterial2d<ColorMaterial>>| {
materials
.get_mut(balls.get(over.entity).unwrap())
.unwrap()
.color = Color::WHITE;
},
)
.observe(
move |out: On<Pointer<Out>>,
mut materials: ResMut<Assets<ColorMaterial>>,
balls: Query<&MeshMaterial2d<ColorMaterial>>| {
materials
.get_mut(balls.get(out.entity).unwrap())
.unwrap()
.color = ball_color;
},
);
}
}
}
// TODO: if anyone would like to improve this example, a freecam could be fun to mess around with :D
fn move_camera(
mut camera: Single<&mut Transform, With<Camera>>,
input: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
const MOVE_SCALE: f32 = 1.0;
let mut delta = Vec3::ZERO;
if input.pressed(KeyCode::KeyW) {
delta.z -= time.delta_secs() * MOVE_SCALE;
}
if input.pressed(KeyCode::KeyS) {
delta.z += time.delta_secs() * MOVE_SCALE;
}
if input.pressed(KeyCode::KeyA) {
delta.x -= time.delta_secs() * MOVE_SCALE;
}
if input.pressed(KeyCode::KeyD) {
delta.x += time.delta_secs() * MOVE_SCALE;
}
if input.pressed(KeyCode::ShiftLeft) {
delta.y -= time.delta_secs() * MOVE_SCALE;
}
if input.pressed(KeyCode::Space) {
delta.y += time.delta_secs() * MOVE_SCALE;
}
camera.translation += delta;
}