We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Collision Id. (Read 1085 times)
Collision Id.
Aug 29th, 2008, 6:43pm
 
Hi all,
I’m trying to print which ball is touched. I was searching the info inside “void collide” but I don’t get it. I just need the number of the ball that is in contact with the ball[0] controled by the mouse.
Thanks for the help
David  

/**
* Bouncy Bubbles.
* Based on code from Keith Peters (www.bit-101.com).
*
* Multiple-object collision.
*/

int numBalls = 8;
float spring = 0.5;
float gravity = 0.03;
Ball[] balls = new Ball[numBalls];

//____________________________________________________________________
void setup()
{
 size(200, 200);
 noStroke();
 smooth();
 for (int i = 1; i < numBalls; i++) {

   balls[i] = new Ball(random(width), random(height), 20, i, balls);
 }
 balls[0] = new Ball(mouseX,mouseY, 30, 0, balls);
}

//____________________________________________________________________
void draw()
{
 background(0);
 balls[0].x=mouseX;
 balls[0].y=mouseY;
 balls[0].collide();
 balls[0].display();
 for (int i = 1; i < numBalls; i++) {
   balls[i].collide();
   balls[i].move();
   balls[i].display();  
 }

}

//____________________________________________________________________
class Ball {
 float x, y;
 float diameter;
 float vx = 0;
 float vy = 0;
 int id;
 Ball[] others;

 Ball(float xin, float yin, float din, int idin, Ball[] oin) {
   x = xin;
   y = yin;
   diameter = din;
   id = idin;
   others = oin;
 }

 void collide() {
   for (int i = id ; i < numBalls; i++) {
     float dx = others[i].x - x;
     float dy = others[i].y - y;
     float distance = sqrt(dx*dx + dy*dy);
     float minDist = others[i].diameter/2 + diameter/2;
     if (distance < minDist) {
       float angle = atan2(dy, dx);
       float targetX = x + cos(angle) * minDist;
       float targetY = y + sin(angle) * minDist;
       float ax = (targetX - others[i].x) * spring;
       float ay = (targetY - others[i].y) * spring;
       vx -= ax;
       vy -= ay;
       others[i].vx += ax;
       others[i].vy += ay;
     }
   }  
 }

 void move() {
   vy += gravity;
   x += vx;
   y += vy;
   if (x + diameter/2 > width) {
     x = width - diameter/2;
     vx += -0.9;
   }
   else if (x - diameter/2 < 0) {
     x = diameter/2;
     vx *= -0.9;
   }
   if (y + diameter/2 > height) {
     y = height - diameter/2;
     vy *= -0.9;
   }
   else if (y - diameter/2 < 0) {
     y = diameter/2;
     vy *= -0.9;
   }
 }

 void display() {
   fill(255, 204);
   ellipse(x, y, diameter, diameter);
 }
}


Re: Collision Id.
Reply #1 - Aug 29th, 2008, 10:00pm
 
Immediately under the line

if (distance < minDist) {

just insert

if (id==0) println("Ball 0 collided with ball "+i);


There are also a few other things you could do to make the code more efficient (and therefore faster):

You never need to compare a ball with itself, so the first line of the collide() method would be better as

for (int i = id+1 ; i < numBalls; i++) {

Also, since the array of balls is, in effect, global, there is no need to have the array (oin) passed as an argument to the constructor.

Sqrt calculations can be a little costly to compute, so dealing with the squared distance between balls will be a bit faster.

See also this thread on efficient collision detection
Re: Collision Id.
Reply #2 - Aug 30th, 2008, 12:52am
 
maybe this post answeres some questions too, had similar problems/questions...

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1218853051
Re: Collision Id.
Reply #3 - Aug 30th, 2008, 12:53am
 
maybe i should read first, Jo postet the same link Smiley
Re: Collision Id.
Reply #4 - Aug 31st, 2008, 10:54pm
 
Hey,
Nice answers. I’ve learned a lot from this. I have more questions but i prefer to work on it a little bit more to find the answer. But the identification of the particle and the substitution of the “sqrt” its already done.
Thanks a lot
David
Page Index Toggle Pages: 1