Angle Between Two Points
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?
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().
I am working in vb6 and i dont not have access to the atan2 function, so I need a more indepth explination
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.
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).
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).
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.
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; } }
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement