Angle Between Two Points

Started by
11 comments, last by Dmytry 18 years, 6 months ago
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
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().
I am working in vb6 and i dont not have access to the atan2 function, so I need a more indepth explination
-----------------www.stevemata.com
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.
I have x1,y1 and x2,y2 how do I turn that into an angle?

Forgive my lack of knowledge
-----------------www.stevemata.com
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).

Merry Christmas from a five-second Google search.
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;			}		}
Rob Loach [Website] [Projects] [Contact]
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!"

This topic is closed to new replies.

Advertisement