# Vector Addition

Adding vectors together is one of simplest operation you can do. Take each x,y,z values and add them together. Mathematically speaking this means the following.

$$
\begin{bmatrix}
x \ y \ z
\end{bmatrix}
=============

\begin{bmatrix}
x1 \ y1 \ z1
\end{bmatrix}
\+
\begin{bmatrix}
x2 \ y2 \ z2
\end{bmatrix}
\iff
\begin{matrix}
x = x1 + x2\\
y = y1 + y2\\
z = z1 + z2
\end{matrix}
$$

This is exactly what the [add](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/util/Vector.html#add\(org.bukkit.util.Vector\)) method does. So writing

```java
Vector greenVector = new Vector(3, 0, 1);
Vector orangeVector = new Vector(2, 0, 3);
Vector purpleVector = greenVector.clone().add(orangeVector);
```

{% hint style="info" %}
The y component is 0 because the tutorial explains 2D examples in the xz-plane.
{% endhint %}

{% hint style="warning" %}
Adding vectors without cloning them beforehand means that `orangeVector` in this example would actually be modified and `purpleVector` is just like `orangeVector`  (they would point to the same value in memory).

Often this is not desired. Use clone before adding.
{% endhint %}

![Addition of two vectors.](https://3012238209-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3CiKZGX91xk3wTXNDkac%2Fuploads%2F67lLAPV5Ujlpx8EvKiIs%2Fvector%20addition.gif?alt=media\&token=81d54539-52e7-4a86-848f-d2c783258510)

You can also add arbitrary many vectors together.

```java
Vector greenVector = new Vector(3, 0, 1);
Vector orangeVector = new Vector(-2, 0, -1);
Vector yellowVector = new Vector(-3, 0, 4);
Vector purpleVector = greenVector.clone().add(orangeVector).add(yellowVector);
```

![Chaining additions.](https://3012238209-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3CiKZGX91xk3wTXNDkac%2Fuploads%2FVkTbCikk7o3JfJM2d0W5%2Fvector%20addition%20multiple.gif?alt=media\&token=92469f24-0449-40bc-b3c5-6410732380d1)

Vector Addition is commonly used for ,what I call, *offset vectors*. So when doing operations around the origin, adding back in the actual location in the world where whatever we just calculated should take place/be shown is a key step.
