I am porting this post from the alpha board over to the new one because I am just incapable of figuring out how to do ball collisions.

This is the first time I've used the class menthology and I guess I just haven't figured out the innerworking of using classes.

I don't quite know how you can check the distance between 5 balls (for example). Wouldn't you need to tell the programm to check the distance to the next iteration of the ball and the one after it (and so on)?

just can't get my head around this :/

noob

mflux

**Quote:**As far as ball to ball collision, simply make a function (call it "collide()" or something) inside the ball class, then have it loop through all the other instances of that class (in your case.. Ball[]). Have it test each ball's distance against itself, minus their radius.

If they are within radius-length of each other, swap their velocities (direction of motion).

fish-face

**Quote:**mflux's collision method is slightly inaccurate; it only works for head on collisions. If two balls 'glance' each other then their momentum is shared between them according to the angle at which they strike. To do collision checking simply:

Step 1: Iterate through Ball classes, check if this one is touching/covering another one

Step 2: Move the two balls apart so that they are edge to edge

Step 3: Work out the momentum to give to each ball. This can be worked out thus:

Each ball's momentum is comprised of two separate, equal momentums each at 45 degrees to its direction of motion. If the balls collide head on (collision angle of 0 degrees) then all of both momentums is given to the other. If the balls are of equal masses and velocities, this causes them both to halt.

If they collide at 45 degrees, then all of one momentum is given, but the momentum that is in a direction perpendicular to the angle of collision is retained. This causes the balls to glance of each other, at (again, only if equal masses + velocities, opposite directions) 45 degree angles compared to their original directions.

At a collision angle of 90 degrees, there has not actually been a collision, so all momentum is retained.

This is difficult to explain, but you can have great fun sliding coins across the table to work stuff out.

**Code:**

import processing.opengl.*;

Ball[] ball;

int objectnumber = 500;

void setup (){

size (900,700,OPENGL);

framerate(30);

//colorMode(RGB, 100);

ball = new Ball[objectnumber];

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

ball[i] = new Ball(random(50,500),random(50,400),random(1,20),random(1,20), 10, 30);

}

}

void draw(){

background(#280915);

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

ball[i].draw ();

ball[i].move ();

// Collide Part // ball[i].collide();

}

}

class Ball {

float xpos;

float ypos;

float xspeed;

float yspeed;

float diameter;

float diameter2;

//float alpha_ = 100;

int xdirection = 1;

int ydirection = 1;

Ball (float xp, float yp,float xs, float ys, float d, float d2){

xpos = xp;

ypos = yp;

xspeed = xs;

yspeed = ys;

diameter = d;

diameter2 = d2;

}

void draw(){

//smooth();

ellipseMode(CENTER);

stroke(40);

fill (148,11,53,xspeed*5); // fill (148,11,53,alpha_)

ellipse(xpos,ypos,diameter,diameter);

if (diameter > 10){

diameter--;}

//if (alpha_<100){

//alpha_++;}

}

/* void collide(){

if (xpos == xpos - diameter*0.5){

xdirection *=-1;

}

if (ypos == ypos - diameter*0.5){

ydirection *=-1;

}

}

*/

void move(){

if (mousePressed == true){

xpos = xpos +(xspeed/5 * xdirection);

ypos = ypos +(yspeed/5 * ydirection);

}

else{

xpos = xpos +(xspeed * xdirection);

ypos = ypos +(yspeed * ydirection);}

if (xpos > width-diameter2*0.5 || xpos < diameter2*0.5){

xdirection *=-1;

diameter = diameter2;

//alpha_= 20;

}

if (ypos > height-diameter2*0.5 || ypos < diameter2*0.5){

ydirection *=-1;

diameter = diameter2;

//alpha_ = 20;

}

}

}