c# - Smallest difference between two angles? -
c# - Smallest difference between two angles? -
i'm trying calculate smallest difference between 2 angles.
this current code (a slight variation of found online):
float a1 = mathhelper.todegrees(rot); float a2 = mathhelper.todegrees(m_ftargetrot); float dif = (float)(math.abs(a1 - a2); if (dif > 180) dif = 360 - dif; dif = mathhelper.toradians(dif);
it works fine except in cases @ border of circle. illustration if current angle 355 , target angle 5 calculates difference -350 rather 10 since 365 degrees equal 5 degrees.
any ideas on can create work?
you had it. take dif modulus 360 before checking see if greater 180:
float a1 = mathhelper.todegrees(rot); float a2 = mathhelper.todegrees(m_ftargetrot); float dif = (float)math.abs(a1 - a2) % 360; if (dif > 180) dif = 360 - dif; dif = mathhelper.toradians(dif);
edit: @andrew russell made great point in comments question , solution below takes advantage of mathhelper.wrapangle method suggested:
diff = math.abs(mathhelper.wrapangle(a2 - a1));
c# math xna
Comments
Post a Comment