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.
IndexProgramming Questions & HelpSyntax Questions › balls colliding  problem
Page Index Toggle Pages: 1
balls colliding  problem (Read 335 times)
balls colliding  problem
Apr 21st, 2008, 9:35pm
 
hello people, my name is Matias, im new in this list, im also new with processing, ... im need to make a lot spheres that moves randomly in the screen and when they collide (or become very near between each other)they  change their color and if they separate they come back to their original color.
I was trying to adapt the bouncy bubbles  from the processing examples in the web site, but im getting unexpected and wierd behaivors ... do anybody know whats going on? any hint would be very appreciated. here is the code:

int numAgentes = 22;
Agente[] arreglo =new  Agente[numAgentes];


void setup(){
 size(500,500);

 for (int i=0; i<numAgentes; i++){
   
arreglo[i] = new Agente(random(width), random(height), i, arreglo);  

  }
}

void draw(){
 background(188, 282, 299);

   for (int i=0; i<numAgentes; i++){
   arreglo[i].collide();  
   arreglo[i].mueve();
   arreglo[i].display();
     }
}


class Agente{
float coloro = 0;
float x, y;
float diameter;
 float vx = 0;
 float vy = 0;
 int id;
 Agente[] others;

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

  void collide() {
 
   for (int i = id + 1; i < numAgentes; i++) {
     float dx = others[i].x - x;
     float dy = others[i].y - y;
     float distance = sqrt(dx*dx + dy*dy);
     
     float minDist = 60;
     
    if (distance < minDist) {
         coloro = 122;

     }
     
        else {
           coloro = 0;

   }
   }  
 }

  void mueve(){
    x = x + random(2) - 1;
    y = y + random(2) - 1;
   
    }
 
 void display(){
   fill(coloro);    
   ellipse(x , y  ,20,20);  
}
}
Re: balls colliding  problem
Reply #1 - Apr 22nd, 2008, 4:55pm
 
In arregelo.display(), you could add an if() statement that checks to see if the circle is within 20px of any other circles.  If yes, then draw the ellipse with one color, if not, then use another.
Page Index Toggle Pages: 1