PVector Function Parameters HELP

I’ve created a program that depicts a ball bouncing off of the floor(actually respawning once it touches the floor) to be used in a larger set of code. I would like to be able to duplicate 4 more bouncing balls however I have used PVectors to store both the velocity and location of the ball at any on time. I’m struggling to transform these PVectors from global variables to local variables to the function that contains the ball itself, however, I am unable to do so without destroying the code itself. I’m not sure how to store PVectors as parameters within the balls function and am also unable to localize the PVectors without causing an error whereby the ball continually resets its position. The problem with the current code is the more balls I create by reusing the balls function, the faster the speed of all balls within the code becomes.

This is my code:

PVector location = new PVector(random(0,50),20);
PVector velocity = new PVector(random(1.15,1.2),random(0.2,1.2));
int gravity = 1;
int rad = 30;
void setup(){
  frameRate(40);
  size(640,480);
  background(0);
}

void draw(){
  background(0);
  pudding();
}

void pudding(){
  location.add(velocity);
  if((location.x>width) || (location.x<0)){
    velocity.x = velocity.x * -1;
  }
  if((location.y>height-rad)){
    location.x = random(0,50);
    location.y = 20;
    velocity.x = random(1.15,1.2);
    velocity.y = random(0.2,1.2);
  }
  ellipse(location.x,location.y,rad,rad);
  velocity.y = velocity.y + gravity;
}

Am I missing something obvious?

1 Like

Hey there ! First of all you should format your code with crtl-t in Processing and use these guys (in here aka the Post on the Forum if unclear):

```
Code goes here
```
To fix your problem you should look into building a class which stores the location of the ball and its velocity and manages it for each object. Example template here:


public class Ball {
  PVector pos;
  int rad;
  PVector velocity;
  Ball( PVector setPos, PVector setVelocity, int setSize ) {
    pos = setPos;
    velocity = setVelocity;
    rad = setSize;
  }


  void pudding() {
    location.add(velocity);
    if ((location.x>width) || (location.x<0)) {
      velocity.x = velocity.x * -1;
    }
    if ((location.y>height-rad)) {
      location.x = random(0, 50);
      location.y = 20;
      velocity.x = random(1.15, 1.2);
      velocity.y = random(0.2, 1.2);
    }
    ellipse(location.x, location.y, rad, rad);
    velocity.y = velocity.y + gravity;
  }

What your trying to do is assign one set of data to many things. Having only one position can work for reusing if your ball goes off screen but displaying four balls with just one set of information that messy to code up. So why worry about that ! Build a class declare 4 objects and let the computer worry ! That what it there for :smiley:

Also not sure if you want to be resetting the ball every time it hits the ground or bouncing it back but this is how it can be done:

if ((location.y>height-rad)) {
    velocity.y *= -1;
  }
1 Like

For a general introduction to creating (and using!) a class object, see this tutorial:

If you don’t want to dive into objects yet, then because you only really need four floats per ball (x, y, velx, vely) can instead use an array.

Here is a very simple example – it has no bouncing implemented, just motion from location and velocity:

float[][] balls;
void setup(){
  balls = new float[10][4];
  for(int i=0; i<10; i++){
    // create random locations and directions for each ball
    balls[i][0] = (int)random(width);  // x
    balls[i][1] = (int)random(height);  // y
    balls[i][2] = random(-1, 1);  // velx
    balls[i][3] = random(-1, 1);  // vely
  }
}
void draw(){
  background(128);
  // for each ball
  for(int i=0; i<10; i++){
    // change location by velocity
    balls[i][0] += balls[i][2];
    balls[i][1] += balls[i][3];
    // draw ball
    ellipse(balls[i][0], balls[i][1], 20, 20);
  }
}

If all you need are numbers then you can extend this design – for example, adding a size as a fifth number, a line thickness as a sixth, et cetera. In the long term, however, classes based on PVector are usually the way to go – they are easier to work with, and much more is possible, such as adding a name to each object, storing its shape, and most importantly building in custom methods.