Adding and Subtracting Vectors

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

Adding vectors consists of adding each corresponding component, that is to say, given \vec{a} and \vec{b}, then \vec{a} + \vec{b} will equal a_{x} + b_{x}, a_{y} + b_{y}, a_{z} + b_{z} .

Say \vec{a} is \langle 2, 4, 0 \rangle and \vec{b} is \langle 3, 2, 0 \rangle . If we were to add these vectors we would get a resulting vector \langle 5, 6, 0 \rangle. 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 \vec{a} + \vec{b} = \vec{b} + \vec{a}. 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.

Subtracting Vectors

To subtract vectors, we find the difference between the corresponding components of both vectors.

For example, given \vec{a} and \vec{b}, then \vec{b} - \vec{a} will equal b_{x} - a_{x}, b_{y} - a_{y}, b_{z} - a_{z} .

Remember, Subtraction is not commutative, so the order of the vector subtraction is significant. If you want the vector from \vec{a} to \vec{b}, the order would be \vec{b} - \vec{a}.

Using our previous data as an example, we have \vec{a} = \langle 2, 4, 0 \rangle and \vec{b} = \langle 3, 2, 0 \rangle. If we were to subtract \vec{b} and \vec{a} we would get a resulting vector \langle 1, -2, 0 \rangle. 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.

Image from Wikipedia

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.

Summary

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.

Want to connect? Follow me on Twitter and LinkedIn!

Take care.
Stay awesome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s