Skip to content

Commit 1358746

Browse files
committed
Clean up examples
1 parent a4b1ede commit 1358746

File tree

2 files changed

+56
-66
lines changed

2 files changed

+56
-66
lines changed

crates/avian2d/examples/joint_motors_2d.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -207,47 +207,42 @@ fn setup(mut commands: Commands) {
207207

208208
fn control_motors(
209209
keyboard: Res<ButtonInput<KeyCode>>,
210-
mut velocity_motors: Query<&mut RevoluteJoint, With<VelocityMotorJoint>>,
211-
mut position_motors: Query<
210+
mut velocity_motors: Single<&mut RevoluteJoint, With<VelocityMotorJoint>>,
211+
mut position_motors: Single<
212212
&mut RevoluteJoint,
213213
(With<PositionMotorJoint>, Without<VelocityMotorJoint>),
214214
>,
215-
mut prismatic_motors: Query<&mut PrismaticJoint, With<PrismaticMotorJoint>>,
215+
mut prismatic_motors: Single<&mut PrismaticJoint, With<PrismaticMotorJoint>>,
216216
) {
217-
for mut joint in velocity_motors.iter_mut() {
218-
if keyboard.just_pressed(KeyCode::ArrowUp) {
219-
joint.motor.target_velocity += 1.0;
220-
}
221-
if keyboard.just_pressed(KeyCode::ArrowDown) {
222-
joint.motor.target_velocity -= 1.0;
223-
}
224-
if keyboard.just_pressed(KeyCode::Space) {
225-
joint.motor.enabled = !joint.motor.enabled;
226-
}
217+
// Velocity-controlled revolute joint motor
218+
if keyboard.just_pressed(KeyCode::ArrowUp) {
219+
velocity_motors.motor.target_velocity += 1.0;
220+
}
221+
if keyboard.just_pressed(KeyCode::ArrowDown) {
222+
velocity_motors.motor.target_velocity -= 1.0;
227223
}
228224

229-
for mut joint in position_motors.iter_mut() {
230-
if keyboard.just_pressed(KeyCode::KeyA) {
231-
joint.motor.target_position += 0.5;
232-
}
233-
if keyboard.just_pressed(KeyCode::KeyD) {
234-
joint.motor.target_position -= 0.5;
235-
}
236-
if keyboard.just_pressed(KeyCode::Space) {
237-
joint.motor.enabled = !joint.motor.enabled;
238-
}
225+
// Position-controlled revolute joint motor
226+
if keyboard.just_pressed(KeyCode::KeyA) {
227+
position_motors.motor.target_position += 0.5;
228+
}
229+
if keyboard.just_pressed(KeyCode::KeyD) {
230+
position_motors.motor.target_position -= 0.5;
231+
}
232+
233+
// Position-controlled prismatic joint motor
234+
if keyboard.just_pressed(KeyCode::KeyW) {
235+
prismatic_motors.motor.target_position += 25.0;
236+
}
237+
if keyboard.just_pressed(KeyCode::KeyS) {
238+
prismatic_motors.motor.target_position -= 25.0;
239239
}
240240

241-
for mut joint in prismatic_motors.iter_mut() {
242-
if keyboard.just_pressed(KeyCode::KeyW) {
243-
joint.motor.target_position += 25.0;
244-
}
245-
if keyboard.just_pressed(KeyCode::KeyS) {
246-
joint.motor.target_position -= 25.0;
247-
}
248-
if keyboard.just_pressed(KeyCode::Space) {
249-
joint.motor.enabled = !joint.motor.enabled;
250-
}
241+
// Toggle motors on/off
242+
if keyboard.just_pressed(KeyCode::Space) {
243+
velocity_motors.motor.enabled = !velocity_motors.motor.enabled;
244+
position_motors.motor.enabled = !position_motors.motor.enabled;
245+
prismatic_motors.motor.enabled = !prismatic_motors.motor.enabled;
251246
}
252247
}
253248

crates/avian3d/examples/joint_motors_3d.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -211,47 +211,42 @@ fn setup(
211211

212212
fn control_motors(
213213
keyboard: Res<ButtonInput<KeyCode>>,
214-
mut velocity_motors: Query<&mut RevoluteJoint, With<VelocityMotorJoint>>,
215-
mut position_motors: Query<
214+
mut velocity_motors: Single<&mut RevoluteJoint, With<VelocityMotorJoint>>,
215+
mut position_motors: Single<
216216
&mut RevoluteJoint,
217217
(With<PositionMotorJoint>, Without<VelocityMotorJoint>),
218218
>,
219-
mut prismatic_motors: Query<&mut PrismaticJoint, With<PrismaticMotorJoint>>,
219+
mut prismatic_motors: Single<&mut PrismaticJoint, With<PrismaticMotorJoint>>,
220220
) {
221-
for mut joint in velocity_motors.iter_mut() {
222-
if keyboard.just_pressed(KeyCode::ArrowUp) {
223-
joint.motor.target_velocity += 1.0;
224-
}
225-
if keyboard.just_pressed(KeyCode::ArrowDown) {
226-
joint.motor.target_velocity -= 1.0;
227-
}
228-
if keyboard.just_pressed(KeyCode::Space) {
229-
joint.motor.enabled = !joint.motor.enabled;
230-
}
221+
// Velocity-controlled revolute joint motor
222+
if keyboard.just_pressed(KeyCode::ArrowUp) {
223+
velocity_motors.motor.target_velocity += 1.0;
224+
}
225+
if keyboard.just_pressed(KeyCode::ArrowDown) {
226+
velocity_motors.motor.target_velocity -= 1.0;
231227
}
232228

233-
for mut joint in position_motors.iter_mut() {
234-
if keyboard.just_pressed(KeyCode::KeyA) {
235-
joint.motor.target_position += 0.5;
236-
}
237-
if keyboard.just_pressed(KeyCode::KeyD) {
238-
joint.motor.target_position -= 0.5;
239-
}
240-
if keyboard.just_pressed(KeyCode::Space) {
241-
joint.motor.enabled = !joint.motor.enabled;
242-
}
229+
// Position-controlled revolute joint motor
230+
if keyboard.just_pressed(KeyCode::KeyA) {
231+
position_motors.motor.target_position += 0.5;
232+
}
233+
if keyboard.just_pressed(KeyCode::KeyD) {
234+
position_motors.motor.target_position -= 0.5;
235+
}
236+
237+
// Position-controlled prismatic joint motor
238+
if keyboard.just_pressed(KeyCode::KeyW) {
239+
prismatic_motors.motor.target_position += 25.0;
240+
}
241+
if keyboard.just_pressed(KeyCode::KeyS) {
242+
prismatic_motors.motor.target_position -= 25.0;
243243
}
244244

245-
for mut joint in prismatic_motors.iter_mut() {
246-
if keyboard.just_pressed(KeyCode::KeyW) {
247-
joint.motor.target_position += 0.5;
248-
}
249-
if keyboard.just_pressed(KeyCode::KeyS) {
250-
joint.motor.target_position -= 0.5;
251-
}
252-
if keyboard.just_pressed(KeyCode::Space) {
253-
joint.motor.enabled = !joint.motor.enabled;
254-
}
245+
// Toggle motors on/off
246+
if keyboard.just_pressed(KeyCode::Space) {
247+
velocity_motors.motor.enabled = !velocity_motors.motor.enabled;
248+
position_motors.motor.enabled = !position_motors.motor.enabled;
249+
prismatic_motors.motor.enabled = !prismatic_motors.motor.enabled;
255250
}
256251
}
257252

0 commit comments

Comments
 (0)