Game Maths — Dot Product
Recently I’ve been revisiting some useful maths used in game development, starting with Dot Product.
Today’s Objective: Explain the Dot Product math formula and uses, relating to game development.
Math:
…is not something I find very enjoyable. Nor am I particularly good at it. But I hope these posts will help anyone in a similar situation to get a better grasp of the important parts of a few common/useful math formulas in the context of game development and programming.
It can be an exhausting topic to delve into if you need to, but fortunately in most cases (from my experience) you don’t need too much expertise in math to be a good developer these days. I’ve been a game developer/programmer for many years, and am only now looking into things like the Dot Product with any seriousness.
Purpose:
Using the Dot Product, you can find the Float value that represents:
“How far along Vector A does Vector B sit, when viewed from a right-angled perspective?”
The Technical Stuff:
The Dot Product takes 2 Vectors and returns a Float (“Scalar”).
In normal math terms:
Dot Product = V1.x * V2.x + V1.y * V2.y + V1.z * V2.z;
Or in Unity C#:
float DotProduct = Vector3.Dot(VectorA, VectorB);
A Real Game Example:
I was recently working on a custom mesh generator, and found a use for the Dot Product when modifying a set of Vector3 points, based on another set of Vector3 points.
I have a dynamically-generated curve made of a number of Vector3 points. I want to generate another set of points where each is part-way between the first set, and a straight line between the first and last point.
In this case, the straight line is the normalized Vector A, and the existing curve is the set of Vector B’s (where each curve point is a separate Vector B).
Hopefully this has been an easy-to-understand explanation which can help you when you’re writing code or developing your games.