Problem with reusing a function

Hi! Based on Daniel Shiffman’s sketch Bouncing Ball, im trying to display a secind ball on the screen unsuccessfully. However both balls appears, they dont move at all. Can someone explain me where i’m making mistakes? thanks.

float circleX;
float circleY;
float xspeed = 4;
float yspeed = 4;
float value = 0;

void setup() {
  size(640,360);
  circleX = 0; 
  circleY = 0; 
  background(51);
}

void draw() {
  displayBall(0,0);
  moveBall(0,0);
  checkEdges();
  displayBall(50,50);
  moveBall(50,50);
}

void displayBall(float circleX, float circleY) {
  fill(255);
  stroke(0);
  ellipse(circleX, circleY,32,32);
}

void moveBall(float circleX, float circleY) {
  circleX = circleX + xspeed;
  circleY = circleY + yspeed;
}

void checkEdges() {
    if(circleX > width || circleX < 0) {
    xspeed = xspeed * -1;
  }
  if(circleY > height || circleY < 0) {
    yspeed = yspeed * -1;
  }
}
1 Like

Please, when pasting code, use the little </> icon to format it with monospace and all that fancy stuff to make it more readable and stuff.

You might want to keep in mind that I wrote this without looking at the example itself, as I can’t seem to find the exact sketch that looks like this one. The closest match is this one, but it’s called “Bouncing Ball with Vectors” and seems to be pretty different to what you have: https://www.processing.org/examples/bouncingball.html

The problem here is how you use these functions.
displayBall(...) expects coordinates of the ball, and in draw() you use it as displayBall(0,0);, where both zeroes are constant and never change. So, displayBall(...); always draws the ball at (0,0); and never anywhere else.

Next, moveBall(...) also expects coordinates of a ball, and then uses xspeed and yspeed to add to them. Same problem.
Moreso, it creates its own values called circleX and circleY, which refer to the input that the function gets and not to the actual circleX and circleY values themselves, and adds speed values to them - which are then immediately discarded as soon as the function ends as they are not needed.
The problem is that when you name arguments of a function with the same name as some variable, this function will always use its argument and not that variable, so real, global circleX and circleY variables get ignored by it.

Function checkEdges() is made properly and operates on global values, but since no other function changes circleX and circleY in any way, it’s practically doing the same calculations with same numbers every time its run.

And even if you were to fix all of that up, the code would still be faulty, as all 3 functions will still operate on same circleX, circleY, xspeed and yspeed variables, which only correspond to a single ball. So, at best, you would be calculating same math on one ball data twice.

For such a thing, I suggest to learn arrays, and how to make your own objects and put functions in them - this way, you can just create a new variable Ball[], which is an array of any number of variables Ball, where each Ball will have its own circleX, circleY, xspeed and yspeed variables and functions that work on these variables.

Or, if you don’t want to do that yet, I suggest instead dialing it back to 1 ball, making sure that it works, and then create new circleX2, circleY2, xspeed2 and yspeed2 variables and copies of these functions that work on these variables, and then run them inside of draw() independently.

Sorry if I bombarded you with information and explanations, it’s 3:03 AM here in Russia and I drank too much coffee. :smiley:

2 Likes

thanks Architector! haha no worries! i’ll take my time to fully get all the concepts you have just told me. Thanks again!