Game Development Community

Math question

by Teromous · in Torque Game Builder · 08/23/2009 (2:50 pm) · 3 replies

First of all I'd just like to mention that I'm not very good at math, so please excuse me if this question has a simple answer. I would like to take two objects and have the angle between them returned in a 0-360 degree value. So basically if the target is north of the object it = 0, east = 90, south = 180, and west = 270. My goal is to pie out the degrees into 8 directions, so north would be between 337.5 and 22.5, northeast is between 22.5 and 67.5, and so on. My problem is this:

function SeekingBehavior::trackPlayer(%this, %player)
{
   %vector = t2dVectorSub(%player.getPosition(), %this.owner.getPosition());
   %track = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90;
   echo(%track);
}

Using this math:
North: 0
Northeast: 45
East: 90
Southeast: 135
South: 180
Southwest: 225
West: 270 (could also be -90 but I haven't tested it at perfect west)
Northwest: -45 <-- this is not supposed to be negative

Anything from West to North is around -90 to -1 instead of being 270 to 360.

So I'm hoping that you math gurus can shed some light on my problem so I can get to fixing it.



#1
08/23/2009 (9:25 pm)
Could you add this line under "%track = mRadToDeg...":

if( %track < 0 ) %track += 360;

?
#2
08/23/2009 (9:28 pm)
Also, as I thought, there already exists a function that does your first 2 lines of code called t2dAngleToPoint.

Later!
#3
08/23/2009 (10:44 pm)
Ah cool! Thanks for the heads up on the t2dAngleToPoint.