Vector dot product

Started by
0 comments, last by Cornstalks 11 years, 3 months ago

I'm trying to use vector dot product to get the angle between two vectors, but I'm puzzled by the results.

Given two vectors, A and B:

A(0.0, 0.2, 0.0)
B(0.8, 0.0, 0.0)

The angle between those, calculated with the dot product:

alpha= (acos(A*B)) * RAD_TO_DEG

properly returns 90°.

Now, let's have two vectors pointing in the same direction:

A(0.2, 0.2, 0.0)
B(0.8, 0.8, 0.0)

Would you guys explain me why in this case alpha is equal to 9.9°, while I'd expect to return 0°? Thanks

Advertisement

Your vectors aren't normalized. Normalize them first. That's required when using this equation.


Edit: (just to add an explanation for why the above matters)

To make it really clear, the dot product equation is:

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

Where (A and B are your vectors, . is the dot product, and ||A|| means the magnitude of the vector A). In this form the equation, A and B are not required to be normalized (because the ||A|| * ||B|| part takes care of that... you'll see below).

Rearranging it, we see:

theta = acos((A . B) / (||A|| * ||B||))

Now, to normalize a vector A, you just do A / ||A||. I'll let An be the normlized version of A, where An = A / ||A||. Then you can plug it in as:

theta = acos(An . Bn)

Which is what you're trying to do, but your vectors aren't normalized, which is why you're getting problems.

So, to say it one more time:

theta = acos((A . B) / (||A|| * ||B||)) <-- General equation
theta = acos(An . Bn) <-- Special case of the generalized equation (simplified because for normalized vectors An and Bn, ||An|| * ||Bn|| = 1)

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement