Many bouncing balls

hi everyone, i have problem with my codes. i wanna try making animation many bouncing balls. my bouncing balls solved when all the balls force the all windows’s side. but the problem is the balls is still through the other. i want the balls bouncing them all everytime it close. ignore the physic problem. i just wanna know the all balls is bouncing. thanks the advice

here is the code

int ballCount = 10;

float[] x = new float[ballCount];
float[] y = new float[ballCount];
float[] xSpeed = new float[ballCount];
float[] ySpeed = new float[ballCount];
float[] size = new float[ballCount];
float[] r = new float[ballCount];
float[] g = new float[ballCount];
float[] b = new float[ballCount];

void setup() {
  size(800, 400);
  for(int i = 0; i < ballCount; i++){
    x[i] = random(width);
    y[i] = random(height);
    xSpeed[i] = random(-5, 5);
    ySpeed[i] = random(-5, 5);
    size[i] = random(10, 50);
    r[i] = random(256);
    g[i] = random(256);
    b[i] = random(256);

void bouncing(){
  for(int i = 0; i < ballCount; i++){
    x[i] += xSpeed[i];
    if(x[i] < 0 || x[i] > width || x[i] < size[i]){
      xSpeed[i] *= -1;
    y[i] += ySpeed[i];
    if(y[i] < 0 || y[i] > height|| y[i] < size[i]){
      ySpeed[i] *= -1;
    fill(r[i], g[i], b[i]);
    ellipse(x[i], y[i], size[i], size[i]);   

void draw() {
1 Like


and welcome to the forum!

Great to have you here!

Check out this example, but remove gravity


thanks for replying, i read that article. i think i still have much efforts and makes some revise my code almost all include adding the function of collision. i need time to revise my code

and thanks for jeremydouglas for editing my code in my question

1 Like