[gameprogrammer] Re: Diff Between 2 Angles
- From: Simon <simonmihevc@xxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Thu, 29 Sep 2005 21:58:06 +0200
I've got a really simple problem that I'm almost ashamed to ask about!
I've written a 3D pseudo-realtime strategy game called Lazer Squad 3D
(www.carlylesmith.karoo.net/lazersquad3d/). One problem I always think
I've solved, but then comes back to bite me, is how to get the
difference between two angles. This seems easy on the face of it, but
then along comes two angles and my formula gets it all wrong. (This is
degrees, BTW, not rads).
The main problem is when dealing with negative angles, angles > 360, and
also getting the shortest distance (i.e. not the long way round). For
example, the difference between -359 and 359 should be 2 degrees. The
difference between 90 and -270 should be zero. Etc.. etc.. Can anyone
help please?
Thanks in advance,
Steve Smith
Maybe try it like this:
// convert angles to positive
while (angle1 < 0) angle1 += 360;
while (angle2 < 0) angle2 += 360;
// clamp angles betwen 0 and 360
angle1 = angle1 % 360;
angle2 = angle2 % 360;
// compute angle difference
angle_dif = abs(angle1 - angle2)
// find the smaller angle
if (one_angle_dif > 180) angle_dif = 360 - angle_dif;
Cheers, Simon.
---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- Follow-Ups:
- [gameprogrammer] Re: Diff Between 2 Angles
- From: Olof Bjarnason
- References:
- [gameprogrammer] fgfgjhf
- From: Mohammad Al-Janabi
- [gameprogrammer] Re: fgfgjhf
- From: Bob Pendleton
- [gameprogrammer] Re: fgfgjhf
- From: Mike Gillissie
- [gameprogrammer] Re: fgfgjhf
- From: Bob Pendleton
- [gameprogrammer] Re: fgfgjhf
- From: Chris Nystrom
- [gameprogrammer] My game
- From: Kevin Jenkins
- [gameprogrammer] Re: My game
- From: Mike Gillissie
- [gameprogrammer] Re: My game
- From: Evan Stone
- [gameprogrammer] Re: My game
- From: Kevin Jenkins
- [gameprogrammer] Re: My game
- From: Bob Pendleton
- [gameprogrammer] Re: My game
- From: Kevin Jenkins
- [gameprogrammer] Re: Diff Between 2 Angles
- From: Stephen
Other related posts:
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
- » [gameprogrammer] Re: Diff Between 2 Angles
The main problem is when dealing with negative angles, angles > 360, and also getting the shortest distance (i.e. not the long way round). For example, the difference between -359 and 359 should be 2 degrees. The difference between 90 and -270 should be zero. Etc.. etc.. Can anyone help please?
Thanks in advance,
Steve Smith
Maybe try it like this:
--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html
- [gameprogrammer] Re: Diff Between 2 Angles
- From: Olof Bjarnason
- [gameprogrammer] fgfgjhf
- From: Mohammad Al-Janabi
- [gameprogrammer] Re: fgfgjhf
- From: Bob Pendleton
- [gameprogrammer] Re: fgfgjhf
- From: Mike Gillissie
- [gameprogrammer] Re: fgfgjhf
- From: Bob Pendleton
- [gameprogrammer] Re: fgfgjhf
- From: Chris Nystrom
- [gameprogrammer] My game
- From: Kevin Jenkins
- [gameprogrammer] Re: My game
- From: Mike Gillissie
- [gameprogrammer] Re: My game
- From: Evan Stone
- [gameprogrammer] Re: My game
- From: Kevin Jenkins
- [gameprogrammer] Re: My game
- From: Bob Pendleton
- [gameprogrammer] Re: My game
- From: Kevin Jenkins
- [gameprogrammer] Re: Diff Between 2 Angles
- From: Stephen