1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! Rotation in whole mouse counts.
//!
//! A real client turns by `mouse delta × multiplier × 0.15` degrees, so every yaw / pitch
//! change is an integer multiple of the sensitivity's GCD (`multiplier × 0.15`). Anti-cheats
//! (Grim's aim checks) look for that. This module reproduces the client's float arithmetic:
//! the multiplier is computed in `f32` like `GameRenderer.render`, the
//! angles are added in `f64` and stored as `f32` like `Entity.increaseTransforms`.
//!
//! The speed model (by default at most 14° yaw / 10.5° pitch per tick with a carried budget,
//! see [`Rotation::with_turn_rate`]) is the one the reference behaviours were tuned with; it is
//! a behaviour choice, not vanilla (a player's mouse has no such cap).
/// `GameRenderer`: `f = sensitivity * 0.6 + 0.2; f * f * f * 8`, all `f32`.
#[must_use]
pub fn mouse_multiplier(sensitivity: f32) -> f32 {
let s = sensitivity * 0.6 + 0.2;
s * s * s * 8.0
}
/// The smallest rotation step for a sensitivity: `multiplier × 0.15` (as `f32`).
#[must_use]
pub fn mouse_gcd(sensitivity: f32) -> f32 {
mouse_multiplier(sensitivity) * 0.15
}
/// `MathHelper.wrapDegrees`.
#[must_use]
pub fn wrap_degrees(a: f32) -> f32 {
let mut v = a % 360.0;
if v >= 180.0 {
v -= 360.0;
}
if v < -180.0 {
v += 360.0;
}
v
}
/// Java's `Math.round(double)`: `floor(x + 0.5)` (as an integer).
fn java_round(x: f64) -> i64 {
// Out-of-range values saturate like Java's (long) cast.
(x + 0.5).floor() as i64
}
/// Integer mouse counts with a fractional speed budget carried between ticks.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct MouseStep {
budget: f64,
}
impl MouseStep {
/// Mouse counts toward `delta` degrees this tick, at most `degrees_per_tick` (plus the
/// carried fraction).
pub fn counts(&mut self, delta: f32, multiplier: f32, degrees_per_tick: f64) -> i64 {
let step = f64::from(multiplier) * 0.15;
let per_tick = degrees_per_tick / step;
self.budget = (self.budget + per_tick).min(per_tick + 1.0);
let wanted = java_round(f64::from(delta) / step);
let lim = self.budget.trunc() as i64;
let count = wanted.clamp(-lim, lim);
self.budget -= count.unsigned_abs() as f64;
if count == wanted {
self.budget = 0.0;
}
count
}
}
/// Turns toward a target in whole mouse counts.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rotation {
sensitivity: f32,
yaw_step: MouseStep,
pitch_step: MouseStep,
target_yaw: f32,
target_pitch: f32,
max_yaw: f64,
max_pitch: f64,
}
impl Rotation {
/// Default maximum yaw change per tick (degrees).
pub const MAX_YAW_PER_TICK: f64 = 14.0;
/// Default maximum pitch change per tick (degrees).
pub const MAX_PITCH_PER_TICK: f64 = 10.5;
/// A controller for an options.txt mouse sensitivity (0 … 1; vanilla default 0.5).
#[must_use]
pub fn new(sensitivity: f32) -> Self {
Rotation {
sensitivity,
yaw_step: MouseStep::default(),
pitch_step: MouseStep::default(),
target_yaw: 0.0,
target_pitch: 0.0,
max_yaw: Self::MAX_YAW_PER_TICK,
max_pitch: Self::MAX_PITCH_PER_TICK,
}
}
/// The same controller turning at most `yaw` / `pitch` degrees per tick (defaults
/// [`Rotation::MAX_YAW_PER_TICK`] / [`Rotation::MAX_PITCH_PER_TICK`]); the steps stay whole
/// mouse counts. Values that are not finite and positive keep the default.
#[must_use]
pub fn with_turn_rate(mut self, yaw: f64, pitch: f64) -> Self {
let ok = |v: f64| v.is_finite() && v > 0.0;
self.max_yaw = if ok(yaw) { yaw } else { Self::MAX_YAW_PER_TICK };
self.max_pitch = if ok(pitch) {
pitch
} else {
Self::MAX_PITCH_PER_TICK
};
self
}
/// The turn rate cap per tick: (yaw, pitch) degrees.
#[must_use]
pub fn turn_rate(&self) -> (f64, f64) {
(self.max_yaw, self.max_pitch)
}
/// The sensitivity.
#[must_use]
pub fn sensitivity(&self) -> f32 {
self.sensitivity
}
/// Forgets carried budgets and aims at the current look.
pub fn reset(&mut self, yaw: f32, pitch: f32) {
self.yaw_step = MouseStep::default();
self.pitch_step = MouseStep::default();
self.target_yaw = yaw;
self.target_pitch = pitch;
}
/// Sets the target (pitch clamped to ±89.5).
pub fn set_target(&mut self, yaw: f32, pitch: f32) {
self.target_yaw = yaw;
self.target_pitch = pitch.clamp(-89.5, 89.5);
}
/// The target yaw / pitch.
#[must_use]
pub fn target(&self) -> (f32, f32) {
(self.target_yaw, self.target_pitch)
}
/// One tick: the new (yaw, pitch) from the current one.
///
/// `Entity.increaseTransforms(dx * m, dy * m)`: `yaw = (float)(yaw + dx * 0.15)`,
/// `pitch = (float)(pitch - dy * 0.15)`, pitch clamped to ±90.
pub fn step(&mut self, yaw: f32, pitch: f32) -> (f32, f32) {
let m = mouse_multiplier(self.sensitivity);
let dx = self
.yaw_step
.counts(wrap_degrees(self.target_yaw - yaw), m, self.max_yaw);
let dy = self
.pitch_step
.counts(self.target_pitch - pitch, m, self.max_pitch);
// Mouse counts are small integers; the float conversion is exact.
let fx = dx as f32 * m;
let fy = -(dy as f32) * m;
let new_yaw = (f64::from(yaw) + f64::from(fx) * 0.15) as f32;
let new_pitch = ((f64::from(pitch) - f64::from(fy) * 0.15) as f32).clamp(-90.0, 90.0);
(new_yaw, new_pitch)
}
/// Remaining yaw error to the target (degrees, wrapped).
#[must_use]
pub fn yaw_error(&self, yaw: f32) -> f32 {
wrap_degrees(self.target_yaw - yaw).abs()
}
/// Remaining pitch error to the target.
#[must_use]
pub fn pitch_error(&self, pitch: f32) -> f32 {
(self.target_pitch - pitch).abs()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gcd_steps() {
let sens = 0.338_028_2_f32;
let gcd = f64::from(mouse_gcd(sens));
let mut r = Rotation::new(sens);
let (mut yaw, mut pitch) = (12.5_f32, 3.0_f32);
r.set_target(100.0, 40.0);
for _ in 0..40 {
let (ny, np) = r.step(yaw, pitch);
let dy = f64::from(ny) - f64::from(yaw);
let dp = f64::from(np) - f64::from(pitch);
assert!(dy.abs() <= Rotation::MAX_YAW_PER_TICK + gcd + 1e-3);
assert!(dp.abs() <= Rotation::MAX_PITCH_PER_TICK + gcd + 1e-3);
// Every change is a whole number of GCD steps (up to f32 rounding of the sum).
let k = dy / gcd;
assert!(
(k - k.round()).abs() < 1e-2,
"yaw step {dy} is not a multiple of {gcd}"
);
yaw = ny;
pitch = np;
}
assert!(r.yaw_error(yaw) < gcd as f32);
assert!(r.pitch_error(pitch) < gcd as f32);
}
/// A raised turn rate turns farther per tick, still in whole mouse counts.
#[test]
fn turn_rate_cap() {
let sens = 0.5_f32;
let gcd = f64::from(mouse_gcd(sens));
let mut r = Rotation::new(sens).with_turn_rate(45.0, 30.0);
assert_eq!(r.turn_rate(), (45.0, 30.0));
r.set_target(120.0, 80.0);
let (y, p) = r.step(0.0, 0.0);
assert!(
f64::from(y) > 40.0 && f64::from(y) <= 45.0 + gcd + 1e-3,
"{y}"
);
assert!(
f64::from(p) > 26.0 && f64::from(p) <= 30.0 + gcd + 1e-3,
"{p}"
);
let k = f64::from(y) / gcd;
assert!((k - k.round()).abs() < 1e-2);
// Bad values keep the defaults.
let d = Rotation::new(sens).with_turn_rate(f64::NAN, -1.0);
assert_eq!(
d.turn_rate(),
(Rotation::MAX_YAW_PER_TICK, Rotation::MAX_PITCH_PER_TICK)
);
}
#[test]
fn wrap() {
assert_eq!(wrap_degrees(190.0), -170.0);
assert_eq!(wrap_degrees(-190.0), 170.0);
assert_eq!(wrap_degrees(540.0), -180.0);
assert_eq!(wrap_degrees(180.0), -180.0);
}
#[test]
fn java_rounding() {
assert_eq!(java_round(2.5), 3);
assert_eq!(java_round(-2.5), -2);
assert_eq!(java_round(-2.6), -3);
}
/// Values computed with an independent TypeScript (`Math.fround`) port.
#[test]
fn multiplier_is_f32() {
assert!((mouse_multiplier(0.5) - 1.0).abs() < 1e-6);
let m = mouse_multiplier(0.338_028_2);
assert_eq!(m, {
let s: f32 = 0.338_028_2 * 0.6 + 0.2;
s * s * s * 8.0
});
}
}