Skip to content

Commit bebd122

Browse files
committed
Add angle rounding for heatseeker
1 parent 21370de commit bebd122

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/simulation/ball.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ impl Ball {
263263
.pitch
264264
.clamp(-heatseeker::MAX_TURN_PITCH, heatseeker::MAX_TURN_PITCH);
265265

266+
new_angle = new_angle.round();
267+
266268
// Determine new interpolated speed
267269
let current_state = ((vel - heatseeker::INITIAL_TARGET_SPEED)
268270
/ heatseeker::TARGET_SPEED_INCREMENT)
@@ -273,7 +275,6 @@ impl Ball {
273275
let new_speed = vel + ((target_speed - vel) * heatseeker::SPEED_BLEND);
274276

275277
// Update velocity
276-
// dbg!(new_angle);
277278
self.velocity = new_angle.get_forward_vec() * new_speed;
278279
}
279280

src/simulation/geometry.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ impl Angle {
6161
delta.normalize_fix();
6262
delta
6363
}
64+
65+
#[allow(clippy::cast_precision_loss)]
66+
#[allow(clippy::cast_possible_truncation)]
67+
#[must_use]
68+
pub fn round(self) -> Self {
69+
const TO_INTS: f32 = (1 << 15) as f32 / PI;
70+
const BACK_TO_RADIANS: f32 = (1. / TO_INTS) * 4.;
71+
const ROUNDING_MASK: i32 = 0x4000 - 1;
72+
73+
let r_yaw = ((self.yaw * TO_INTS) as i32 >> 2) & ROUNDING_MASK;
74+
let r_pitch = ((self.pitch * TO_INTS) as i32 >> 2) & ROUNDING_MASK;
75+
76+
Self {
77+
yaw: r_yaw as f32 * BACK_TO_RADIANS,
78+
pitch: r_pitch as f32 * BACK_TO_RADIANS,
79+
}
80+
}
6481
}
6582

6683
#[cfg(feature = "heatseeker")]

0 commit comments

Comments
 (0)