@@ -207,47 +207,42 @@ fn setup(mut commands: Commands) {
207207
208208fn 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
0 commit comments