Finding the angle between two vectors

Started by
26 comments, last by EGD Eric 20 years, 2 months ago
Here's what I'm doing: //Finding the DotProduct float Vector2D::DotProduct(Vector2D &someVector) { return x * someVector.x + y * someVector.y; } float Vector2D::InnerAngle(Vector2D &someVector) { float Angle = 0; //find the dot product of 2 normalized vectors to get //the inner angle Vector2D me_Normalized = FindUnitVector(); someVector.normalize(); Angle = me_Normalized.DotProduct(someVector); //convert to radians, because C math functions //only take radians Angle = Angle * PI /180; Angle = acos(Angle); //convert angle back to degrees Angle = Angle * 180/PI; return Angle; } Edit: God, I'm absent minded today. I forgot to mention what the problem was! Its not returning the right number. Always returns something between between 81 and 89 [edited by - EGD Eric on January 24, 2004 4:40:12 PM] [edited by - EGD Eric on January 24, 2004 5:25:06 PM]
Advertisement
quote:Original post by EGD Eric
Angle = Angle * PI /180;
Angle = acos(Angle);

acos() doesn''t take an angle, it RETURNS an angle. Thus no conversion before the call.
in fact the dot product gives "cos(angle)" so there''s no meaning to convert a cosinus in radian...
I''m just following these steps because a book says that''s how to do it.

It says this:

cos = (A . B ) / (|A| * |B|)

the pipe symbols (|A|) mean length. A . B means the Dot product.

It says that if A and B are normalized (have length of 1) then the equation is simplified:

cos = (A . B) / (1 * 1)

cos = A . B

So from there I should get the cosine or arccosine (cos -1) to get the angle, right? So to do that, I''ve got to call the accos or cos function. But C math functions take radians, not degrees. So I convert to radians to let C math do its thing, then revert it back to degrees, a measurement that I understand.


I was never any good at math, especially trig. Can you tell me in the simplest possible way, what I need to do to get the angle between 2 vectors?
>> cos = (A . B ) / (|A| * |B|)

better: cos a = (A . B ) / (|A| * |B|) where a is your angle.

a = arccos(cos a) because arccos is the inverse function of cos (within limits)

so:
a = arccos(cos a) = arccos [ (A . B ) / (|A| * |B|) ]

as you already mentioned a will be in radians, so you might want to convert it to degrees of angle if you feel you need that.

One other thing: Consider overloading the operator* with the dot product so Vector1.DotProduct(Vector2) => Vector1*Vector2. It doesn´t have any impact on your code but imho it looks better (since it´s closer to the mathematical expressions).
One more thing. Get used to working with radians instead. It simplifies the code and makes it less prone to errors like the above.
float angle(float ax, float ay, float bx, float by){ float result; float dx=bx-ax; float dy=by-ay;  result = (atan2(dx , dy)) * 180 / 3.14; result = result - 90; if (result < 0) {  result = result + 360; } return result;}


That''s what I use.
But I don't understand, Atheist, from what you and the book are telling me, what I'm doing should be working.

Sure, I'll overload that operator. Might as well.

You're right, anonymous, Radians are better. Problem is, I'm making this game in OpenGL, which uses degrees.

Edit: I tried using your code, Jack, it doesn't give me an accurate angle.

Edit: again: Ok, how are you guys putting in text boxes?

[edited by - EGD Eric on January 24, 2004 7:49:47 PM]

[edited by - EGD Eric on January 24, 2004 8:33:55 PM]



[edited by - EGD Eric on January 24, 2004 8:35:29 PM]
the problem with your code is that the result of the dotproduct is NOT an angle at all. only when you take the arccosine of it you will get an angle. in radians. you can scale that value to degrees if you like. just dont do any scaling on the dotproduct outcome.
And don''t bother with jack_1313''s code. It won''t give you the angle between vector a and b.

This topic is closed to new replies.

Advertisement