Introduction

If you are coming from the Points and Vectors guide, let me thank you guys for reading that and I hoped it helped! If you are new to the series, I will be going over 3D and Linear Algebra Game Development Math for you to be successful in your future gameplay programming endeavors.

If you already know dot products mathematically and geometrically speaking, go down to the Use Cases section and look at some of the many examples of how we can use dot products in games!

What is a Dot Product?

Finally we have arrived to one of the most important tools in game development. The Dot Product. As fancy as it sounds the math for this is not difficult to do. I am here instead to help you think of dot products geometrically and how we can utilize its full potential.

The dot product as Allen Chou, a gameplay programmer at Naughty Dog sums it up really well by saying, “The dot product is a simple yet extremely useful mathematical tool. It encodes the relationship between two vectors’ magnitudes and directions into a single value.”

Let’s break this down and let’s see why this is the case. In mathematical terms, the dot products looks as so:

There are two ways to compute the dot product and the resultant is always the same. The different ways however tell you different things about the vectors. Here are the two ways.

First Equation Information

Let’s look at the first way in the picture and let’s see what kind of data we can extract so we can visually see what is happening. Let’s right away notice the cos(theta). The coolest thing about this way is the angle inside the cosine. This because we are able to extract the angle and you might be wondering what that angle is. Well let’s look at what that angle represents geometrically.

So we have two vectors U and V, if we put their starting points at the same place, we create an angle between the two vectors. The theta tells you that exact angle shown above. So you might be wondering why that is useful, let me save this information for when we get there below to the Use Cases section.

What Does The Cos (Theta) Tell Us?

Now let’s talk about what the Cosine is actually doing. First a Cosine value is between -1 to 1. If the Cosine of the angle is 1 meaning Cos(angle) == 1.0f, that means the Vector U and V in the picture above are facing exactly the same direction and the angle between the two vectors is 0 degrees. Notice how as the Cosine value decreases, the angle between the two vectors increases. You can see from the image above that if the vectors are facing the same direction, the angle between them is acute and if they get closer that means the Cos(angle) is getting closer to 1. If the two vectors are orthogonal (perpendicular, same thing) the Cosine of the value is 0. And you guessed it, if the two vectors are obtuse angle or greater than 90 degrees, then the Cos(angle) < 0.0f.

What we just did shows the relationship between the two vectors because they are what determines what the Cos(angle) will be.

The dot product, it tells you two things, how similar these two vectors are to each other and the strength of these vectors. We will talk about the strength in just a bit but the Cos(angle) part of the equation of the dot product tells us the similarity of these vectors. If they are in the same direction we know that the Cosine value will be greater than 0 and depending on how similar these vectors are, closer or farther from 1. The opposite is also true, if the two vectors are facing away from each other then we know the Cosine value will be less than 0 and depending on far apart away they are looking closer or further from -1. Just for completeness, if the Cosine value is exactly 0, then we know the vectors are exactly perpendicular.

What Does The ||A|| * ||B|| Tell Us?

Let’s look at strength. The other part of the first equation is Magnitude A * Magnitude B or ||A|| * ||B||. From the last guide we know what this is. It is the length of these vectors. So you can imagine that if the two vectors A and B are really big, the magnitudes of these vectors will also be big. So if multiply these values together, we get some total scalar value that is big. If the magnitudes are small then we will get a small scalar value. That is all there is.

Do you see what the Cosine value is doing to these magnitudes. If the Cosine value is 0, then it does not matter how big these vectors were, because they are exactly perpendicular of each other. If the Cosine value is 1, then we get the biggest magnitude value of these vectors because they are exactly aligned in the same direction. If the Cosine value is -1, we get the largest negative magnitude value meaning these vectors are not related at all to each other.

Second Equation Information

These equation produce the same exact result and I won’t go into how they are same. Again, I just want to show you geometrically what the dot product is. Yes, I am avoiding this problem because it is very mathy but the key thing to note about this one is if you are not interested in knowing the exact value of the angle between the two vectors, we can simplify our understanding of the dot product. All we care about here is just like above the sign of the dot product, which tells us how closely these vectors are related to each other, and its strength. I already did the heavy lifting of explaining how the first equation shows this. The second one just does the same thing as well.

Here is the equations again just for repetition. Get familiar with these equations because we only care about what the dot product value tells us geometrically speaking.

The Mistakes I See With The Dot Product

First of all, and the biggest of them all, we can only take the dot product of vectors! We can not take dot products of points! It does not mean anything!

Secondly, some people let’s say would like to generate a specific perpendicular vector to another vector, so they might do something like this, vector A dot vector B = 0. Then create a vector B that satisfies the equation. Yes this will give you a perpendicular vector. Keyword here being ‘a‘ because this perpendicular vector can be almost anything in 3D space. To generate a specific perpendicular vector, you will need more information. For example, if I want to create a vector that is perpendicular to a plane, and the plane equation is given in vector or parametric form. You can’t just make any old perpendicular vector.

Dot Product Code in C++

``````static float dotp(const vec3& a, const vec3& b)
{
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}``````

Use Cases in Game Development

Dot products in games utilize the relationship of vectors. How do these two vectors relate to one another, and what information I can extract about them. Here are a few cases of how dot products can really amplify the way we write gameplay code.

AI Vision

So a case that everyone needs to know about for video games that is heavily used in gameplay and AI programming is the idea of vision. For example, Let’s say enemies can see a full 360 degrees, that means if a player is trying to sneak up on the enemy, it can not because the enemy’s vision is a full 360 degrees. Well the dot product is the perfect case here and I can show you how.

First and foremost, we need to know a few things, we need to to know the direction of the enemy is facing, we can call this the forward direction vector. Next we need to know, the direction vector from the enemy to the player. To get this direction vector, we can just take the position of the enemy and subtract it from the position of the player to get the direction vector from the enemy to the player. Now we have the two vectors we need to determine if the player is in vision of the enemy.

Now let’s imagine that these two vectors are starting at the same position. What happens to the vectors made if the player is behind the enemy? Well the vector from the enemy to the player is facing in the direction of the player, and the forward vector on the enemy is facing in front of him. If we take the dot product we know that it will be < 0 because there is an obtuse angle made between these two directions. Now we just need to say, that if the dot product is negative, we don’t see the player. Let’s show a small code example of this.

``````//Enemy forward direction
Vector3 l_AIForward     = transform.forward;

//Direction from Enemy to Player
Vector3 l_DirToPlayer   = m_Player.transform.position - transform.position;

//dot product value of left side of equation 1
float l_Dot             = Vector3.Dot(l_AIForward, l_DirToPlayer);

//dividing the magnitudes over to the other side
//we do this so the equation looks like this dot / mag A * mag B = Cos(Angle)
float l_DotOverMag      = l_Dot / (Vector3.Magnitude(l_AIForward) * Vector3.Magnitude(l_DirToPlayer));

//The Cosine value, notice how I need to convert the the angle to radians
//Also notice how I have the member variable m_DegreesSeen, this is for
//designers to edit how much the enemy can see, or its vision cone, I divide by two
//here because I want the values for designers to enter from 0 to 180 degrees
float l_Vis             = Mathf.Cos(Mathf.Deg2Rad * (m_DegreesSeen / 2.0f));

//The AI can see the player and its in vision range
if (l_DotOverMag > l_Vis)
{
//Do Some code if the player is visible
}``````

So even without anything in the body, we can quickly make a small tool to show what the enemy is seeing, and we can have designers customize that value! In Unity I quickly threw this together and this is what it looks like.

Notice how when the player entered the vision of the enemy marked by the yellow area, the enemy was able to follow the player!

Here we used the property of the angle and vector relationship of the dot product to help solve this issue. Almost every game with AI does this exact thing for vision related things.

One Way Triggers

Another example of dot products that you might see are one way triggers. An implementation for this is that your trigger game object has a forward direction, and the player also has a forward direction. If we check the dot product between these vector and the trigger object and the player are colliding and the dot product is greater than 0, then do something. An example of this something is pop text when you enter an area like Jak and Daxter, Dark Souls, and other games. I will show you an example from my game, called Metamorphos.

Notice running through the trigger, we are able to activate the text, but when running back through the trigger, the text is not activated again. Dot products!

Interactable Object Prompts

Lastly, I will show one more very common use case of dot products. This is when we are looking at an object, we should see some kind of prompt to press a button, but when we are looking away the prompt should disappear. This is common for games with a lot of interactable objects like accessing a shop, reading text, or just conveying a button prompt to the player.

The same same idea as before, we use the relationship of the direction vectors of the objects and direction vectors to targets to determine what we should do. One way of doing this is creating the direction vector from the interactable object towards the player, and using the players forward. If we compare the two we should get a dot product of < 0 and if this is true then we show the prompt. This way, if the player goes to the object from any angle to interact with it, we will show the prompt to them. Try imagine the vectors we create and see the relationships between them geometrically.

I will show another example of this from the game Metamorphos.

Conclusion

As you can see there are many various applications of dot products in video games. There are so many and the idea here is that we utilize this tool as a way to geometrically interpret what the relationship between two vectors is and that is what the dot product is telling us. If you want to contact me and tell me your love about dot products or talk about linear algebra for fun, email me at amirazmi0830@gmail.com. I will be hopefully soon make a tutorial on cross products! See you next time!