Skip to main content
gamedev.net
🔒 Locked

Angle Between Two Points

Started by SquareDanceSteve Sep 22, 2005 at 4:37 PM 11 replies 9.3k views
Original Post
SquareDanceSteve
SquareDanceSteve
I am writing a top down game and i what my charecter to always point at the mouse. How do i calculate the angle between the players position and the mouse?
-----------------www.stevemata.com
Sneftel
Sneftel
Two points do not define an angle. If you mean, what is the orientation of the vector from the player's position to the mouse's position, use atan2().
Trapper Zoid
Trapper Zoid
Quote:
Original post by SquareDanceSteve
I am working in vb6 and i dont not have access to the atan2 function, so I need a more indepth explination


atan2(y,x) is just the arctangent function, but with some extra logic to get the quadrant of the resulting angle right (based on the signs of y and x). If you Google for "atan2" you'll get a description of what it does.

You can write your own using atan pretty easily. I'm sure VB6 has an atan function.
Jan-Lieuwe
Jan-Lieuwe
Calculate the deltas (x1-x2, y1-y2) and then throw them into the atan function.

You get back the angle in radians. Now use the sign of the deltas to figure out which quadrant the angle is (4 portions of 90 degrees).

nhatkthanh
nhatkthanh
asking for angle between 2 points does not make sense, what you're probably trying to do is to get the angle between the current facing vector of the character, and the new vector from the character position to the new point. Then from these 2 vector you can use the dot product to find out the angle in between.
Rob Loach
Rob Loach
This is a clip of code from a class I wrote up for SdlDotNet, in C#:
		/// <summary>		/// Gets and sets the direction of the vector, in radians.		/// </summary>		public double Direction		{			get			{				return Math.Atan2(m_y, m_x);			}			set			{				double length = this.Length;				m_x = Math.Cos(value) * length;				m_y = Math.Sin(value) * length;			}		}		/// <summary>		/// Gets and sets the length of the vector.		/// </summary>		public double Length		{			get			{				return Math.Sqrt(m_x * m_x + m_y * m_y);			}			set			{				double direction = this.Direction;				m_x = Math.Cos(direction) * value;				m_y = Math.Sin(direction) * value;			}		}
Rob Loach [Website] [Projects] [
vNistelrooy
vNistelrooy
Quote:
Original post by nhatkthanh
asking for angle between 2 points does not make sense, what you're probably trying to do is to get the angle between the current facing vector of the character, and the new vector from the character position to the new point. Then from these 2 vector you can use the dot product to find out the angle in between.


The cosine of the angle between the two normalized vectors.
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"
SquareDanceSteve
SquareDanceSteve
i dont remember the vb code script... so ill just paste it in.


Private Function getAngle(ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single)
'Const PI = 3.14159
'Const RAD = PI / 180
'Const DEG = 180 / PI

On Error Resume Next

Dim xLength As Single
Dim yLength As Single

xLength = x1 - x2: yLength = y1 - y2

If xLength = 0 And yLength = 0 Then Exit Function
If xLength = 0 And yLength > 0 Then getAngle = 0
If yLength = 0 And xLength < 0 Then getAngle = 90
If xLength = 0 And yLength < 0 Then getAngle = 180
If yLength = 0 And xLength > 0 Then getAngle = 270


getAngle = Atn(yLength / xLength)
getAngle = getAngle * DEG

If xLength < 0 Then
getAngle = getAngle + 90
ElseIf xLength > 0 Then
getAngle = getAngle + 270
End If

End Function
-----------------www.stevemata.com
SquareDanceSteve
SquareDanceSteve
Here is a better one

Private Function FindAngle(intX1 As Single, intY1 As Single, intX2 As Single, intY2 As Single) As Single

Dim sngXComp As Single
Dim sngYComp As Single

sngXComp = intX2 - intX1
sngYComp = intY1 - intY2
If sngYComp > 0 Then FindAngle = Atn(sngXComp / sngYComp)
If sngYComp < 0 Then FindAngle = Atn(sngXComp / sngYComp) + PI

End Function
-----------------www.stevemata.com
Dmytry
Dmytry
you forgot "if sngYComp == 0 crash and burn".
I would do that:
Private Function FindAngle(intX1 As Single, intY1 As Single, intX2 As Single, intY2 As Single) As SingleDim sngXComp As SingleDim sngYComp As SingleX = intX2 - intX1Y = intY1 - intY2If abs(X)<1E-10 and abs(Y)<1E-10 [something like FindAngle = 0; Exit Function]If abs(Y)>abs(X) Then If Y > 0 Then FindAngle = Atn(X / Y) Else FindAngle = Atn(X / Y) + PI End IfElse If X > 0 Then FindAngle = 0.5*PI - Atn(Y / X) Else FindAngle = -0.5*PI - Atn(Y / X) End IfEnd IfEnd Function

General idea is to find which value is bigger and divide by it. This way, various errors and imprecisions are smaller.
Note: I have no idea about actual VB syntax. Probably it will not compile, you need to edit it.

BTW, it is made to give same direction and origin of angle as your code. It might be not what you really want.[grin]
Also, it probably will give out somewhat messy result in sense that in some quadrants it will give angle and in other it will give angle+2*pi.
To make result be always in range -pi to pi you can add this in the end:
if angle>pi then angle=angle-2*pi else if angle<-pi angle=angle+2*pi

[Edited by - Dmytry on September 26, 2005 2:04:37 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.