2 Replacing % with fmod
source | link

It looks like you want to compute the signed difference between your two angles, taking into account that angles "wrap around," so the fastest way to get from +170 to -170 is to keep increasing, even though on a number line you'd have to go the other way and decrease.

Try this trick:

  float angleDifference = fmod(destination - current + 900f) %, 360f) - 180f;

This gives us the signed difference between two angles in the range [-360, 360], correctly handling the rotation direction across the wrap-around point.

  current += Clampclamp(angleDifference, -1f * turnRate, 1f * turnRate);

  current = fmod(current %, 360f;360f); // Keep current from wandering too far afield.

How this works is the %fmod or modulo operator (also represented by a % for integers or in other languages) "wraps" the number back to 0 when it goes above 360, or below -360. Adding 900 (2.5 * 360) keeps the intermediate math positive for our given input range, and we subtract the 0.5 * 360 at the end so that clockwise rotations come out negative.

It looks like you want to compute the signed difference between your two angles, taking into account that angles "wrap around," so the fastest way to get from +170 to -170 is to keep increasing, even though on a number line you'd have to go the other way and decrease.

Try this trick:

  float angleDifference = (destination - current + 900f) % 360f - 180f;

This gives us the signed difference between two angles in the range [-360, 360], correctly handling the rotation direction across the wrap-around point.

  current += Clamp(angleDifference, -1f * turnRate, 1f * turnRate);

  current = current % 360f; // Keep current from wandering too far afield.

How this works is the % or modulo operator "wraps" the number back to 0 when it goes above 360, or below -360. Adding 900 (2.5 * 360) keeps the intermediate math positive for our given input range, and we subtract the 0.5 * 360 at the end so that clockwise rotations come out negative.

It looks like you want to compute the signed difference between your two angles, taking into account that angles "wrap around," so the fastest way to get from +170 to -170 is to keep increasing, even though on a number line you'd have to go the other way and decrease.

Try this trick:

  float angleDifference = fmod(destination - current + 900f, 360f) - 180f;

This gives us the signed difference between two angles in the range [-360, 360], correctly handling the rotation direction across the wrap-around point.

  current += clamp(angleDifference, -1f * turnRate, 1f * turnRate);

  current = fmod(current, 360f); // Keep current from wandering too far afield.

How this works is the fmod or modulo operator (also represented by a % for integers or in other languages) "wraps" the number back to 0 when it goes above 360, or below -360. Adding 900 (2.5 * 360) keeps the intermediate math positive for our given input range, and we subtract the 0.5 * 360 at the end so that clockwise rotations come out negative.

1
source | link

It looks like you want to compute the signed difference between your two angles, taking into account that angles "wrap around," so the fastest way to get from +170 to -170 is to keep increasing, even though on a number line you'd have to go the other way and decrease.

Try this trick:

  float angleDifference = (destination - current + 900f) % 360f - 180f;

This gives us the signed difference between two angles in the range [-360, 360], correctly handling the rotation direction across the wrap-around point.

  current += Clamp(angleDifference, -1f * turnRate, 1f * turnRate);

  current = current % 360f; // Keep current from wandering too far afield.

How this works is the % or modulo operator "wraps" the number back to 0 when it goes above 360, or below -360. Adding 900 (2.5 * 360) keeps the intermediate math positive for our given input range, and we subtract the 0.5 * 360 at the end so that clockwise rotations come out negative.