Help me make an array of balls bounce off each other

Hi all! I am a beginner to programming and am still working on creating simple interactions and environments with relative ease. This is my latest experiment, and it was going so smoothly until I had to make the balls from the array of balls bounce off of each other. I checked every possible thread on making objects from an array interact with each other, went through every array tutorial, and still I was unable to do it. The closest I came to was having some balls interact with some balls, but not all balls interact with all balls. (I can post the code for that as well if anybody wants to see the direction I was headed.)

I am completely stuck and even though I really wanted to figure it out on my own it's really starting to give me a headache and tons of frustration. If anybody could explain to me in a more thorough manner how exactly I can make every object interact with every object in an array, I would be tremendously grateful!

//variables for mouse-controlled ball// float mx; float my; //gravity// float g = 0.01; //array of falling balls// ball[] BALL = new ball[5]; //create falling balls// class ball { PVector loc; PVector vel; float xinert = 0.3; float yinert = 0; float classx; float classy; float classs; ball(float tempxpos, float tempypos, float tempballsize) { classx = tempxpos; classy = tempypos; classs = tempballsize; //create vector for ball location// loc = new PVector(classx, classy); } void spawnball() { //make larger balls behave 'heavier' by increasing their gravity and reducing maximum inertia// if ((classs>=20) && (classs<30)) { g = 0.01; xinert = constrain(xinert, -8, 8); yinert = constrain(yinert, -8, 8); } if ((classs>=30) && (classs<=40)) { g = 0.012; xinert = constrain(xinert, -7.5, 7.5); yinert = constrain(yinert, -7.5, 7.5); } if ((classs>40) && (classs<=50)) { g = 0.014; xinert = constrain(xinert, -7, 7); yinert = constrain(yinert, -7, 7); } if ((classs>50) && (classs<=60)) { g = 0.016; xinert = constrain(xinert, -6.5, 6.5); yinert = constrain(yinert, -6.5, 6.5); } if ((classs>60) && (classs<=70)) { g = 0.018; xinert = constrain(xinert, -6, 6); yinert = constrain(yinert, -6, 6); } if ((classs>70) && (classs<=80)) { g = 0.02; xinert = constrain(xinert, -5.5, 5.5); yinert = constrain(yinert, -5.5, 5.5); } } void moveball() { noStroke(); //create ellipse for ball// fill(0); ellipse(loc.x, loc.y, classs, classs); //create vector for ball inertia// vel = new PVector(xinert, yinert); //add inertia to location// loc.add(vel); //dampen down horizontal inertia// xinert = xinert*0.999; //vertical inertia depends on gravity// yinert = yinert + g; //'bounce' ball off left and right side for 'walls'// if (loc.x > width-classs/2) { loc.x = width-classs/2; xinert = xinert*-0.85; } if (loc.x < classs/2) { loc.x = classs/2; xinert = xinert*-0.85; } //bounce ball off top and bottom sides for 'ground and ceiling'// if (loc.y > height-classs/2) { loc.y = height-classs/2; yinert = yinert*-0.85; } if (loc.y < classs/2) { loc.y = classs/2; yinert = yinert*-0.85; } //make balls bounce off of mouse ball// if (dist(loc.x, loc.y, mouseX, mouseY)<=(classs/2 + 40)) { //values are random and derived from experiments, they sort of work. Would love it if someone came up with something more efficient though...// xinert = ((loc.x - mouseX)/60 + (mouseX - pmouseX)*0.5); yinert = ((loc.y - mouseY)/60 + (mouseY - pmouseY)*0.5); //restrain mouse ball from going inside other balls// mouseX = pmouseX; mouseY = pmouseY; } } } void setup() { size(800, 800); frameRate(400); //create five balls// for (int i = 0; i < 5; i++) { BALL[i] = new ball(random(0, 800), 40, random(20, 80)); } } void draw() { background(255); //spawn five balls// for (int i = 0; i < 5; i++) { BALL[i].spawnball(); BALL[i].moveball(); } //draw mouse-controlled ball// fill(255, 100, 100); ellipse(mx, my, 80, 80); mx = mouseX; my = mouseY; }

Answers

  • You can check some of the examples in the Processing foundation's website. For example:

    https://processing.org/examples/bouncybubbles.html

    Kf

  • edited September 2017

    I did, many times, but it does nothing in helping me fathom how this works exactly. Sin? Cos? Just... what? There's a reason I used vectors, and it is to avoid trigonometry such as this. I simply need a function saying 'if the distance between two balls is less than their radii, their inertia should change according to their position & current inertia', or, to be more precise, I just need to know how to say 'IF THE DISTANCE BETWEEN TWO BALLS OF THE SAME ARRAY IS LESS THAN THEIR RADII....' and I can easily come up with the rest of the formula.

  • IF THE DISTANCE BETWEEN TWO BALLS OF THE SAME ARRAY IS LESS THAN THEIR RADII...

    // two balls, a and b, are colliding if the distance is less than the sum of their radii
    if (dist(a.x, a,y, b.x, b.y) < (a.r + b.r)) {
        ...
    }
    

    it's right there. there are probably 1000 examples of this on the forum.

    https://processing.org/examples/bouncybubbles.html

    also, please don't format your code using the backticks, they are meant for single lines. highlight the code and press ctrl-o and you'll get syntax colouring and line numbers which make it easier to refer to your mistakes...

Sign In or Register to comment.