Math for game developers

September 25, 2020


Math is heavily involved in different aspects of video games that you have played!

  • Artificial Intelligence? Math
  • Beautiful water waves? Math
  • Procedural Generation? Math
  • Medical Visualizations? Math

In this blog post, I will demonstrate mathematics uses in some video games.

#1 DaysGone : Zombies Detection Trigger

As you can see in the video when the player is in the detection range, the zombies will start following him.

We will see how we can achieve this with simple math.

Let’s extract the variables that we need to be able to implement this :

  • Zombie Position
  • Player Position
  • Detection Radius

For this example, the zombie will detect and follow the player only if the distance between them is less or equal to a known radius.

I got the habit of using Gizmos whenever I prototype something so I can quickly visualize what’s going on:

Consider these two vectors:

  • Zombie position : (x1, y1, z1)
  • Player position : (x2, y2, z2)

The distance between them is calculated as:

$$ rangeDist = \sqrt{(x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2} $$

In Unity there is a helper function : Vector3.Distance that does the math for us. So it looks like this in our code:

float rangeDist = Vector3.Distance(playerPos, zombiePos);

We could then compare this value with the detection radius :

bool isInRange = rangeDist <= _detectionRadius;

Then, we just use this boolean to visualize the behavior:

Gizmos.color = isInRange ? :;
Gizmos.DrawWireSphere(zombiePos, _detectionRadius);

You can get the full project source code from the Github repository.

#2 Control : Objects On Sight Detection

In Control, the player can pick up an object on sight and throw it to deal damage. The mechanism of detecting objects on sight is achievable using the Dot Product.

We will need a couple of variables to implement this:

  • Player Position
  • Player Look Vector
  • Object Position

I have built a scene with a player (capsule) and three objects to be detected, added a rigidbody component to each one with Kinematic on. The moment the player looks at the object, it will turn Kinematic off, and it will fall.

First, we need to calculate the direction vector between the player and the object :

Vector3 direction = objPos - playerPos;

Then we need to get the look vector of the player which will be the forward vector :

Vector3 playerLookDir = _playerDetectionTrans.forward;

Note that we need to have at least one normalized vector to be able to use the Dot Product :

Vector3 direction = (objPos - playerPos).normalized;

// transform.forward defines the blue axis of the transform in world space.
Vector3 playerLookDir = _playerDetectionTrans.forward;

One way to calculate the Dot Product between two vectors is by using this formula :

$$ dotProdVal = {x1x2 + y1y2 + z1z2} $$

In Unity we have a helper function; Vector3.Dot.

The code looks like this:

float dotProdVal = Vector3.Dot(direction, playerLookDir);

If the value calculated is greater or equal to a given threshold, the player is looking at the object:

bool isLooking = dotProdVal >= _lookThreshold;

From the result we can manipulate the object’s kinematic :

_rgbdy.isKinematic = !isLooking;

You can get the full project source code from the Github repository.

Are you searching for my Resume?→Click!