A brief explanation of Game Engine math

In Game Engine development, you always deal with Dot Products, Cross Products, Transformations, Inverses. You deal with Quaternions (I used quaternions and dual-quaternions in my game engine A LOT). You deal with the Minkowski Difference. You deal with finding if a point lies within a Tetrahedron, or if a point is above or below a plane, etc.

You may not know why you need to use these operations or when you should use them. And that is why I wrote this article. I want teach you why and where you should use these operations.

Dot Product

Let's start off with a useful operation in computer graphics: Dot Product. The dot product computes how much one vector influences the other. The Dot Product is commonly used in OpenGL shaders to determine the lighting of a mesh.

To determine the amount of light a mesh receives, you need two vectors: A Normal Vector and the vector emanating from the light source. From experience, you know that if a light source is right on top of an object, the object becomes entirely bright. As the light source changes position, the object's brightness changes. We can emulate this physical property in computer graphics through the use of the Dot Product.

When the light source vector s is parallel and heading in the same direction as the normal vector n, the dot product is 1. Since the dot product ranges between [-1.0,1.0], the point becomes fully lit.

As the light source moves, the angle between vectors s and n changes and the dot product changes. When this occurs, the lighting also changes. This is exactly how I compute lighting in my game engine. As a game character moves away from a light source, its brightness changes.

Dot product 2.jpeg

Below are two rendered samples of a mesh with a light source at different positions.

 Light Source at position (1.0, 0.0, 0.0)

Light Source at position (1.0, 0.0, 0.0)

 Light Source at position (0.0, 1.0, 0.0)

Light Source at position (0.0, 1.0, 0.0)

Cross Product

The Cross Product is a frequently used operation in computer graphics. The Cross Product computes a perpendicular vector with respect to two other vectors. This operation is not as intuitive as the dot product. It took me a while to grasp the usefulness of this product. As a matter of fact, I fully understood its value when I started developing the Physics Engine.

Let me explain,

In a Physics Engine, a mesh's center of mass is an important property. Applying a force along the center of mass, translates the mesh but does not rotate it.

A rotation only occurs if the force is applied at an offset position from the center of mass. The question is, about which axis should the mesh rotate? The Cross Product provides this answer.

In my game engine, I use the properties of a cross product to determine a box's axis of rotation. In this case, the axis of rotation is the z-axis.

Matrix Inverse

One operation that is very useful but not as intuitive is the Inverse of a matrix. So you can understand it better, let me step back a bit and talk about vectors and matrices.

A vector defines a position in space. A vector has magnitude and direction. Vectors can be added or subtracted. But none of these operations changes a vector's coordinate system. And this is the key to understanding what a matrix does to a vector.

A matrix transforms the coordinate system of a vector. When a vector is rotated or scaled, its coordinate system is transformed into a new coordinate system.

So what does this have to do with a Matrix Inverse? The Inverse of a matrix simply does the opposite of the transformation. For example, if a matrix rotates(transforms) a vector about the x-axis by 50 degrees. Then the matrix-inverse rotates the vector about the x-axis by -50 degrees. See, the matrix-inverse transforms the coordinate system in the opposite direction.

A practical example in game engines is when you deal with a camera and a world entity. Imagine the world entity contains several buildings. To orbit about these buildings, you have two options: You can orbit the camera about the center of the world, as is shown below:

or you can rotate the world in front of the camera, as illustrated below:

Each option is the inverse operation of the other. You can switch between one or the other by computing an inverse transformation.

Minkowski Difference

The GJK is a popular algorithm used to determine collision among convex objects. At the heart of the algorithm is an important property which states: If two objects, A and B collide, then their Minkowski Difference contains the origin.

Before I show you what a Minkowski Difference is, let me start off with a Minkowski Sum. Visually, the Minkowski Sum can be seen as the region swept by Object A translated to every point in Object B. See the image below:

In contrast, the Minkowski Difference is the region swept by Object A translated to every point negated in Object B. See the illustration below:

As the image above shows, object A and object B have collided. Therefore its Minkowski Difference contains the origin point. A game engine uses this property to determine if two game characters have collided.

I hope these examples helps you understand the usefulness of dot products, cross products, inverses and Minkowski Difference in game engine development.

Thank you for reading.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.