Vectors in Game Development

Game Development

Mathematics and Physics

Programming

Thumbnail for the article

Vectors are one of the most important things in game development. They exist in every game engine, and game developers work with them constantly. They even have an entire branch of mathematics dedicated to them, called “vector math”

Vectors are used in game development in many different ways, such as position, velocity, and direction. They’re also used for physics calculations, such as collision detection, and even in graphics techniques

Defining Vectors

A vector is length and direction A vector’s direction is what makes it different from a line segment (length without direction)

There’s a special kind of vector called a “unit vector,” and what makes this vector special is that its length equals one, which means we can multiply any value by this vector to increase or decrease its length without changing its direction, making it an excellent tool for defining directions

Multiplying a vector by a whole positive number increases its length, by a positive fraction shortens it, and by a negative number reverses its direction

There are several ways to represent vectors. The most common method uses the Cartesian coordinate system—shown in the image—which uses two points to define a vector’s direction and length: the “start” point and the “end” point. In game engines, the start point of a vector is always the zero point. These coordinates are denoted “X” and “Y”

Uses of Vectors in Game Development

Position

The most common use in game development is representing the position of objects. An object’s position is defined by a vector on the coordinate grid of the game world

For example, a player’s position in a 2D game can be represented by the vector (X, Y), where X and Y represent a value on both the horizontal and vertical axes, and from these two values on the axes we can get a specific point on the coordinate grid

Velocity

An object’s velocity is a vector that defines the object’s speed and direction of movement. For example, a car’s velocity in a racing game can be represented by the vector (X, Y) * S, where (X, Y) is a unit vector (the direction), and S is the car’s speed (the length), giving us a vector that contains both direction and magnitude together

So all we need to move the car in a certain direction at a certain speed is to repeatedly add the movement vector’s value to the car’s position vector

Physics and Collision

Direction is extremely important in physics calculations, since a different force direction changes how that force affects an object. That’s why vectors are used to simulate the physical behavior of objects in the game world. This includes things like gravity, friction, and collision

To detect collisions, for example, we calculate the distance between the two objects’ center points and the sum of their radii. If the distance between them is less than the sum of their radii, that means the objects have collided

What if a collision happens between the player’s character and one of the world’s walls? The character shouldn’t be able to enter the wall. In this case, the collision between the two objects is disallowed, and to push them apart, we calculate the difference between the distance separating the two center points and the sum of their radii, then move the two objects apart by a distance equal to half that difference if both objects are movable (character and character). But if one object is movable and the other is fixed (character and wall), the movable object moves away from the fixed object by the full difference

Examples

Here are some additional examples of how vectors are used in game development:

  • Pathfinding Vectors are used to represent the paths that characters and enemies can take through the game world
  • Targeting They can be used to determine which objects fall within the range of a character’s attack or spell (by calculating distance)
  • Shooting A specific direction is determined starting from the player and the weapon’s direction, and that’s added to the bullet along with some speed
  • Grenades Vectors are used to calculate the blast radius of grenades and other explosives
  • Projectile motion This is used to simulate the movement of objects affected by gravity, such as projectiles and grenades
  • Car physics Vectors are used to simulate car physics, such as acceleration, braking, and turning
  • Wind physics Vectors can simulate wind physics, such as storms, breezes, and hurricanes

These are just a few examples of how vectors are used in game development. Vectors are a useful tool that can be used to solve a wide variety of problems

Now, let’s get to the most important part of this article: vector math, and how to use it

Vector Math

A single vector can solve many problems and serve many functions in the world of game development, but you may need to work with several vectors at once, and that’s done through mathematical operations like addition, subtraction, and multiplication

Addition

When adding vectors, we combine two or more vectors to create a new vector. The sum of two vectors is a third vector that has the same direction as the original vectors, and whose magnitude is the sum of the original vectors’ magnitudes

There are two common ways to add vectors: the triangle method and the parallelogram method. The triangle method is the simpler of the two

To add vectors using the triangle method, draw the two vectors so that the end of one is the start of the other, giving you two vectors that aren’t in a straight line. Forming a third vector to close the triangle gives you the result of their sum

Algebraically, it’s even simpler—you add the X values together, and add the Y values together Example: (5, 3) + (2, 8) = (7, 11)

Subtraction

As for subtracting vectors, algebraically it’s just as easy. We can easily subtract the vector (5, 5) from the vector (3, 3), giving us a new vector with the value (-2, -2) (5, 5) - (3, 3) = (-2, -2)

But what’s different, or strange, is the shape of the resulting vector—you’ll find it barely seems related to the other two vectors, beyond just the math operation itself

But as soon as you place the tail of the resulting vector at the head of one of the vectors used in the equation, you’ll always find that its head meets the head of the other vector

Multiplication

When we decide to multiply vectors together, the math isn’t as smooth as addition and subtraction—there are several methods for multiplying vectors: cross product, dot product, scalar multiplication

Vector multiplication isn’t something that’s easy to grasp the first time around, and quite often you’ll find that the game engine you’re using gives you a fair amount of built-in help with it

But out of respect for the complexity of vector multiplication, nothing more will be said about it here beyond the fact that it isn’t easy to explain, and the platform will soon publish a dedicated article on the subject

Well-Known Vectors

Because vectors are so useful, both mathematically and physically, you’ll find that there’s a set of vectors that have already been predefined, simply because we reuse these vectors over and over again

Here are the most important of these predefined vectors

  • Unit vector: A vector whose length is always one, so length no longer matters—what matters is direction, not length
  • Normal vector: A vector perpendicular to a surface, whose length equals the area of the surface it’s perpendicular to
  • Velocity vector: A number denoting speed is added to a unit vector, so we say the object is moving in that direction at that speed
  • Zero vector: A vector with zero magnitude that doesn’t point in any direction (0, 0)

Summary

In conclusion, vector math is a powerful tool that can be used to solve a wide variety of problems in game development

It’s used to represent a variety of things, such as an object’s position, velocity, and acceleration, and its direction of movement. It’s also used to calculate things like collision detection, physics, and animation

I hope this article has helped you understand some of the fundamentals of vectors, their usefulness, and how they can be used in game development. Let me know your thoughts on it in the comments, and tell me when you first started noticing their usefulness, or the last time you used them

Sources: 1, 2, 3, 4, 5