angle help please

BlitzMax Forums/BlitzMax Programming/angle help please

slenkar(Posted 2008) [#1]
I need to know the angle between a car facing at a certain angle and another car(doesnt matter what direction the second car is facing)

So for example If I have a car at 2x,3y
and another car at 1x,1y

car 1 is facing at a 90 degree angle.

so how far does car 1 have to turn to face car 2

I tried using some vector modules in the code archives but the results always turn out wrong


Gabriel(Posted 2008) [#2]
The Angle Between Two objects is calculated with

ATan2(TargetY - SourceY, TargetX - SourceX)

So find the difference by subtracting from the angle of the source car. But bear in mind that you'll want to keep your angle difference between 180 and -180 in order to prevent from going the long way instead of the short way. So if it's higher than 180 or lower than -180 then add/subtract 360 to ensure that it isn't.


slenkar(Posted 2008) [#3]
thanks, that helped me do it,
I used ABS to get the angle positive all the time


Gabriel(Posted 2008) [#4]
Glad it helped you, but if the angle is always positive, when you end up with an angle difference of greater than 180, aren't you going to be going the wrong way? Or do you need the car to always turn in the same direction, regardless of which way is nearest?


slenkar(Posted 2008) [#5]
oh yeah I always make sure to keep track of whether it is positive or not before I use ABS.
One thing that confused me, is that 0 angle is pointing to the right, whereas I assumed zero was pointing up the screen.


Pineapple(Posted 2008) [#6]
Don't use Abs for angles. If you have an angle -90, that's actually 270. Using Abs, you end up with 90.

Instead, use Mod 360. (angle = angle Mod 360)


slenkar(Posted 2008) [#7]
yeah true but i just wanted to know how many degrees to turn


Warpy(Posted 2008) [#8]
Function AngleToTurn#(an1#,x1#,y1#,x2#,y2#)
	an2#=ATan2(y2-y1,x2-x1)
	
	an2=(an2-an1) Mod 360
	
	If an2<-180 Then an2:+360
	If an2>180 Then an2:-360

	Return an2
End Function