• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Vector dot product

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1Alessandro  Members

Posted 13 January 2013 - 11:36 AM

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:

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

### #2Cornstalks  Members

Posted 13 January 2013 - 11:58 AM

POPULAR

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)

Edited by Cornstalks, 13 January 2013 - 11:08 PM.

[ 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 ]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.