Jump to content
  • Advertisement
Sign in to follow this  
SquareDanceSteve

Angle Between Two Points

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!