Skip to content

Dot Product

James Russell edited this page Apr 9, 2018 · 2 revisions

Practically speaking, the dot product simply states how much alike one vector is to another.

So let’s imagine two vectors:

To make this easy, these two vectors have the same length of 1. When we do a dot product between these two vectors, we are asking how much alike these two vectors are in both length and direction.

Since these vectors are normalized, when we do a dot product on these vectors, we are pretty much just asking how much alike are their directions.

We do this in Godot by simply using “dot()” inside one of the vectors you are going to use in the dot product, like this:

DotProduct = Vector_A.dot(Vector_B)

But what is happening when we do this? There are actually two ways of calculating a dot product, but I will only show you one (the easier one). It goes like this:

DotProduct = (Vector_A.x * Vector_B.x) + (Vector_A.y * Vector_B.y)

We multiply the X's, multiply the Y's, then add them together.

In the example above, it would go like this:

DotProduct = (0.60 * 0.87) + (0.79 * 0.47) = 0.522 + 0.3713 = 0.8933

So our dot product is “0.8933”. Okay, that’s great, but so what? Well, with this we can find the angle between these two vectors.

To do so in Godot, we simply need to use “acos()”.

Angle = acos(0.8933) = 0.466161766

Note that every program/calculator may use either radians or degrees when using trigonometry functions. In Godot they will always use radians. Depending on the program/calculator, if you use a trigonometry function it may use angles in radians or degrees. You’ll have to find out. On my calculator program on my computer I can change the angle units used to use either degrees or radians. Always make sure of what you are using.

The result “0.466161766” is in radians, so if we convert that into degrees either using Godot’s built-in function…

Angle_Degrees = rad2deg(0.466161766) = 26.709101783 degrees

Or you can just do it yourself (slower; just use the built-in function):

Angle_Degrees = 0.466161766 * (180/PI) = 26.709101783 degrees

So the angle between these vectors is about 26.7 degrees. Cool.

But what if we had two vectors like this:

How much alike are these two vectors? Let’s find out. Our formula is this:

DotProduct = (Vector_A.x * Vector_B.x) + (Vector_A.y * Vector_B.y)

And the real numbers are:

DotProduct = ( 0.0 * 1.0 ) + ( 1.0 * 0.0 ) = 0.0 + 0.0 = 0.0

Do you see how it gets the the similarity between the two vectors? The X axis position on the first vector is “0.0” and we multiply that against the X axis of the second vector. But it doesn’t matter what the second vector’s X axis is because anything multiplied by 0 is always 0. The same with the Y axis, in this example. It’s get multiplied by the second vector’s Y axis, which is “0.0”.

So, really, when we use the dot product what we are asking is “How similar are vector 1 and vector 2?” And in this case the answer is “They are not at all similar,” or in mathematical terms “0”.

So what’s the angle between these two vectors?

Angle = acos(0.0) = 1.570796327

And conversion to degrees...

Angle_Degrees = 1.570796 * (180/PI) = 90

We already knew this, but it’s nice to see it done with math, so we can see how it

One last example:

So how similar are these two vectors? They’re exactly the opposite of each, they seem to have nothing in common. Wouldn’t they have a dot product of “0.0”? Well, think about it. They are exactly the same, but they are going in exactly the opposite directions. So really, what would their dot product be?

DotProduct = ( -0.801838 * 0.801838 ) + ( -0.597542 * 0.597542 ) =0.642944178 +0.357056442 = -1.0

Note that I had to use more precise numbers. I did this because if I used the rounded numbers in the picture I would of gotten “−0.9881”. So, of course, more precise number equal more precise numbers.

Also, if you were to calculate the dot product above you would actually get “−1.00000062”. This is okay as this is the nature of programming with floating points in computers. It’s not always possible to get exactly “1.0” when using a dot product because the numbers can only be so precise. But when programming for a video game, it’s okay to have less precision, as it is almost never noticed as the numbers are actually quite precise in the first place. The only exception I can think of is maybe something to do with physics. Sometimes errors or glitches occur because of lack of precision, but that’s to be expected. If it were any more precise video games would run slowly.

So the result of our dot product is “-1.0”. Our assumption was kind of right. The vectors weren’t anything alike in direction, but they still had the same number but they were negative. So we result in a “-1.0” because they are the same, but they are just going in exactly the opposite direction from each other.

So what would the angle of that be?

Angle = acos(-1.0) = 3.141592654

And converting it to degrees…

Angle_Degrees = 3.141592654 * (180/PI) = 180

This comes as no surprise, but the angle of these two vectors is “180”. Sweet.

3D Dot Product Formula

To get a dot product from two 3D vectors, it goes like this:

DotProduct = (Vector_A.x * Vector_B.x) + (Vector_A.y * Vector_B.y) + (Vector_A.z * Vector_B.z)

Understanding the Dot Product

So what exactly is going on? It’s confusing! Well, let’s say we have these vectors:

Now, in order to visualize what’s going on, we will tilt both the vectors at the same time so as to lay Vector A down on the black line, so that it will be “(1.0 , 0.0)”:

So Vector A will be our “base” vector, the one that will be the first argument of the dot product, or in Godot it will be the vector we will be calling the “dot()” function from, like this:

DotProduct = VecA.dot(VecB)

Imagine there is a line from the end of Vector B going straight down, like this:

Since we’ve laid the vectors down, Vector A only has a non-zero number on the X axis, which is “0.972219”. This is the dot product: it basically means how much do you need to multiply Vector A by to get a right triangle. If you were to multiply “( 1.0 , 0.0)” by “0.972219”, what would you get? “(0.972219 , 0.0)”, of course. And if we were to get the length of Vector A after that, it would also be “0.972219”.

Why is this important? Well, just look at what we now have:

It’s a right triangle! Now we can see what angle we’ve got by using arcsine, arc cosine, or arctangent!

So let’s say we want the angle between these two vectors. We have the dot product, which is “0.972219”.

acos(0.972219) = 0.236265072 radians = 13.536991487 degrees

We get the angle, in radians, between the two vectors. Why is this? Remember that “cosine” simply means the length of the adjacent edge compared to the length of the hypotenuse edge:

That means that the adjacent side is about “0.97” the length of the hypotenuse. And remember that all of these sides are in relation to the “θ” angle. So if the cosine of “θ” is the adjacent’s length divided by the hypotenuse’s length, then that means if we do it backwards, we will get “θ”, or the angle of the two vectors.

We use “arc cosine” in this case, which simply means “do cosine but backwards”. There is also “arcsine” and “arctangent”. And, as we have already seen, we just use “acos()” to do an arc cosine calculation.

I suggest looking for more tutorials on this subject. It takes a while to understand what is going on, but once you do you have it. Just give it time and come back to this subject again and again until you get it.