Skip to content

Commit

Permalink
Add angle rounding for heatseeker
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Jan 2, 2025
1 parent 21370de commit bebd122
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/simulation/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ impl Ball {
.pitch
.clamp(-heatseeker::MAX_TURN_PITCH, heatseeker::MAX_TURN_PITCH);

new_angle = new_angle.round();

// Determine new interpolated speed
let current_state = ((vel - heatseeker::INITIAL_TARGET_SPEED)
/ heatseeker::TARGET_SPEED_INCREMENT)
Expand All @@ -273,7 +275,6 @@ impl Ball {
let new_speed = vel + ((target_speed - vel) * heatseeker::SPEED_BLEND);

// Update velocity
// dbg!(new_angle);
self.velocity = new_angle.get_forward_vec() * new_speed;
}

Expand Down
17 changes: 17 additions & 0 deletions src/simulation/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ impl Angle {
delta.normalize_fix();
delta
}

#[allow(clippy::cast_precision_loss)]
#[allow(clippy::cast_possible_truncation)]
#[must_use]
pub fn round(self) -> Self {
const TO_INTS: f32 = (1 << 15) as f32 / PI;
const BACK_TO_RADIANS: f32 = (1. / TO_INTS) * 4.;
const ROUNDING_MASK: i32 = 0x4000 - 1;

let r_yaw = ((self.yaw * TO_INTS) as i32 >> 2) & ROUNDING_MASK;
let r_pitch = ((self.pitch * TO_INTS) as i32 >> 2) & ROUNDING_MASK;

Self {
yaw: r_yaw as f32 * BACK_TO_RADIANS,
pitch: r_pitch as f32 * BACK_TO_RADIANS,
}
}
}

#[cfg(feature = "heatseeker")]
Expand Down

0 comments on commit bebd122

Please sign in to comment.