# Trouble with Vector.add

**URL:** https://discourse.processing.org/t/trouble-with-vector-add/24170
**Category:** Coding Questions
**Created:** [September 27, 2020, 7:24pm UTC](https://discourse.processing.org/t/trouble-with-vector-add/24170 "2020-09-27T19:24:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![noonscoomo](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@noonscoomo](https://discourse.processing.org/u/noonscoomo)
#### Post date: [September 27, 2020, 7:24pm UTC](https://discourse.processing.org/t/trouble-with-vector-add/24170/1 "2020-09-27T19:24:21Z")

</div>

Hi,

I am in trouble with the p5.Vector.add() method and I don’t see why.  
My code generates a 2D Vector in setup, adds a random 2D vector in draw, pushes the result in an array and draws the whole array in a loop. If I add the vectors with  
v.add(vr);  
I get an array with identical vectors in it and nothing works, if I use the static method  
v = p5.Vector.add(v,vr);  
everything works fine and I don’t understand why.  
Here’s the code:

```auto
let v;
let path = [];

function setup() {
  createCanvas(600, 600);
  v = createVector(200, 200);
}

function draw() {
  background(220);
  let vr = p5.Vector.random2D();
  
  v.add(vr); // this does not work
  //v = p5.Vector.add(v,vr); // this does
  path.push(v);
  
  for (i = 0; i < path.length - 1; i++) {
    line(path[i].x, path[i].y, path[i+1].x, path[i+1].y);
  }
}

```

---

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [September 27, 2020, 7:48pm UTC](https://discourse.processing.org/t/trouble-with-vector-add/24170/2 "2020-09-27T19:48:26Z")

</div>

This is down to a little quirk of JavaScript…

When you write `v.add(vr)`, you are adding `vr` to the p5 vector object `v`. This makes intuitive sense.

However, it gets a little confusing when you push `v` into the array `path`. Because of the way JavaScript deals with objects (by reference rather than by value), the `v` you have pushed to the array is not its own new vector as you might expect, but rather it refers to the same object as the `v` that you define at the top of your code. As a result, when you call `v.add(vr)`, you are referring to not only to the `v` you originally defined, but any other `v` that you have used elsewhere.  
This means when you add `vr` to `v`, you are adding the vector to all of the vectors in the `path` array, so they all move simultaneously, meaning that when lines are drawn between them, they just appear as a single point.

A simple solution to this (other than using `p5.Vector.add`) is to push `v.copy()` instead of `v`. This uses the p5 vector `copy` method which creates a new identical vector, which does not refer to the original vector object (`v`), meaning that if `v` is changed, this copy will not be.

The reason that `v = p5.Vector.add(v,vr);` does not produce the same problem is that the `v` object is replaced with a new vector object every time you push a vector to the array. This means that `v` no longer refers to the vectors pushed to the array, so changing it does not affect the vectors already in the array.

---

<div class="post-metadata">

### Author: ![noonscoomo](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@noonscoomo](https://discourse.processing.org/u/noonscoomo)
#### Post date: [September 27, 2020, 8:03pm UTC](https://discourse.processing.org/t/trouble-with-vector-add/24170/3 "2020-09-27T20:03:07Z")

</div>

Hi TheWizardBear,

Thank you so much for that clear explanation. I understand that now, it’s good to know and I think it’s a nice feature to have both options.

---

<div class="post-metadata">

### Author: ![KaliBrain](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kalibrain/32/10811_2.png) [@KaliBrain](https://discourse.processing.org/u/KaliBrain)
#### Post date: [October 8, 2020, 7:27am UTC](https://discourse.processing.org/t/trouble-with-vector-add/24170/4 "2020-10-08T07:27:40Z")

</div>

I didn’t ask the question yet I had a feeling I would learn something valuable. Wasn’t disappointed. Thanks!
