# Angle Between Two Points

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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().

##### Share on other sites
I am working in vb6 and i dont not have access to the atan2 function, so I need a more indepth explination

##### Share on other sites
Quote:
 Original post by SquareDanceSteveI 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.

##### Share on other sites
I have x1,y1 and x2,y2 how do I turn that into an angle?

Forgive my lack of knowledge

##### Share on other sites
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).

##### Share on other sites
Merry Christmas from a five-second Google search.

##### Share on other sites
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.

##### Share on other sites
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;
}
}

##### Share on other sites
Quote:
 Original post by nhatkthanhasking 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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013245
×