Let’s talk about the dot product in game development

BlueBubbleBee
7 min readJan 27, 2021

How to use the dot product operation in Unreal Engine using C++ and Blueprints

When I work on gameplay mechanics, I often stumbled across situations where I need to apply maths.
When implementing my NPC (Non-player characters), I face a similar situation like the example I will show you here.

The dot product operation is one of them, it’s handy!
In this example, we have an intimidating giant blue cat stares at my salmon sashimi….

Don’t worry, the cat is friendly and only looks at you if you are facing it, and right there we have a clear situation where we can apply the dot product.
Am I facing the cat or not?

Hey kitty kitty, I offer you this delicious meal if you tell me your secrets…Hey kitty kitty, tell me your secrets

To illustrate this example, I am using C++ and Blueprints on Unreal Engine 4.26.

You can follow the video tutorial here and the project files here.

Dot Product Concept

The dot product is an operation between 2 vectors, which returns a float number.

  • If Dot Product is greater than 0, the cat and the robot face the same direction. (They are looking at each other)
  • If Dot Product is equal to 0, the cat and the robot face perpendicular direction (The robot is looking at the side of the cat)
  • If Dot Product is less than 0, the cat and the robot face the opposite direction (they are back to back)

You are likely using an engine such as Unreal Engine 4, Unity ur other if you are making a game.

These engines provide you with the basic operations, so you don’t have to worry about them.

But just in case you are interested here is how you calculate the dot product:

// Multiply each component of each vector and add everything together
float dotProduct (v0, v1)
{
return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
};
// The dot product is also the cosine of the angle that both vector form, divided by their lenght
dotProduct(v0…

--

--

BlueBubbleBee

I am Bee, an indie developer making Kitori Academy a cosy life sim where you take the role of a wizard apprentice and have a cat as a companion!