Help - Search - Members - Calendar
Full Version: Help Me With A Code Of Angles - Solved
Game Maker Community > Working with Game Maker > Advanced Users Only
kickboxer89
I am making a personal application similar to Macromedia Flash, or Move Now.
You can make animations with a complex sprite, so you can make it walking, running, punching, dancing, etc.
Between keyframes, the application calculates the tweening.
All here is OK, but I am having problems with that:
for the tweening I calculate the difference of 2 different angles. I use that code. It have to return the minimum distance. For example, I have an angle of 100 degrees, and other one of 130 degrees. The difference can be +30 or -330 degrees, depending of the direction you choose for tweening. The problem is that I don't know the exact formula for calculating that difference angle.
I use this code:
CODE
//Argument0 - Angle from
//Argument1 - Angle to
var _a,_b;
_a=argument1-argument0;
_b=-(359-argument1+argument0);
if _a<0 then _a+=360; else if _a>=360 then _a-=360;
if _b<0 then _b+=360; else if _b>=360 then _b-=360;
if abs(_a)>=abs(_b) then return _b;
else if abs(_a)<abs(_b) then return _a;


The problem appears when the angle from is bigger than the angle to. For example, if I have an angle of 350 and the other one is 100, the difference will be -250 or 110. My code can't calculate that.

Help please sleep.gifU. Thank you.

EDIT: I use degrees from 0 to 359. I know that GM can handle degrees higher than 359 or lower than zero, but I convert it.
kickboxer89
Just solved it. For those who want an answer...
CODE
/*
**  usage:
**      diff = angle_difference(angle1,angle2);
**
**  given:
**      angle1    first direction in degrees, real
**      angle2    second direction in degrees, real
**
**  returns:
**      difference of the given angles in degrees, -180 to 180
**
**  www.planetxot.com
*/
return ((((argument0 - argument1) mod 360) + 540) mod 360) - 180;

From this topic
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.