Unsure how to use PVectors

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

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);
      }
    }
}
1 Like

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

2 Likes

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

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/PVector.html
https://www.processing.org/reference/PVector_add_.html

Chrisir

2 Likes

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

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!

1 Like

I improved my post above a bit

1 Like

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

1 Like

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

1 Like

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

1 Like