How do I bounce all the balls?

Nice to meet you.

Use this site for the first time.

I am worried about not doing anything rude.

I want you to help.

I want all the balls to bounce the wall.

However, only one ball bounces the wall.

Please tell me how to improve it.

This program is trying to realize the following functions.

1 Generate all balls so as not to overlap.
2 I want to move all the balls in any direction.

However, 2 is incomplete.
Only one ball moves.

I would like advice on how to improve it.

I am an amateur of processing.
It has been about three months since I started.

I spent a lot of time and finished the program so far.

The great technology of the great people was also helpful.

However, you can not bounce more than one ball.

I understand that I have a lack of study.

However, I would like you to lend me some power to move forward.

Thank you in advance.

`type or paste code here`


ArrayList <PVector> drops0;


PVector location;
PVector velocity;



int num = 10; //num = circles.size()

float r = 10;
float r2 = r*2;


void setup() {

  drops0 = new ArrayList <PVector>();

  ellipseMode(RADIUS);//RADIUS
  frameRate(60);
  background(255);
  size(1200, 600);

  while (drops0.size() < num) {


    velocity = new PVector (1, 1);

    float xxx = random(r, width-r);
    float yyy = random(r, height-r);

    location = new PVector(xxx, yyy, r);


    boolean overlapping = false;

    for (PVector test : drops0) {

      if (dist(location.x, location.y, test.x, test.y) < (location.z+2 + location.z+2)) {
        overlapping = true;
        break;
      }
    }
    if (!overlapping) {
      //drops0.add(velocity);
      drops0.add(location);
    }
  }
}



void draw() {
  background(255);

  noStroke();

  move();

}




void move() {


  location.add(velocity);

  for (int i=0; i < num; i++) {

    PVector location = drops0.get(i);

    if ( i < 20 ) {
      fill(50, 50, 200, 100);
    } else if ( i+(20) < num ) {
      fill(150, 0, 0, 100);
    }
    ellipse(location.x, location.y, r, r);
  }



  if (location.x < r || location.x > width-r) {
    velocity.x = velocity.x * -1;
  }

  if (location.y < r || location.y > height-r) {
    velocity.y = velocity.y * -1;
  }
}




1 Like

when you want to have multiple instances of some object i think it’s usually recommended to build a class for that object. you have a single drop which runs well enough now but if you wanted more each would need their own properties such as location velocity acceleration and radius so you would build a class containing those variables with a constructor enabling you to set each to distinct values and such. i’ve done a rough example below and kept it as close to you original naming and formatting as possible. something good to implement now would be collisions between drops. best of luck.

class Drop
{
  PVector location;
  PVector velocity;
  PVector acceleration;
  
  float r, r2;
  
  public Drop(float x, float y, float r) {
    this.location = new PVector(x, y);
    this.velocity = new PVector();
    this.acceleration = new PVector();
    this.r = r;
    this.r2 = r * 2;
  }

  void addForce(float x, float y) {
    velocity.x += x;
    velocity.y += y;
  }
  
  void update() {
    velocity.add(acceleration);
    acceleration.mult(0);
    location.add(velocity);
  }
  
  void present() {
    ellipse(location.x, location.y, r, r);
  }
}

int num ;
ArrayList <Drop> drops0;

void setup() {
  size(1200, 600);
  frameRate(60);
  
  ellipseMode(RADIUS);//RADIUS
  noStroke();  
    
  num = 10;    
  drops0 = new ArrayList <Drop>();

  while (drops0.size() < num) {

    //choose a random radius location and velocity
    float r = random(32, 64);    
    float x = random(r, width - r);
    float y = random(r, height - r);
    PVector vel = PVector.random2D();
    
    Drop newDrop = new Drop(x, y, r);
    newDrop.addForce(vel.x, vel.y);

    boolean overlapping = false;
    for (Drop drop : drops0) {
      if (dist(x, y, drop.location.x, drop.location.y) < newDrop.r2 + drop.r2) {
        overlapping = true;
        break;
      }
    }
    if (!overlapping) {
      //drops0.add(velocity);
      drops0.add(newDrop);
    }
  }
}

void draw() {
  background(255);
  move();
}

void move() {
  for (int i=0; i < num; i++) {
    Drop drop = drops0.get(i);
    
    drop.update();
    if(drop.location.x < drop.r || drop.location.x > width - drop.r) {
      drop.velocity.x = -drop.velocity.x;
    }
    if(drop.location.y < drop.r || drop.location.y > height - drop.r) {
      drop.velocity.y = -drop.velocity.y;
    }
    
    if ( i < 20 ) {
      fill(50, 50, 200, 100);
    } else if ( i+(20) < num ) {
      fill(150, 0, 0, 100);
    }
    
    drop.present();
  }
}
2 Likes

Dear hotfooted

Thanks for the great reply! !

I am very moved.

I would like to express my heartfelt gratitude for your immediate reply and for giving me a great example.

thank you very much.

We will use this example as a teaching material to further study.

We will learn how the example taught to you is working.

Thank you very much.
You are my savior.

I pray from the bottom of my heart to be lucky.

1 Like