# Unsure how to use PVectors

**URL:** <https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597>\
**Category:** Coding Questions\
**Created:** [May 26, 2019, 7:02am UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597 "2019-05-26T07:02:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![codingNoob](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@codingNoob](https://discourse.processing.org/u/codingNoob)\
**Post date:** [May 26, 2019, 7:02am UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/1 "2019-05-26T07:02:47Z")

</div>

How do you add or subtract PVectors arrays in processing?  
This is my code for my class Ball for my game but i have to change in it PVectors

```auto
class Ball {
  int billHeadYCentre=300;
  int ballSizeX=20;
  int ballSizeY=20;
  int circleX=0;
  float circleY=random(10, 200);
  float yspeed = random(0.2, 1.2);
  float xspeed = random(1.15, 1.2);
  float x;
  float y;
  float gravity=0.1 ;
  float airfriction = 0.00011;
  float friction = 0.00011;
  float hatWidth = 100;
  float hatHeight = 2;
  int hatBounceRate = 2;
  boolean gameOverScreen =true;
  PVector[] ballSpeed= new PVector[5];
  PVector[] ballLocation= new PVector[5];
// PVector[] gravity= new PVector[1];

  void display () {
    strokeWeight(2);
    fill(255);
    ellipse(circleX, circleY, ballSizeX, ballSizeY);
    for (int i=0; i<5; i++) {
      ballSpeed[i] = new PVector(random(1.15, 1.2), random(0.2, 1.2));
      ballLocation[i]= new PVector(0, 0);
    }
 // gravity[0]=new PVector(0, 0.05);
  }

  void gravity () {
    circleY = circleY + yspeed;
    yspeed= yspeed + gravity;
    yspeed= yspeed - (yspeed *airfriction);
  } 
  void bounceBottom(int surface) {
    circleY = surface-(ballSizeY/2);
    yspeed =yspeed * -1;
    yspeed = yspeed - (yspeed * friction);
  }

  void bottomStop(int height) {
    circleY=height;
    yspeed =0;
    gameOverScreen();
  }

  void mousePressed() {
    if (mousePressed) {
      restart();
    }
  }

  void restart() {
    setup();
  }

  void bounceTop(int surface) {
    circleY = surface+(ballSizeY/2);
    yspeed = yspeed *-1;
    yspeed = yspeed - (yspeed * friction);
  }
  void xSpeed() {
    circleX += xspeed;
    xspeed -= (xspeed * airfriction);
  }
  void bounceLeft(int surface) {
    circleX = surface+(ballSizeX/2);
    xspeed*=-1;
    xspeed -= (xspeed * friction);
  }
  void bounceRight(int surface) {
    circleX = surface-(ballSizeX/2);
    xspeed*=-1;
    xspeed -= (xspeed * friction);
  }

  void stayInsideTheScreen() {
    if (circleY+(ballSizeY/2) > height) { 
      bottomStop(height);
    }
    if (circleY-(ballSizeY/2) < 0) {
      bounceTop(0);
    }
    if (circleX-(ballSizeX/2) < 0) {
      bounceLeft(0);
    }
    if (circleX+(ballSizeX/2) > width) {
      bounceRight(width);
    }
  }

  void watchHatBounce() {
    if ((circleX+(ballSizeX/2) > mouseX-(hatWidth/2)) && (circleX-(ballSizeX/2) < mouseX+(hatWidth/2))) {
      if (dist(circleX, circleY, circleX, billHeadYCentre)<=(ballSizeX/2)) {
        xspeed = (circleX - mouseX)/5;
      }
    }
    if ((circleX+(ballSizeX/2) > mouseX-(hatWidth/2)) && (circleX-(ballSizeX/2) < mouseX+(hatWidth/2))) {
      if (dist(circleX, circleY, circleX, billHeadYCentre)<=(ballSizeX/2)) {
        bounceBottom(billHeadYCentre);
      }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2019, 7:23am UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/2 "2019-05-26T07:23:54Z")

</div>

> [@codingNoob](#):
>
> float yspeed = random(0.2, 1.2);  
> float xspeed = random(1.15, 1.2);

eg. this becomes

PVector speed=new PVector(  
random(0.2, 1.2),random(1.15, 1.2) );

Access it with speed.x and speed.y

Also read the reference on PVector please

---

<div class="post-metadata">

**Author:** ![codingNoob](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@codingNoob](https://discourse.processing.org/u/codingNoob)\
**Post date:** [May 26, 2019, 7:28am UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/3 "2019-05-26T07:28:32Z")

</div>

i understand i did that, but for the places i have yspeed= yspeed + gravity  
how would i change that?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2019, 7:28am UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/4 "2019-05-26T07:28:52Z")

</div>

See reference

gravity must also be a vector (x=0)

Then use `add()` - see reference

[https://www.processing.org/reference/](https://www.processing.org/reference/)  
[https://www.processing.org/reference/PVector.html](https://www.processing.org/reference/PVector.html)  
[https://www.processing.org/reference/PVector\_add\_.html](https://www.processing.org/reference/PVector_add_.html)

Chrisir

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [May 26, 2019, 8:36am UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/5 "2019-05-26T08:36:04Z")

</div>

Hey there!

A PVector is a set of an x and a y value ( But doesn’t necessarily have to correspond to the X and Y coordinates but just two values, a X and a Y.

So let’s assume the following exists

```auto
PVector pos = new PVector(width/2,height/2);

```

This PVector starts of x at **width/2** and y at **height/2** to access each value and change it correspondingly you can do  
`pos.x -=1; pos.y-= 2;`  
Hope this helps!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2019, 1:14pm UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/6 "2019-05-26T13:14:59Z")

</div>

I improved my post above a bit

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2019, 3:34pm UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/8 "2019-05-26T15:34:46Z")

</div>

You can’t run it? Why? What happens?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2019, 3:37pm UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/9 "2019-05-26T15:37:49Z")

</div>

That looks terrible…

In the class you should only handle ONE ball and have an array of that type outside the class as a global array

Then delete all non-PVector variables you are going to replace by PVector

But normally no arrays inside a ball class

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 26, 2019, 3:40pm UTC](https://discourse.processing.org/t/unsure-how-to-use-pvectors/11597/10 "2019-05-26T15:40:07Z")

</div>

And please learn how to post code

Also please post the entire code and not only the class

Please read the reference on PVector and the tutorials on objects and about arrays

**How to post code:**

Prior to posting hit ctrl-t in processing

Then copy and paste

Then select the code section with the mouse

And hit `<\>` in the small command bar
