From ccdc4fe6826ef87ad2343cce852fd119b128a5c8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 14 Oct 2026 18:58:22 +0200 Subject: [PATCH] Controllers: rumble Short, light pulses through gilrs force feedback, on by default (Vibration in horizon-controller.txt): damage taken (stronger with the damage, from any source, here or on a server), a block broken, an explosion nearby (fading over four times its radius), a totem of undying. Only the pad that is playing vibrates; macOS has no force feedback in gilrs and stays still. pad status reports the last vibration asked for. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/explosion.rs | 9 ++ crates/horizon-client/src/game/pad.rs | 74 +++++++++++++++- crates/horizon-client/src/game/totem.rs | 1 + crates/horizon-client/src/gamepad.rs | 94 +++++++++++++++++++++ 5 files changed, 177 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index c5fea0539..6fbabb375 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1996,6 +1996,7 @@ impl Game<'_> { self.with_player_actor(|game| game.break_block_as_player(pos)) } fn break_block_as_player(&mut self, pos: [i32; 3]) -> Result<()> { + self.pad_rumble(horizon_client::gamepad::Rumble::BlockBroken); // ServerPlayerGameMode.destroyBlock. let state = self.world.block(pos[0], pos[1], pos[2]); let facts = self.res.facts.get(state).clone(); diff --git a/crates/horizon-client/src/game/explosion.rs b/crates/horizon-client/src/game/explosion.rs index 9aaafd045..1332ad51d 100644 --- a/crates/horizon-client/src/game/explosion.rs +++ b/crates/horizon-client/src/game/explosion.rs @@ -335,6 +335,15 @@ impl Game<'_> { interaction: BlockInteraction, fire: bool, ) -> Result<()> { + if let Some(p) = &self.player { + let d = [ + p.position.x - center[0], + p.position.y - center[1], + p.position.z - center[2], + ]; + let distance = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt(); + self.pad_rumble(horizon_client::gamepad::Rumble::Explosion { distance, radius }); + } let mut targets = self.exploded_positions(center, radius); // Explosion.explode: the blast is heard. let ctx = horizon_client::sculk::Context::default(); diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 1251eab25..1c472dc31 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -6,12 +6,12 @@ use super::{Game, Screen}; use horizon_client::{ Result, - gamepad::{self, Button, Edges, GlyphStyle, PadState}, + gamepad::{self, Button, Edges, GlyphStyle, PadState, Rumble}, keybinds::{KeyBinds, key_code, mouse_button}, mouse::Rotation, }; use serde_json::{Value, json}; -use std::time::Instant; +use std::time::{Duration, Instant}; use winit::{event_loop::ActiveEventLoop, keyboard::KeyCode}; pub(super) const HELP: &[&str] = &[ @@ -48,6 +48,12 @@ pub(super) struct Pads { pub(super) style: GlyphStyle, /// The cursor the pad drives in menus. pub(super) menu: super::pad_menu::MenuCursor, + /// Vibrations playing (an effect stops when dropped) and when they end. + rumbles: Vec<(gilrs::ff::Effect, Instant)>, + /// The last vibration asked for, for `pad status`. + last_rumble: Option<(&'static str, gamepad::Pulse)>, + /// The player's health at the last poll (a drop is damage taken). + last_health: Option, } fn gilrs_button(button: Button) -> gilrs::Button { @@ -181,6 +187,16 @@ impl Game<'_> { self.pad.in_use = true; } self.pad.state = state; + // Damage from any source, here or on a server. + let health = self.player.as_ref().map(|p| p.health.health()); + if let (Some(before), Some(now)) = (self.pad.last_health, health) + && now < before + { + self.pad_rumble(Rumble::Hurt(before - now)); + } + self.pad.last_health = health; + let now = Instant::now(); + self.pad.rumbles.retain(|(_, end)| *end > now); let ticking = self.title.is_none() && self.screen == Screen::Playing && !self.pause; for button in std::mem::take(&mut self.pad.late_releases) { if !ticking || self.pad.pressed_at[button as usize] != Some(self.ticks) { @@ -359,6 +375,57 @@ impl Game<'_> { self.pitch = rotation.x_rot; } + /// Vibrate the active pad, when vibration is on and the pad is the one + /// playing (a pad lying idle while the mouse plays stays still). + /// Platforms without force feedback (gilrs on macOS) skip it. + pub(super) fn pad_rumble(&mut self, rumble: Rumble) { + let settings = &self.settings.controller; + if !settings.enabled || !settings.vibration || !self.pad.in_use { + return; + } + let Some(pulse) = rumble.pulse() else { + return; + }; + self.pad.last_rumble = Some((rumble.name(), pulse)); + let (Some(gilrs), Some(id)) = (self.pad.gilrs.as_mut(), self.pad.active) else { + return; + }; + if !gilrs + .connected_gamepad(id) + .is_some_and(|g| g.is_ff_supported()) + { + return; + } + use gilrs::ff::{BaseEffect, BaseEffectType, EffectBuilder, Repeat, Replay, Ticks}; + let length = Ticks::from_ms(pulse.millis); + let motor = |kind| BaseEffect { + kind, + scheduling: Replay { + play_for: length, + ..Default::default() + }, + envelope: Default::default(), + }; + let magnitude = |v: f32| (v.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16; + let effect = EffectBuilder::new() + .add_effect(motor(BaseEffectType::Strong { + magnitude: magnitude(pulse.strong), + })) + .add_effect(motor(BaseEffectType::Weak { + magnitude: magnitude(pulse.weak), + })) + .repeat(Repeat::For(length)) + .gamepads(&[id]) + .finish(gilrs); + match effect.and_then(|e| e.play().map(|()| e)) { + Ok(effect) => { + let end = Instant::now() + Duration::from_millis(u64::from(pulse.millis) + 50); + self.pad.rumbles.push((effect, end)); + } + Err(e) => log::debug!("Horizon: controller vibration: {e}"), + } + } + /// `horizon-ctl pad ...`. pub(super) fn pad_command(&mut self, words: &[&str]) -> Result { let arg = |i: usize| -> Result<&str> { @@ -400,6 +467,9 @@ impl Game<'_> { "backend": self.pad.gilrs.is_some(), "controllers": pads, "glyphs": self.pad.style.name(), + "last_rumble": self.pad.last_rumble.map(|(why, p)| json!({ + "reason": why, "strong": p.strong, "weak": p.weak, "ms": p.millis, + })), "in_use": self.pad.in_use, "held": held, "left": s.left, diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs index 257c65db2..805360c8d 100644 --- a/crates/horizon-client/src/game/totem.rs +++ b/crates/horizon-client/src/game/totem.rs @@ -71,5 +71,6 @@ impl Game<'_> { height, 30, ); + self.pad_rumble(horizon_client::gamepad::Rumble::Totem); } } diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index 5dcab6f5a..0cfed55e9 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -589,6 +589,69 @@ pub fn nearest(at: (f32, f32), targets: &[(f32, f32)], radius: f32) -> Option &'static str { + match self { + Rumble::Hurt(_) => "hurt", + Rumble::BlockBroken => "block", + Rumble::Explosion { .. } => "explosion", + Rumble::Totem => "totem", + } + } + /// The pulse, or `None` when it is too faint to feel. + pub fn pulse(self) -> Option { + match self { + Rumble::Hurt(amount) if amount > 0.0 => Some(Pulse { + strong: (0.2 + 0.06 * amount).min(0.8), + weak: 0.3, + millis: (150.0 + 20.0 * amount).min(400.0) as u32, + }), + Rumble::Hurt(_) => None, + Rumble::BlockBroken => Some(Pulse { + strong: 0.0, + weak: 0.25, + millis: 60, + }), + Rumble::Explosion { distance, radius } => { + // Felt up to four times the blast radius, fading with distance. + let reach = f64::from(radius.max(1.0)) * 4.0; + let near = (1.0 - distance / reach) as f32; + (near > 0.05).then_some(Pulse { + strong: 0.9 * near, + weak: 0.6 * near, + millis: 350, + }) + } + Rumble::Totem => Some(Pulse { + strong: 0.7, + weak: 0.7, + millis: 500, + }), + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -758,6 +821,37 @@ mod tests { assert_eq!(action_caption("key.jump"), "key.jump"); } + #[test] + fn rumble_pulses() { + let small = Rumble::Hurt(1.0).pulse().unwrap(); + let big = Rumble::Hurt(10.0).pulse().unwrap(); + assert!(big.strong > small.strong && big.millis > small.millis); + assert!(Rumble::Hurt(100.0).pulse().unwrap().strong <= 0.8); + assert_eq!(Rumble::Hurt(0.0).pulse(), None); + let near = Rumble::Explosion { + distance: 2.0, + radius: 4.0, + } + .pulse() + .unwrap(); + let far = Rumble::Explosion { + distance: 12.0, + radius: 4.0, + } + .pulse() + .unwrap(); + assert!(near.strong > far.strong); + assert_eq!( + Rumble::Explosion { + distance: 20.0, + radius: 4.0 + } + .pulse(), + None + ); + assert_eq!(Rumble::BlockBroken.pulse().unwrap().strong, 0.0); + } + #[test] fn hotbar_wraps_and_glyphs_follow_the_vendor() { assert_eq!(hotbar_step(8, true), 0);