Merge pull request #83 from ludovic111/fix/hand-sway-66

Hand sway: the first-person hands lag behind the view, as in vanilla
This commit is contained in:
Ludovic Marie
2026-10-14 15:42:12 +02:00
committed by GitHub
8 changed files with 96 additions and 11 deletions
@@ -5,7 +5,7 @@ use horizon_client::{
Result,
attack_strength::{AttackStrength, HeldStack},
first_person::{
Clicks, Hand, HandHeights, HandWorld, Hit, SwingAnimation, SwingState, UseResult,
Clicks, Hand, HandHeights, HandSway, HandWorld, Hit, SwingAnimation, SwingState, UseResult,
swing_duration,
},
};
@@ -308,6 +308,9 @@ fn run(case: &Value, out: &mut impl Write) -> Result<()> {
let mut strength = AttackStrength::default();
// FirstPersonHandsAndItems' visible items start as ItemStack.EMPTY.
let mut visible = [0usize, 0usize];
// The player's xRot and yRot, and the hands' lag behind them.
let mut rot = [0.0f32; 2];
let mut sway = HandSway::default();
for (s, step) in case["steps"].as_array().unwrap().iter().enumerate() {
probe.step = step;
probe.events.clear();
@@ -327,6 +330,10 @@ fn run(case: &Value, out: &mut impl Write) -> Result<()> {
step["ud"].as_bool().unwrap(),
);
probe.swing.tick();
if let Some(r) = step["rot"].as_array() {
rot = [fl(&r[0]), fl(&r[1])];
}
sway.tick(rot[0], rot[1]);
let main = probe.stack(Hand::Main).clone();
strength.tick(held(&main));
// FirstPersonHandsAndItems.tick: shouldInstantlyReplaceVisibleItem
@@ -363,7 +370,7 @@ fn run(case: &Value, out: &mut impl Write) -> Result<()> {
let d = &clicks.destroying;
writeln!(
out,
"tick {s} {current} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}",
"tick {s} {current} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}",
sw.ticks,
fb(sw.animation),
fb(sw.old_animation),
@@ -379,7 +386,11 @@ fn run(case: &Value, out: &mut impl Write) -> Result<()> {
clicks.right_click_delay,
d.delay,
d.active,
fb(d.progress)
fb(d.progress),
fb(sway.x_bob),
fb(sway.y_bob),
fb(sway.x_bob_o),
fb(sway.y_bob_o)
)?;
}
Ok(())
+29
View File
@@ -192,6 +192,35 @@ impl HandHeights {
}
}
/// `LocalPlayer.xBob` and `yBob`: the hands lag behind the view when it
/// turns. `LocalPlayer.applyInput` eases them halfway to the rotation every
/// tick; `FirstPersonHandsAndItemsRenderer` turns the hands by a tenth of
/// the gap left.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct HandSway {
pub x_bob: f32,
pub y_bob: f32,
pub x_bob_o: f32,
pub y_bob_o: f32,
}
impl HandSway {
/// `LocalPlayer.applyInput` with the player's `getXRot` and `getYRot`.
pub fn tick(&mut self, x_rot: f32, y_rot: f32) {
self.y_bob_o = self.y_bob;
self.x_bob_o = self.x_bob;
self.x_bob += (x_rot - self.x_bob) * 0.5;
self.y_bob += (y_rot - self.y_bob) * 0.5;
}
/// The degrees the hands turn about X, then Y, before either is drawn:
/// `(viewXRot - xBob) * 0.1` and `(viewYRot - yBob) * 0.1`, the bobs
/// interpolated as `extractRenderState` does.
pub fn rotation(&self, partial: f32, view_x_rot: f32, view_y_rot: f32) -> [f32; 2] {
let x_bob = crate::camera::lerp(partial, self.x_bob_o, self.x_bob);
let y_bob = crate::camera::lerp(partial, self.y_bob_o, self.y_bob);
[(view_x_rot - x_bob) * 0.1, (view_y_rot - y_bob) * 0.1]
}
}
/// `HitResult` at the crosshair (`Minecraft.hitResult`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Hit {
+11 -1
View File
@@ -706,6 +706,9 @@ struct Game<'a> {
/// The first-person equip heights and the items the hands show (with
/// the slot each came from).
heights: horizon_client::first_person::HandHeights,
/// The hands' lag behind the view (`LocalPlayer.xBob`, `yBob`); a new
/// LocalPlayer (joining, respawning, changing dimension) starts at 0.
sway: horizon_client::first_person::HandSway,
visible_hands: [(usize, Option<ItemStack>); 2],
/// `Minecraft`'s attack handling, attack clicks since the last tick and
/// the attack strength tickers the hand reads.
@@ -2587,6 +2590,7 @@ impl Game<'_> {
}
self.previous_position = feet;
self.player = Some(player);
self.sway = Default::default();
self.apply_equipment();
self.eye = 1.62;
self.previous_eye = 1.62;
@@ -5652,6 +5656,7 @@ impl Game<'_> {
player.velocity = Vec3::ZERO;
self.previous_position = player.position;
self.player = Some(player);
self.sway = Default::default();
self.apply_equipment();
let at = [f64::from(spot[0]), f64::from(spot[1]), f64::from(spot[2])];
self.audio
@@ -6537,7 +6542,11 @@ impl Game<'_> {
};
let inverse_height = 1.0 - self.heights.interpolated(a)[0];
let view = horizon_client::camera::view_matrix(self.yaw, self.pitch);
let to_world = view.inverse();
// The hand sway: both hands turn by a tenth of the bobs' lag.
let [sway_x, sway_y] = self.sway.rotation(a, self.pitch, self.yaw);
let to_world = view.inverse()
* glam::Mat4::from_rotation_x(sway_x.to_radians())
* glam::Mat4::from_rotation_y(sway_y.to_radians());
let eye = self.eye_position().unwrap_or_default();
let light = self.world.light(
eye[0].floor() as i32,
@@ -8631,6 +8640,7 @@ pub fn run() -> Result<()> {
swing: Default::default(),
swings_started: 0,
heights: Default::default(),
sway: Default::default(),
visible_hands: [(0, None), (0, None)],
clicks: Default::default(),
attack_clicks: 0,
+1
View File
@@ -485,6 +485,7 @@ impl Game<'_> {
player.fall_distance = 0.0;
self.previous_position = player.position;
self.player = Some(player);
self.sway = Default::default();
self.apply_equipment();
let pitch = self.world.random.next_float() * 0.4 + 0.8;
self.audio
+4
View File
@@ -170,6 +170,10 @@ impl Game<'_> {
/// strength and item swap tickers (`Player.tick`), then
/// `FirstPersonHandsAndItems.tick`.
pub(super) fn tick_hand(&mut self) {
// LocalPlayer.applyInput (the camera is the player's own).
if self.player.is_some() {
self.sway.tick(self.pitch, self.yaw);
}
self.swing.tick();
let main = self.inventory[self.selected].clone();
self.hand_strength.tick(main.as_ref().map(|s| HeldStack {
+1
View File
@@ -1569,6 +1569,7 @@ impl Game<'_> {
let mut player = PlayerMotion::new(self.motion, position)?;
player.velocity = delta;
self.player = Some(player);
self.sway = Default::default();
self.eye = 1.62;
self.previous_eye = 1.62;
self.clock.reset();
@@ -50,7 +50,9 @@ import net.minecraft.world.phys.*;
* level, connection and item model resolver are shells answering what these
* read, and the results of using items on blocks, entities and the air are
* scripted. The attack and use part of Minecraft.handleKeybinds and the
* ticker lines of Player.tick are mirrored here in vanilla's order.
* ticker lines of Player.tick are mirrored here in vanilla's order. Steps may
* turn the player; the real LocalPlayer.applyInput moves the hands' xBob and
* yBob after it.
*/
public final class VanillaFirstPersonOracle {
static Object unsafe;
@@ -150,6 +152,8 @@ public final class VanillaFirstPersonOracle {
}
@Override public AttributeMap getAttributes() { return attributes; }
// The shell Minecraft has no game renderer: the player is the camera.
@Override protected boolean isControlledCamera() { return true; }
@Override public boolean isEyeInFluid(TagKey<Fluid> type) { return false; }
@Override public boolean onGround() { return true; }
@Override public boolean isWithinEntityInteractionRange(Entity entity, double buffer) { return inRange; }
@@ -274,6 +278,7 @@ public final class VanillaFirstPersonOracle {
swingState.set(player, new LivingEntity.SwingState());
lastItem.set(player, ItemStack.EMPTY);
hands.set(player, new FirstPersonHandsAndItems());
player.input = new net.minecraft.client.player.ClientInput();
var effects = new HashMap<Holder<MobEffect>, MobEffectInstance>();
if (!c.get("haste").isJsonNull()) {
effects.put(MobEffects.HASTE, new MobEffectInstance(MobEffects.HASTE, 100000, c.get("haste").getAsInt()));
@@ -363,6 +368,13 @@ public final class VanillaFirstPersonOracle {
}
lastItem.set(player, main.copy());
}
// LocalPlayer.aiStep's applyInput, after the step's turn.
if (step.has("rot") && !step.get("rot").isJsonNull()) {
var rot = step.getAsJsonArray("rot");
player.setXRot(Float.intBitsToFloat(Integer.parseUnsignedInt(rot.get(0).getAsString(), 16)));
player.setYRot(Float.intBitsToFloat(Integer.parseUnsignedInt(rot.get(1).getAsString(), 16)));
}
player.applyInput();
var fp = (FirstPersonHandsAndItems) hands.get(player);
fp.tick(player);
for (var ev : events) {
@@ -376,7 +388,8 @@ public final class VanillaFirstPersonOracle {
+ " " + fb(mainHeight.getFloat(fp)) + " " + fb(oMainHeight.getFloat(fp))
+ " " + fb(offHeight.getFloat(fp)) + " " + fb(oOffHeight.getFloat(fp))
+ " " + missTime.getInt(mc) + " " + rightClickDelay.getInt(mc) + " " + destroyDelay.getInt(gameMode)
+ " " + destroying.getBoolean(gameMode) + " " + fb(progress.getFloat(gameMode)));
+ " " + destroying.getBoolean(gameMode) + " " + fb(progress.getFloat(gameMode))
+ " " + fb(player.xBob) + " " + fb(player.yBob) + " " + fb(player.xBobO) + " " + fb(player.yBobO));
}
}
}
+21 -5
View File
@@ -47,9 +47,12 @@ ITEMS = [None, ['minecraft:diamond_sword', 1], ['minecraft:diamond_pickaxe', 1],
TARGETS = [(0, 64, 0), (1, 64, 0), (0, 65, 2)]
def step(hit=('miss',), ac=0, uc=0, ad=False, ud=False, busy=False, using=False, uses=None, shrink=False, set=None):
def step(hit=('miss',), ac=0, uc=0, ad=False, ud=False, busy=False, using=False, uses=None, shrink=False, set=None,
rot=None):
"""rot: the player's (xRot, yRot) from this step on (LocalPlayer.applyInput's hand sway)."""
return dict(hit=list(hit), ac=ac, uc=uc, ad=ad, ud=ud, busy=busy, using=using,
uses=uses or dict(entity='pass', block='pass', item='pass'), shrink=shrink, set=set)
uses=uses or dict(entity='pass', block='pass', item='pass'), shrink=shrink, set=set,
rot=None if rot is None else [fbits(rot[0]), fbits(rot[1])])
def case(steps, creative=False, main=None, off=None, blocks=None, haste=None, fatigue=None):
@@ -120,12 +123,24 @@ def scenarios():
yield case(steps, True, None, blocks={TARGETS[1]: 'minecraft:dirt'}, haste=haste, fatigue=fatigue)
def turning_cases():
"""The hands lag behind the view: turns, snaps, a yaw past 180 and back, pitch to its limits."""
for main in (None, ['minecraft:diamond_sword', 1]):
steps = [step(rot=(0.0, 0.0))]
steps += [step(rot=(0.0, 4.5 * i)) for i in range(1, 40)]
steps += [step()] * 6
steps += [step(rot=(90.0, 179.5)), step(rot=(-90.0, 185.25)), step(rot=(-12.75, 900.0))] + [step()] * 8
steps += [step(rot=(30.0 - i, -720.0 + 33.3 * i), ac=i % 2, ad=True) for i in range(30)] + [step()] * 4
yield case(steps, False, main)
def random_cases(rng):
results = ['success', 'server', 'consume', 'pass', 'fail']
for _ in range(3000):
blocks = {p: rng.choice(list(BLOCKS) + ['minecraft:air']) for p in TARGETS}
steps = []
for _ in range(rng.randrange(5, 80)):
rot = (rng.uniform(-90.0, 90.0), rng.uniform(-1080.0, 1080.0)) if rng.random() < 0.3 else None
hit = rng.choice([('miss',), ('entity', rng.random() < 0.7)] + [block_hit(p, rng.randrange(6)) for p in TARGETS] * 2)
setting = None
if rng.random() < 0.04:
@@ -133,7 +148,7 @@ def random_cases(rng):
steps.append(step(hit, ac=rng.choice([0] * 6 + [1, 1, 2]), uc=rng.choice([0] * 8 + [1, 2]), ad=rng.random() < 0.5,
ud=rng.random() < 0.3, busy=rng.random() < 0.05, using=rng.random() < 0.08,
uses=dict(entity=rng.choice(results), block=rng.choice(results), item=rng.choice(results)),
shrink=rng.random() < 0.3, set=setting))
shrink=rng.random() < 0.3, set=setting, rot=rot))
yield case(steps, rng.random() < 0.4, rng.choice(ITEMS), rng.choice(ITEMS), blocks,
rng.choice([None] * 6 + [0, 1, 3]), rng.choice([None] * 6 + [0, 2]))
@@ -145,7 +160,7 @@ def make_cases(build):
cases.mkdir(exist_ok=True)
count = 0
with path.open('w') as out:
for generator in (scenarios(), random_cases(rng)):
for generator in (scenarios(), turning_cases(), random_cases(rng)):
for c in generator:
text = json.dumps(c, separators=(',', ':'))
out.write(text + '\n')
@@ -163,7 +178,8 @@ def main():
'FirstPersonHandsAndItems.tick and itemUsed, on a LocalPlayer shell with a real Inventory, effects and attribute '
'modifiers, over scripted clicks, held buttons, crosshair targets, item changes and use results. Level effects '
'(particles, game events), packets and the outcome of using items on blocks, entities and the air are scripted '
'or absorbed; every vanilla item plays the swap animation unscaled except spears, which are not scripted.')
'or absorbed; every vanilla item plays the swap animation unscaled except spears, which are not scripted. Steps also '
'turn the player: the real LocalPlayer.applyInput moves xBob and yBob, the hands\' lag behind the view.')
harness.differential(
'rust-first-person-parity', 'VanillaFirstPersonOracle', 'first_person_probe', make_cases, scope,
sources=[lab.ROOT / 'crates/horizon-client/src/first_person.rs', lab.ROOT / 'crates/horizon-client/src/attack_strength.rs',