# Vectors give me same data

**URL:** https://discourse.processing.org/t/vectors-give-me-same-data/26054
**Category:** Beginners
**Created:** [December 8, 2020, 6:15pm UTC](https://discourse.processing.org/t/vectors-give-me-same-data/26054 "2020-12-08T18:15:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Banana](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/banana/32/11371_2.png) [@Banana](https://discourse.processing.org/u/Banana)
#### Post date: [December 8, 2020, 6:15pm UTC](https://discourse.processing.org/t/vectors-give-me-same-data/26054/1 "2020-12-08T18:15:49Z")

</div>

Hello everyone, i’m new here  
I want to make a slingshot that throws projectiles but i’m confronted to a problem with vector.  
The objectiv is to have a vector that stores the datas of the throw. “v3”

Can you tell me why my vectors are the same after the first throw ?

> **My code with vectors**
>
> ```auto
> PVector v1, v2;
> 
> void setup () {
> v1 = new PVector();
> v2 = new PVector();
> size(400, 400);
> }
> 
> void draw() {
> background(255);
> 
> if (mousePressed) line(v1.x, v1.y, mouseX, mouseY);
> }
> 
> void mousePressed() {
> v1.set(mouseX, mouseY);
> }
> 
> void mouseReleased() {
> v2.set(mouseX, mouseY);
> 
> print("v1 ", v1.x, ",", v1.y);
> print(" v2 ", v2.x, ",", v2.y);
> v1 = v2.sub(v1);
> println(" v3 ", v1.x, ",", v1.y);
> }
> 
> ```

> **The first three throws of the slingshot**
>
> v1 132.0 , 185.0 v2 195.0 , 233.0 v3 63.0 , 48.0  
> v1 159.0 , 225.0 v2 159.0 , 225.0 v3 0.0 , 0.0  
> v1 245.0 , 205.0 v2 245.0 , 205.0 v3 0.0 , 0.0

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 8, 2020, 7:27pm UTC](https://discourse.processing.org/t/vectors-give-me-same-data/26054/2 "2020-12-08T19:27:26Z")

</div>

> [@Banana](#):
>
> `v1 = v2.sub(v1);`

`PVector.sub(v2, v1, v1);`

> **[sub() / Reference](https://processing.org/reference/PVector_sub_.html)**
>
> Subtracts x, y, and z components from a vector, subtracts one vector from another, or subtracts two independent vectors. The version of the method that substracts two vectors is a static method and re…

---

<div class="post-metadata">

### Author: ![Banana](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/banana/32/11371_2.png) [@Banana](https://discourse.processing.org/u/Banana)
#### Post date: [December 8, 2020, 8:33pm UTC](https://discourse.processing.org/t/vectors-give-me-same-data/26054/3 "2020-12-08T20:33:32Z")

</div>

> [@GoToLoop](#):
>
> PVector.sub(v2, v1, v1);

I don’t understand what you wrote but after rereading the reference I noticed my error.

The corrected form is _**v2.sub(v1);**_ whitout the assignation

Thanks for the help 😉

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 8, 2020, 8:49pm UTC](https://discourse.processing.org/t/vectors-give-me-same-data/26054/4 "2020-12-08T20:49:13Z")

</div>

> [@Banana](#):
>
> The corrected form is _**v2.sub(v1);**_

But then you’d have to **println()** _v2_ instead of _v1_:  
`println(" v3 ", v1.x, ",", v1.y);` —\> `println(" v3 ", v2.x, ",", v2.y);`

> [@Banana](#):
>
> I don’t understand what you wrote…

`PVector.sub(v2, v1, v1);` means subtract _v2_ - _v1_ then store the result in _v1_.

---

<div class="post-metadata">

### Author: ![Banana](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/banana/32/11371_2.png) [@Banana](https://discourse.processing.org/u/Banana)
#### Post date: [December 9, 2020, 11:01am UTC](https://discourse.processing.org/t/vectors-give-me-same-data/26054/5 "2020-12-09T11:01:56Z")

</div>

> [@GoToLoop](#):
>
> But then you’d have to **println()** _v2_ instead of _v1_ :  
> `println(" v3 ", v1.x, ",", v1.y);` —\> `println(" v3 ", v2.x, ",", v2.y);`

Yes I figured it out

> [@GoToLoop](#):
>
> `PVector.sub(v2, v1, v1);` means subtract _v2_ - _v1_ then store the result in _v1_ .

Ho ok i needed more details to understand. thank you
