bevy/examples/input/gamepad_rumble.rs
Johan Klokkhammer Helsing a1e442cd2a
Add gamepad rumble support to bevy_input (#8398)
# Objective

Provide the ability to trigger controller rumbling (force-feedback) with
a cross-platform API.

## Solution

This adds the `GamepadRumbleRequest` event to `bevy_input` and adds a
system in `bevy_gilrs` to read them and rumble controllers accordingly.

It's a relatively primitive API with a `duration` in seconds and
`GamepadRumbleIntensity` with values for the weak and strong gamepad
motors. It's is an almost 1-to-1 mapping to platform APIs. Some
platforms refer to these motors as left and right, and low frequency and
high frequency, but by convention, they're usually the same.

I used #3868 as a starting point, updated to main, removed the low-level
gilrs effect API, and moved the requests to `bevy_input` and exposed the
strong and weak intensities.

I intend this to hopefully be a non-controversial cross-platform
starting point we can build upon to eventually support more fine-grained
control (closer to the gilrs effect API)

---

## Changelog

### Added

- Gamepads can now be rumbled by sending the `GamepadRumbleRequest`
event.

---------

Co-authored-by: Nicola Papale <nico@nicopap.ch>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
Co-authored-by: Bruce Reif (Buswolley) <bruce.reif@dynata.com>
2023-04-24 15:28:53 +00:00

78 lines
2.7 KiB
Rust

//! Shows how to trigger force-feedback, making gamepads rumble when buttons are
//! pressed.
use bevy::{
input::gamepad::{GamepadRumbleIntensity, GamepadRumbleRequest},
prelude::*,
utils::Duration,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, gamepad_system)
.run();
}
fn gamepad_system(
gamepads: Res<Gamepads>,
button_inputs: Res<Input<GamepadButton>>,
mut rumble_requests: EventWriter<GamepadRumbleRequest>,
) {
for gamepad in gamepads.iter() {
let button_pressed = |button| {
button_inputs.just_pressed(GamepadButton {
gamepad,
button_type: button,
})
};
if button_pressed(GamepadButtonType::North) {
info!(
"North face button: strong (low-frequency) with low intensity for rumble for 5 seconds. Press multiple times to increase intensity."
);
rumble_requests.send(GamepadRumbleRequest::Add {
gamepad,
intensity: GamepadRumbleIntensity::strong_motor(0.1),
duration: Duration::from_secs(5),
});
}
if button_pressed(GamepadButtonType::East) {
info!("East face button: maximum rumble on both motors for 5 seconds");
rumble_requests.send(GamepadRumbleRequest::Add {
gamepad,
duration: Duration::from_secs(5),
intensity: GamepadRumbleIntensity::MAX,
});
}
if button_pressed(GamepadButtonType::South) {
info!("South face button: low-intensity rumble on the weak motor for 0.5 seconds");
rumble_requests.send(GamepadRumbleRequest::Add {
gamepad,
duration: Duration::from_secs_f32(0.5),
intensity: GamepadRumbleIntensity::weak_motor(0.25),
});
}
if button_pressed(GamepadButtonType::West) {
info!("West face button: custom rumble intensity for 5 second");
rumble_requests.send(GamepadRumbleRequest::Add {
gamepad,
intensity: GamepadRumbleIntensity {
// intensity low-frequency motor, usually on the left-hand side
strong_motor: 0.5,
// intensity of high-frequency motor, usually on the right-hand side
weak_motor: 0.25,
},
duration: Duration::from_secs(5),
});
}
if button_pressed(GamepadButtonType::Start) {
info!("Start button: Interrupt the current rumble");
rumble_requests.send(GamepadRumbleRequest::Stop { gamepad });
}
}
}