Advertisement

Angle between two 2D vectors

Started by May 24, 2002 01:35 AM
18 comments, last by AdmiralBinary 22 years, 8 months ago
OK, I''ve done ~4 hrs of research on this and still have no luck I have two 2D vectors and I want to find the angle from vector A to vector B (in degrees). Could anyone help me with this (source code would be a very big help)? ---------------------------------------------------------- "Go out with me you will." - Young Yoda''s favourite pickup line
If you can make a triangle out of the vectors you can use trigonometry.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
Sorry, but you''re dealing with a total dummy when it comes to geometry Can you be a bit more specific plz?

----------------------------------------------------------
"Go out with me you will." - Young Yoda''s favourite pickup line
Okay, you have two vectors. Think of these as 2 lines that come out of the origin (0, 0).

Think of those 2 lines as having a horizontal and vertical component (x & y). You''ve got a right-angled triangle right for each line there. Something like...

   /|  / |y /  |/A__|  x 


Use the inverse tan function (atan I think) to calculate angle A for each line. Now find the difference between each line''s A angle and you''re set.

tan A = (y / x)
A = atan (y / x)

Google is good for this kinda thing.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Thanx for the help, siaspete.

quote:
Google is good for this kinda thing.

Like I said, after 4 hrs of googling, asking on Gamdev was my last resort.

I still have a few problems, so if anyone has any code they can show me, it would be much appreciated (It''s for SS2, BTW)

----------------------------------------------------------
"Go out with me you will." - Young Yoda''s favourite pickup line
The angle between vectors u and v in any number of dimensions is:

cos-1 (u.v / (magnitude(u) * magnitude(v)))

If you know that u and v are normal vectors, then you can of course simplify it
Advertisement
the angle (X) between two vectors a and b in 2d is given by:

a.b = (x1 * x2) + (y1 * y2) = |a||b|cosX
(as in the previous post)

where |a| = sqrt(x1^2 + y1^2)
and where x1 and y1 are the x and y displacements of a. (similarly for b as well with displacements x2 and y2).

hope that helps
Thanx very much Beer Hunter and AP Just one question: What is cos-1 (in BH''s formula)? I''m guessing it''s acos, but I just want to make sure...

----------------------------------------------------------
"Go out with me you will." - Young Yoda''s favourite pickup line
Yay! Dot-Product. (We''ve just covered it recently in maths)
Yep, cos-1 == inverse of cos == acos

This topic is closed to new replies.

Advertisement