diff --git a/code/playerman/playercontrol.cpp b/code/playerman/playercontrol.cpp index 5d9894964be..05d85249ab2 100644 --- a/code/playerman/playercontrol.cpp +++ b/code/playerman/playercontrol.cpp @@ -417,6 +417,15 @@ void do_view_track_target() chase_slew_angles.h = forward_angles.h - view_angles.h; chase_slew_angles.p = -(forward_angles.p - view_angles.p); + // Do over-the-top correction. + // Headings are extracted with atan2, so each one lies in (-PI, PI] and their difference can be + // nearly a full circle in either direction. Without wrapping it back into (-PI, PI], a target just + // past the left shoulder reads as being almost all the way around to the right instead. + if (chase_slew_angles.h > PI) + chase_slew_angles.h -= PI2; + else if (chase_slew_angles.h < -PI) + chase_slew_angles.h += PI2; + // the gimbal limits of the player's virtual neck. // These nested ifs prevent the player from looking up and // down beyond 90 degree angles.