# p5.Vector returns strange and negative x and y values

**URL:** <https://discourse.processing.org/t/p5-vector-returns-strange-and-negative-x-and-y-values/23035>\
**Category:** Coding Questions\
**Created:** [August 4, 2020, 4:42pm UTC](https://discourse.processing.org/t/p5-vector-returns-strange-and-negative-x-and-y-values/23035 "2020-08-04T16:42:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbrarShahriar](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@AbrarShahriar](https://discourse.processing.org/u/AbrarShahriar)\
**Post date:** [August 4, 2020, 4:42pm UTC](https://discourse.processing.org/t/p5-vector-returns-strange-and-negative-x-and-y-values/23035/1 "2020-08-04T16:42:00Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I was experimenting with one of Shiffman’s NOC examples. To be specific the steering behavior one. I added a target for my vehicle with the createVector() method (x = 200, y = 300). But when I ran the code, those x and y values turned to negative 0 point something. And it returns slightly different values each frame.  
Here’s the console:

 ![Screenshot_20200804-223416](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0fa37009f3698167efb66a28545e848d12429392.png)

Here’s the sketch file:

```auto
let mover = new Array(1);
var pos;

function setup() {
				createCanvas(345,400);
				
				pos = createVector(200, 300);
				

				for(var i = 0; i < mover.length; i++) {
								mover[i] = new Mover(random(2), 30,30);
				}
}

function draw() {

				background(51);
				
				for(var i = 0; i < mover.length; i++) {
								
								mover[i].seek(pos);
								
								mover[i].update();
								mover[i].display();
								mover[i].checkEdge();
				}
				
				//console.log(pos.x);
				fill(155);
				ellipse(pos.x,pos.y, 10,10);
}

```

There’s also something strange I noticed. When I print pos.x/pos.y on the setup function, it prints the exact values (200,300). But when I print it inside draw, that’s when things get messy.  
Here’s the Vehicle class:

```auto
function Mover(m, x, y) {
				this.loc = createVector(x,y);
				this.velocity = createVector(0,0);
				this.accelerate = createVector(0,0);
				
				this.mass = m;
				
				var maxSpeed = 5;
				var maxForce = 0.1;
			
				this.update = function() {
								this.velocity.add(this.accelerate);
								this.loc.add(this.velocity);
								
								this.accelerate.mult(0);
				}
				
				this.display = function() {
								stroke(0);
								fill(255);
								
								ellipse(this.loc.x, this.loc.y, this.mass*16, this.mass*16);
				}
				
				this.checkEdge = function() {
								if(this.loc.x < 0 || this.loc.x > width) {
												this.velocity.x *= -1;
								}
								if(this.loc.y < 0 || this.loc.y > height) {
												this.velocity.y *= -1;
								}
				}
				
				this.applyForce = function(force) {
								var f = force.div(this.mass);
								this.accelerate.add(f);
				}
				
				this.seek = function(target) {
								var desired = target.sub(this.loc);
								desired.setMag(maxSpeed);
								
								var steer = desired.sub(this.velocity);
								steer.limit(maxForce);
								
							 this.applyForce(steer);
				}
}

```

I searched a lot around but didn’t find any solution. A brief explanation would be really helpful.

---

<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:** [August 4, 2020, 5:01pm UTC](https://discourse.processing.org/t/p5-vector-returns-strange-and-negative-x-and-y-values/23035/2 "2020-08-04T17:01:10Z")

</div>

> [@AbrarShahriar](#):
>
> `var desired = target.sub(this.loc);`

> **[reference | p5.js](https://p5js.org/reference/#/p5.Vector/sub)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

`const desired = p5.Vector.sub(target, this.loc);`

---

<div class="post-metadata">

**Author:** ![AbrarShahriar](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@AbrarShahriar](https://discourse.processing.org/u/AbrarShahriar)\
**Post date:** [August 4, 2020, 5:05pm UTC](https://discourse.processing.org/t/p5-vector-returns-strange-and-negative-x-and-y-values/23035/3 "2020-08-04T17:05:37Z")

</div>

It worked 🙂. But can you explain why it didn’t work with my code?

---

<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:** [August 4, 2020, 5:11pm UTC](https://discourse.processing.org/t/p5-vector-returns-strange-and-negative-x-and-y-values/23035/4 "2020-08-04T17:11:48Z")

</div>

`target.sub(this.loc);` mutates the parameter _target_, which is the passed argument _pos_: `mover[i].seek(pos);`

> <https://github.com/processing/p5.js/blob/1.1.9/src/math/p5.Vector.js#L528-L545>

`p5.Vector.sub(target, this.loc);` automatically creates a _target_ p5.Vector object ([**copy()**](https://p5js.org/reference/#/p5.Vector/copy) of the 1st parameter _v1_) which stores the result between the 2 passed p5.Vector object arguments:

> <https://github.com/processing/p5.js/blob/1.1.9/src/math/p5.Vector.js#L2048-L2062>
