Dot/Scalar Product
Miscellaneous Forums/General Discussion/Dot/Scalar Product
*sigh* I wish I had a math brain!
Anyway, I know how to calculate the dot product of 2 vectors, and I know what the result is useful for, but, can someone explain to me in layman's terms exactly what the dot product tells us about the relationship between two vectors?
It just won't click in my head. I've tried googling this but, as always, all explanations quickly descend into math moonspeak. It just aswell all be written in Russian! :/
It is more or less the angle between two vectors. That's the simple explanation, as I don't think you need the complex one (you won't understand it any better than I do, and I use this stuff very often..).
The main point is it will return the angle between two vectors... also two vectors multiplied together equal zero then they're perpendicular..very useful. You can also derive other valuable titbits from the answer by checking the answer is greater or less than zero..this tells you whether the angle is greater than 90 degrees or less...good for a quick camera viewport check.
Ok, thanks. I'm a bit confused by the point about the dot product representing the angle between the two vectors though, as the result changes if you alter the magnitude of either vector.
Here's something I read last week when tearing my hair out trying to get corner bouncing working:
'You can project a vector onto another by doing the "dot product", and what you get the is component of the first vector along the direction of the second vector'
Thing is, as you know, you multiply the x of the first vector by the x of the second, and you add it to the y of the first multiplied by the y of the second. So this just gives you a big fat number which is somehow the "combination" direction of both vectors. Then you can use the result to scale up a "normalised/unit vector" i.e. value range is 0 to 1.
Also if you do the dot product of a vector on itself then square root it, you have the distance of the vector because you've done simple pythag e.g. SQR(x*x + y*y).
re: the angle thing? Maybe both vectors have to be normalised first? Ah yes I remember (he says looking at his scribbles from last week), the dot product = length of a * length of b * Cos(Angle). So if you know the lengths of a and b (use pythag), you can do the dot product of a and b, and then divide the result by len a * len b and you get a number which if you ACos gives you the angle.
Perhaps the words of
Hugo Elias will help...
What is the meaning of the value returned by the dot product?
The value is the cosine of the angle between the two input vectors, multiplied by the lengths of those vectors. So, you can easily calculate the cosine of the angle by either, making sure that your two vectors are both of length 1, or dividing the dot product by the lengths.
Cos(theta) = DotProduct(v1,v2) / (length(v1) * length(v2))
Values range from 1 to -1. If the two input vectors are pointing in the same direction, then the return value will be 1. If the two input vectors are pointing in opposite directions, then the return value will be -1. If the two input vectors are at right angles, then the return value will be 0. So, in effect, it is telling you how similar the two vectors are.
I find Hugo's explanation of such things very easy to follow and he usually gives pseudo code examples, which are always useful.
Some interesting points - thanks, folks. Things are starting to click more in my head, now - these things can take a while when it comes to things mathsy.
in effect, it is telling you how similar the two vectors are.
I guess that just about sum's it up in layman's terms. I'll remember that when thinking of the dot product. :)
If you think about what the dot product does to normals, it might be easier to grasp the mechanics.
Consider, in 2D, a normal pointing to the right, and a normal pointing up. Compute the dot product.
D = X1*X2 + Y1*Y2
In this case, D will equal 0, because one is at 90 degrees to the other.
This will be true as long as the normals are at 90 degrees to one another, even if they're on an angle. But the axis aligned case is is the easiest to grasp. One has an X component of 0, the other a Y component of 0. 0 * anything equals 0, so both parts of the equation are 0 and adding them gives you 0.
When they're on an angle what happens is you get different signs multiplying together, and they end up canceling out. You might have to try a few cases to prove this to yourself though.
Now consider what you get when the two normals point in the same direction on one axis. You get 1. And when they point in opposite directions on one axis? -1. Again, through the wonders of vector mathematic, this works even if the normals are at an angle.
The only thing that's different when your vectors aren't normals is that you can't use the values -1..0..1 to give you a precise indication of exactly what the angle between the vectors is.
But you can still say that if the dot product is less than 0 then one's pointing "backward" relative to the other (angle greater than 90 degrees), and if they're greater than 0 they're both pointing "forward" relative to one another. (Angle less than 90 degrees)
cool. Nice explanation sswift.
Yeah, thanks sswift.
I find the whole subject of trigonomentry and vector math fascinating, I'm just not as good at it as I'd like to be. I know a lot more now than I did when I left school, though. ;)
I used the scalar and the dot products to find the position of an enemy relativly to my player
One returns the angle but the enemy could be on the right or on the left side
The sign of the other discriminates the two alternatives