Welcome back to my vector math series. In the last post, we went over a brief introduction to define what a vector is, the type of data it represents, and how it can be interpreted geometrically in a 2D coordinate space. Let’s now learn how to add and subtract vectors!
Adding vectors consists of adding each corresponding component, that is to say, given and , then will equal .
Say is and is . If we were to add these vectors we would get a resulting vector . We added each corresponding component of the vectors like so; x: 2 + 3 = 5, y: 4 + 2 = 5, and z: 0 + 0 = 0.
The geometric interpretation of vector addition can be seen by drawing the vectors so that the head of one is at the tail of the other. The line that connects the first vector’s tail with the head of the second vector is the result of the addition.
Vector addition is commutative, meaning . If we draw two vectors above the center addition line, we would have a parallelogram which proves that this is true. This is called the parallelogram rule.
A common use of vector addition is the use of an offset vector. For example, if we were implementing a follow camera that follows the player, we could add an offset vector to the player position that represents the direction and distance of the camera in relation to the player’s current position.
To subtract vectors, we find the difference between the corresponding components of both vectors.
For example, given and , then will equal .
Remember, Subtraction is not commutative, so the order of the vector subtraction is significant. If you want the vector from to , the order would be .
Using our previous data as an example, we have and . If we were to subtract and we would get a resulting vector . We subtracted each corresponding component of the vectors like so; x: 3 – 2 = 1, y: 2 – 4 = -2, and z: 0 – 0 = 0.
The geometric interpretation of subtraction can be seen by drawing the vectors so that their tails originate at the same point, then placing a vector from the head of one to the head of the other.
A gameplay example of vector subtraction is subtracting the player’s position from the position of their next goal or item to collect in the world. If you’ve ever seen an arrow at the player’s feet or a part of the UI, they could be using this method to provide the direction.
Hopefully, you found these examples of vector addition helpful. We still have plenty to talk about when it comes to vectors, so look forward to more posts in the series coming soon.