You know this moment would come, it’s time to talk about math, especially math for game developers.

**What you’re going to see here is the basic math used in every video games made on the face of the earth**, to be more specific, we’re going to see **how to use vectors in game development** and all the operations around this mathematical instrument.

In fact, without a knowledge of math and how math can be used to develop your games, it’s difficult to design any kind of game that involves basic physics. So the bottom line is: if you **want to be a game developer**, you must have some basic mathematical skills.

**I know that most game engines like Unity or Unreal already have physics or graphics engines that solve most of these problems. But still, you need to know the mathematical tools needed to interact with the game world.**

In this post you’re going to see the basic entity that you’ll probably find in most games that you’re going to develop, the **Vector**.

Most of the examples are made with the Unity Engine but you can obtain the same results with any other game engine.

# ↗ Math for game developers: say hi to the vector

Vector is a quantity that has both magnitude and direction in an n-dimensional space.

We can basically think of it as an arrow: ———>.

- The arrow’s length is its magnitude.
- The tip of the arrow indicate the direction of the vector.

**Ok, but what can a vector be used for?**

Vectors can be used to describe all sorts of physics situations and more.

For example, a ball kicked in a football game can be a situation where vectors are used to describe the force applied to the ball along with the direction where the force is applied.

Same goes for a bullet shot from a gun.

Have you ever played the game Angry Birds?

**The force and the direction applied to the bird can also be described using vectors**.

And what about the velocity of a car running in the latest amazing racing game? That also can be described using vectors. And the list goes on and on.

**How is a vector defined in a game engine?**

In most game engines, you’ll find a vector as a description of its 3 components X, Y, Z.

Why 3 components?

**Because our game is defined in a 3D world so when we apply a force to a game object in our engine, we can apply it in each arbitrary direction inside the game (unless there are restrictions).**

For example if we want to describe an elevator that is moving up and down in our Unity game, our vector we’ll be formed by these coordinates (X = 0, Y = velocity of the elevator, Z = 0).

**This is the definition of a vector in Unity C#:**

`Vector3 aVector = new Vector3(0,3,10);`

**Scalar vector**

A scalar vector is nothing more than the magnitude representation of the vector and is usually written in italics ( e.g *v*) while vectors are written in boldface ( e.g., **v**).

# 🟢 **Use vector to represent a point in space**

Image we want to describe where an object is placed in our 3D or 2D game, how can we do that? **Taken the origin of our 3D world, we can draw a vector from it to the game object** and this is the vector that describes the position of the game object inside the game world.

In fact, imagine drawing an arrow from the origin of the 3D world to the position of the game object, that is the exact vector that we’re looking for.

If you look at the image you’ll notice that the vector in question is nothing more than the coordinate of X,Y,Z in a 3D cartesian system which in our case represents the game world space.

So we understand that vectors can not only be used to describe physical simulations, like the motion of a rigid body in a 3D world, but they can also be helpful to describe the position of an object inside the game world.

# Orientation of vectors

## 👆 **Left-hand system vs right-hand system**

In the right-hand system (RH), when curling your fingers around the z-axis with the thumb pointing toward the z positive direction, your fingers point from the x-axis toward the y-axis.

In the left system, it is the same method but using the left hand.

It’s important to know which system your engine is using and stick to that convention when using it otherwise you might run into some errors.

# ✒️ Understanding **Vector operations, the basis of the game math**

Let’s take a look at some of the basic operations we can do with vectors and how they can be used inside your games.

**Multiplications by a scalar**

Multiplication by a scalar has the effect to change the magnitude of the vector. We simply multiply each component of the vector with the scalar value.

For example, the vector A: (2x, 3y,5z) multiplied by 2 = A * 2 = (4x,6x,10z)

**Addition and subtraction**

Adding and subtracting vectors are quite easy Given two vectors A and B, the addition of the 2 is the sum of the single components of the vector.

For example :

Same things goes with the subtraction.

A good usage of the addition and subtraction of vectors in games could be the problem of finding the distance between 2 game objects.

In this case, the distance from A to B is defined as the sum of the 2 vectors (A + B).

**Normal vector**

A vector is said to be normal to a surface if it is perpendicular to that surface.

**Normalized vector**

It is a simple vector that has a magnitude of length equals to one unit (don’t confuse it with the normal vector).

## Vector **Dot product**

**The Dot Product can be helpful when finding the angle between 2 vectors and by consequence, if the 2 vectors are perpendicular to each other**. If the dot product is zero, then the 2 vectors are perpendicular.

Example of the results of some dot products

Image from: cdn.tutsplus.com

It can also be done by:

**A dot B = |A| * |B| * cos “angle between”**

Another example of the dot product could be when calculating the speed at which a character/game object is moving on a slope. Other examples include finding how much of a character’s velocity is in the direction of gravity, finding how long until a movement crosses a threshold, and more.

**Cross product**

**Example:**

With the cross product, we can find C, which is the axes where we can rotate the tank in our game.

**Magnitude of the cross product:** |a x b| = |a| |b| sin (angle between a and b).

**The direction of the cross product:** Use the right-hand rule to determine the direction.

NB: Unity, can perform all of these operations without having to calculate them manually. But it is just helpful to know how they work and where to use them.

**Linear Interpolation (LERP)**

The linear interpolation (LERP) is one of the most common operations used in game development.

For example, **if we want to smoothly animate from point A to point B over the course of two seconds at 30 frames per seconds, we would need to find 60 intermediate positions between A and B**.

A linear interpolation is a mathematical operation to find an intermediate point between two known points. The operation can be defined as follows:

Where β is a value between 0 and 1 where 0 represents the initial position of the Lerp and 1 as the final position.

As you can see from the definition, we’re representing the lerp as a **Vector**.

Here is an example to visualize the lerp in action:

Most game engines has a lerp function ready to be used. Here is an example of a code using the lerp function in Unity:

```
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Vector3 startPosition;
public Vector3 endPosition;
public float speed = 1.0f;
private float startTime;
private float journeyLength;
void Start() {
startTime = Time.time;
journeyLength = Vector3.Distance(startPosition, endPosition);
}
void Update() {
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPosition,endPosition, fracJourney);
}
}
```

As you can see, we’ve used vectors to describe the start position and the end position of the lerp. The Vector3 Lerp is a function from Unity that executes the LERP operation for you.

# ↪ **Use the vectors math to describe objects and interactions in the game world.**

**Model space**

The model space is the reference system used by the object in question.

**For example, a character game object’s forward axis points in the direction that the object is naturally facing.**

Up: is the direction that point towards the top of the object

The left or right are the axes that points to its left or right sides respectively.

Example with a plane:

## 🌍 **World coordinate**

A word coordinate is a fixed coordinate in which position, orientation, and scale of all the elements placed in the game are described.

The origin of the coordinate system is arbitrary, but it is as a common practice to place it in the center of the world in order to reduce the number of floating point numbers needed to describe objects very far away from the origin.

The orientation of the axes is also arbitrary but usually, the convention is to have the the y-axis as the axis pointing upward.

## 🌁 **View space**

The view space coordinates are often called as the camera coordinates. These are the coordinates fixed or attached to the game camera.

As you can see from the image, you can have a right or left hand coordinate system attached to the camera.

**Coordinate space hierarchy**

A coordinate space hierarchy is a coordinate system that **expresses its position based on its parent objec**t.

For example, assuming you have a character with an arm attached. The vector that expresses the position of that arm is based on the coordinate system of its parent.

As you can see from the example, the coordinates of the red box (child) are expressed as a vector **from** the white box (parent) position.

## 🌙 **Raycasting**

Ray casting is a common technique used in games to find when a line (ray) has hit a specific target. A common example is when a bullet is shot from a gun.As you can imagine, we’ll have to use Vectors to be able to correctly use the Ray Casting function available in most game engines.

**Let’s take this example from the Unity Tutorials:**

Here we want to shoot a projectile and find if we have hit the target (box). As you can see, the Raycast function takes two vectors as a parameter.

**One is the vector “origin” which describes the origin position of the shot in the game world. The other is the ray direction, which describes where the projectile is headed.**

The other parameters are not important for this lecture, but the first 2 vectors are essential to discuss as they describe the position and the direction of the shot

If you want to know more about ray casting, ** here** is a nice tutorial.

**More in-depth reading**

– Follows similar style of explaining dot product through examples**Better Explained Dot Product**and**Dot Product**– More in-depth explanation of dot and cross products**Cross Product**

Save this article and come back to visit it every time you need to remember something about the amazing world of vectors.

## Conclusion

I know that most of the developers out there don't want to deal with all this math, but trust me, if you want to be a good game developer, sooner or later, you are going to need it.

So you better get used to it and start to like the math behind the videogames, because even if most of the game engines have their own physics system built in, sooner or later you are going to need to interact with those systems, and it's better to know what you are doing.

Mail: info@gladiogame.scom