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 & HelpPrograms › agents colliding problem
Page Index Toggle Pages: 1
agents colliding problem (Read 383 times)
agents colliding problem
Dec 25th, 2008, 1:46pm
 
hello im having some problems with a program, ive made an array full of agents that move randomly in the screen, i want that when each agente collide with other agent it can change its color . Ive made this program but im getting wierd behaivors , agents dont change the color when its expected . do anybody know why is that?



int numAgente= 12;

Agente[] arreglo =new Agente[numAgente];

void setup(){
size(800,700);
// noStroke();
background(238, 282, 299);
for (int i=0; i<numAgente; i++){

arreglo[i] = new Agente();

}}

void draw(){

for (int k=0; k<numAgente; k++){
 arreglo[k].dibuja();
}
}

class Agente{
float x= random(800);
float y= random (700 );
float dista;
 color coloryo ;
 
void dibuja(){
x = x + random(20) - 10;
y = y + random(20) - 10;

fill(coloryo);
ellipse(x , y ,20,20);
for (int i=0; i<numAgente; i++){

for (int j= 0 ; j <numAgente; j++){
 if ( i !=  j){
dista = sqrt (((arreglo[i].x - arreglo[j].x) * (arreglo[i].x - arreglo[j].x) )+  ((arreglo[i].y - arreglo[j].y) * (arreglo[i].y - arreglo[j].y)));
 if (dista < 26){
 //  println(arreglo[i].x);
 //  arreglo[j].coloryo =  color(222, 40, 0);
   arreglo[i].coloryo =  color(222, 40, 0);

}
else
{
//arreglo[j].coloryo =  color(2, 40, 0);
arreglo[i].coloryo =  color(2, 40, 0);
}
}
}
}
}
}
Re: agents colliding problem
Reply #1 - Dec 25th, 2008, 3:16pm
 
the braces are weird too Smiley.
i think maybe because the loop checking for every agent if he hits another is done so many times equal to the number of agents. numAgente * dibuja  in draw.
also you can write the loops like this
for (int i=0; i<numAgente; i++){

for (int j= i+1 ; j <numAgente; j++){
...
arreglo[i].coloryo =  color(222, 40, 0);
arreglo[j].coloryo =  color(222, 40, 0);

so every hit is notices once not twice.
cheery
ramin
Re: agents colliding problem
Reply #2 - Dec 25th, 2008, 11:32pm
 
I saw something like that in the past. You might be interested by this thread too: No idea what could be the problem!!
Page Index Toggle Pages: 1