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 › colliding with 2 agents
Page Index Toggle Pages: 1
colliding with 2 agents (Read 689 times)
colliding with 2 agents
Dec 27th, 2008, 12:33am
 

hello, in the following program each agent moves randomly in the screen until it collide with other agent, when that happens it change its color. For this i made a simple function that calculates the distance between all the agents in an array. When the distance is less than some number then "something happens".it works fine but  my question is how about if i need that something happens just when an agent collide with 2 agents?

I need that each agent  moves randomly  until it collides with 2 other agents at the same time, so i need that each agent collide with 2 other agents to trigger a function. How can i make that each agent can be aware of how many agents are colliding with them?





int numAgente= 12;
Agente[] arreglo =new Agente[numAgente];

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

arreglo[i] = new Agente();
}
}


void draw(){

background(238, 282, 299);
 
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(24, 88,99, 34);
//ellipse(x, y , 180, 180);
fill(coloryo);
ellipse(x , y ,180,180);

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

for (int j= i+1 ; 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 < 180){

   arreglo[i].coloryo =  color(222, 40, 0);
   arreglo[j].coloryo =  color(222, 40, 0);

}
else
{
coloryo =  color(2, 40, 0);

}
}
}
}

}
}
Re: colliding with 2 agents
Reply #1 - Dec 29th, 2008, 10:02pm
 
use something like a counter.

Quote:
for (int i=0; i<numAgente; i++){
 int collisionCount = 0;
 for (int j = 0 ; j < numAgente; j++){
   if ( i !=  j) {
     dista = sqrt (((arreglo.x - arreglo[j].x) * (arreglo[i].x - arreglo[j].x) )+  ((arreglo[i].y - arreglo[j].y) * (arreglo[i].y - arreglo[j].y)));
     if (dista < 180) { collisionCount++; }
   }
 }
 if (collisionCount >= 2) { arreglo[i].coloryo =  color(222, 40, 0); }
   else { arreglo[i].coloryo =  color(2, 40, 0); }
}


by the way I think "[i]for (int j= i+1...
" is not what you want. your j iteration should start at 0 so you can make the calculation with each other agent.
Re: colliding with 2 agents
Reply #2 - Dec 29th, 2008, 11:54pm
 
antiplastik wrote on Dec 29th, 2008, 10:02pm:
by the way I think "for (int j= i+1..." is not what you want. your j iteration should start at 0 so you can make the calculation with each other agent.

Well, I think it is OK (but the i != j test is redundant), it is a way to avoid testing an agent against itself, and to avoid doing tests twice: if an agent collides another, no need to do the reverse test.
But the trick doesn't scale well for multiple tests... Smiley
Re: colliding with 2 agents
Reply #3 - Dec 31st, 2008, 6:56am
 
hello philho, what does you mean when you say: "the trick doesnt scale well for multiple tests"
Re: colliding with 2 agents
Reply #4 - Dec 31st, 2008, 1:17pm
 
That's the reason why I haven't answered initially: the optimization is binary, checking pairs of objects. Maybe you can extend it to three objects, but not to an arbitrary number.
Funnily, I just answered collision detection help which has a similar concern. Maybe it can help.

BTW: you can use dist() function. It does the same thing as the sqrt but it is slightly more readable... Smiley
colliding with 3 agents
Reply #5 - Jan 4th, 2009, 3:26pm
 
but how can i extend to 3 or more agents, the folowing example just works with 2 colliding agents, even if i change to   if(collisionCount >= 3) or if(collisionCount >= 4 ) it doenst work, any idea of why is this and how can i make it scalable? other thing : the  int j= i + 1  example doesnt work for 2 colliding agents neither , it seems it just works for some of the agents but not for all, any idea of this?


int numAgente= 42;

Agente[] arreglo =new Agente[numAgente];

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

arreglo[i] = new Agente();

}
}

void draw(){
background(238, 282, 299);
for (int k=0; k<numAgente; k++){
arreglo[k].dibuja();
arreglo[k].mueve();
}
}

class Agente{
float x= random(800);
float y= random (700 );
float dista;
float moviemiento =  random(20) - 10 ;
float moviemientodos =  random(20) - 10 ;
color coloryo ;
 
void mueve(){
// x = x + movimiento;
x = x + random(20) - 10;
y = y +  random(20) - 10 ;
    }
 
void dibuja(){

fill(24, 88,99, 54);
ellipse(x, y , 60, 60);
fill(coloryo);
ellipse(x , y ,15,15);
for (int i=0; i<numAgente; i++){
  int collisionCount = 0;

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 < 60){
   collisionCount++;
}
 }}
 if(collisionCount >= 2) {
   arreglo[i].coloryo = color(222, 40, 0);

}
 else
{
coloryo =  color(2, 40, 0);

}
}}}
Re: colliding with 2 agents
Reply #6 - Jan 4th, 2009, 11:37pm
 
Here is what I got while trying to keep your base code:
Code:
int numAgente= 42;

Agente[] arreglo = new Agente[numAgente];

void setup(){
 size(800,700);
 smooth();
 noStroke();
 background(238, 282, 299);
 for (int i=0; i<numAgente; i++){
   arreglo[i] = new Agente(i);
 }
}

void draw(){
 background(238, 282, 299);
 for (int k=0; k<numAgente; k++){
   arreglo[k].mueve();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].check();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].dibuja();
 }
}

class Agente{
float x = random(800);
float y = random(700);
float moviemiento = random(20) - 10;
float moviemientodos = random(20) - 10;
color coloryo;
int rank;

Agente(int i) { rank = i; }
 
void mueve(){
 // x = x + movimiento;
 x = x + random(20) - 10;
 y = y +  random(20) - 10 ;
}

void check(){
 int collisionCount = 0;  
 for (int i=0; i<numAgente; i++){
   if ( i != rank){
     float dista = dist(arreglo[i].x, arreglo[i].y, x, y);
     if (dista < 60){
       collisionCount++;  
     }
   }
 }
 if (collisionCount >= 2) {
   coloryo = color(222, 40, 0);
 } else if (collisionCount == 1) {
   coloryo = color(222, 240, 0);
 } else {
   coloryo = color(2, 40, 0);
 }
}
 
void dibuja(){
 fill(24, 88,99, 54);
 ellipse(x, y , 60, 60);
 fill(coloryo);
 ellipse(x , y ,15,15);
}
}
Re: colliding with 2 agents
Reply #7 - Jan 6th, 2009, 4:33am
 
you code is beautiful, thanks mate
Re: colliding with 2 agents
Reply #8 - Jan 11th, 2009, 1:41pm
 
PhiLho one question, is it posible to adapt you code in a way each agent can percieve the state or a variable  of the 2 colliding agents?  For example if each agent have a new variable like this :

float stateA =  random(10) ;

how can an agent percieve the value of each each that collides with it? I ask you because in your code you just have one iteration and before i made a code that can percieve neighbours variables but it uses nested iteration like this:
 for(int i=0;i <bichos.length;i++){
 for(int j=0;j<bichos.length;j++){
 float dx=bichos[i].x-bichos[j].x;
 float dy=bichos[i].y-bichos[j].y;
    if((i!=j)&&(sqrt(dx*dx+dy*dy)<rb)){
        bichos[i].colorea(bichos[j].rr);

 }

 }

 }

Any idea?
Re: colliding with 2 agents
Reply #9 - Jan 11th, 2009, 10:34pm
 
You might have missed it, but you have actually still two nested loops: a loop on draw() iterates on the agents, and in the called check() routine, a loop iterates on the others agents.
You can use the state of the other agent here.
Re: colliding with 2 agents
Reply #10 - Jan 12th, 2009, 7:23am
 
hello, philho. I can make that each agent perceives the state of each agent that collides it, that works fine.  But what i need is that each agent can percieve the state of the "2" agents (just when it collides with 2) that collides it and add them.  Perceiving the state of one agent that collides is not problem , but how can i make something like this: each agent moves randomly when 2 agents collide one agent , the agent can percieve the state of both agents and then add them . if the sum of both agents that are colliding me is more than "whatever" something happens.
Any idea?


int numAgente= 12;

Agente[] arreglo = new Agente[numAgente];

void setup(){
 size(800,700);
 smooth();
 noStroke();
 // print( random (5));
 background(238, 282, 299);
 for (int i=0; i<numAgente; i++){
   arreglo[i] = new Agente(i);
 }
}

void draw(){

 background(38, 82, 29);
 for (int k=0; k<numAgente; k++){
   arreglo[k].mueve();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].check();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].dibuja();
 }
}

 
class Agente{
float x = random(800);
float y = random(700);

float stateA = random (5);
float stateB = random (5);

color coloryo;
int rank;

Agente(int i) { rank = i; }
   
void mueve(){
 // x = x + movimiento;
 x = x + random(20) - 10;
 y = y +  random(20) - 10 ;
}



void check(){
 int collisionCount = 0;  
 for (int i=0; i<numAgente; i++){
   if ( i != rank){
     float dista = dist(arreglo[i].x, arreglo[i].y, x, y);
     if (dista < 60){
       collisionCount++;  
   //    print(arreglo[i].stateA + "--");
 //here it checks if the  state of each agent that collides is more than 3
 //if so, then it adds 1 to my state,
// but i need to check the state of the colliding agent just when i collide with 2 at the same time  and then sum both
// if the sum of both agents that are colliding me is more than 4 , then something happens, how can i do this?
   if (arreglo[i].stateA > 3) {
     
     stateA = stateA + 1;
   }
     }
   }
 }
 if (collisionCount >= 2) {
   coloryo = color(222, 40, 0);
   
   
   
 } else if (collisionCount == 1) {
   coloryo = color(222, 240, 0);
 } else {
   coloryo = color(2, 40, 0);
 }
}
   
void dibuja(){
 fill(24, 88,99, 54);
 ellipse(x, y , 60, 60);
 fill(coloryo);
 ellipse(x , y ,15,15);
}
}
Re: colliding with 2 agents
Reply #11 - Jan 12th, 2009, 12:28pm
 
The problem is that you have the final value only after all checks have been done.
I would add yet another loop:
Code:
void draw(){

 background(38, 82, 29);
 for (int k=0; k<numAgente; k++){
   arreglo[k].mueve();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].check();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].applyState();
 }
 for (int k=0; k<numAgente; k++){
   arreglo[k].dibuja();
 }
}

The applyState() method looks at the computed state (resulting from all collisions) and do whatever is needed on the Agente, before drawing its final state.
Page Index Toggle Pages: 1