# PVector transformation

**URL:** https://discourse.processing.org/t/pvector-transformation/3575
**Category:** Coding Questions
**Created:** [September 16, 2018, 1:19pm UTC](https://discourse.processing.org/t/pvector-transformation/3575 "2018-09-16T13:19:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ben1](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Ben1](https://discourse.processing.org/u/Ben1)
#### Post date: [September 16, 2018, 1:19pm UTC](https://discourse.processing.org/t/pvector-transformation/3575/1 "2018-09-16T13:19:31Z")

</div>

Hello there, in a recent tutorial of “the coding train” with Daniel Shiffman I was learning about PVector.

In the video ([https://www.youtube.com/watch?v=7nTLzLf7jUg](https://www.youtube.com/watch?v=7nTLzLf7jUg)) he transforms a simple bouncing ball sketch with variables like xPosition, yPosition and xSpeed, ySpeed to a sketch that uses vectors.

I thought I had understood everything and applied everything he did in my own program, but for some reason my ball does not move. I realise my structure is different as I do not have a separate class for the ball, but since it worked with xSpeed and ySpeed etc… I don’t see why it wouldn’t here.

Any suggestions on how to fix this problem are welcome.

```auto
int ballSize = 50;
PVector location;
PVector velocity;
int hitcount = 0;

void setup() {
  size(600, 400);
}

void draw() {
  location = new PVector(100, 200);
  velocity = new PVector(2, 3);
  background(51);
  drawBall();
  text("Hits: "+ hitcount, 100, 100);
}

void drawBall() {
  ellipse(location.x, location.y, ballSize, ballSize);
  location.add(velocity);
  if ((location.x>(width-(ballSize/2))) || (location.x < (0+(ballSize/2)))) {
    velocity.x = velocity.x * -1;
  }
  if ((location.y>(height-(ballSize/2))) || (location.y < (0 + (ballSize/2)))) {
    velocity.y = velocity.y * -1;
  }
}

void mouseClicked() {
  print("click");
  if (dist(mouseX, mouseY, location.x, location.y)<=40) {
    hitcount++;
  }
}

```

---

<div class="post-metadata">

### Author: ![Pyrros](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@Pyrros](https://discourse.processing.org/u/Pyrros)
#### Post date: [September 16, 2018, 1:28pm UTC](https://discourse.processing.org/t/pvector-transformation/3575/2 "2018-09-16T13:28:09Z")

</div>

Hi @Ben1,  
First off, when you declare values for variables like static vectors (not changing value), it is stylishly best to do it elsewhere than `void draw()`. `void setup()` is a great place to do this:

```auto
void setup() {
  size(600, 400);
  location = new PVector(100, 200);
  velocity = new PVector(2, 3);
}

```

So try rectifying this stilish _mistake_. 🙂

OH, it fixed it. Do you understand why?

Before you rectified this misplacement of static vectors you tried to set `location` at two different places at once. In the top of `draw` you let `location` be a PVector with the values `(100, 200)`, whereas `drawBall` sets the new values as `(102, 203)`.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [September 16, 2018, 1:30pm UTC](https://discourse.processing.org/t/pvector-transformation/3575/3 "2018-09-16T13:30:53Z")

</div>

That’s actually the cause of the bug. It’s resetting the location every frame.

---

<div class="post-metadata">

### Author: ![Ben1](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Ben1](https://discourse.processing.org/u/Ben1)
#### Post date: [September 16, 2018, 1:46pm UTC](https://discourse.processing.org/t/pvector-transformation/3575/4 "2018-09-16T13:46:53Z")

</div>

> First off, when you declare values for variables like static vectors (not changing value), it is stylishly best to do it elsewhere than `void draw()` . `void setup()` is a great place to do this:

Of coooourrrseeee,

Sticking to convention is an even better idea than it used to be.

It would have probably taken me quite a while to realise void draw is resetting the vectors echt time. Thanks a lot! @Pyrros
