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 › colliding agents without poo
Page Index Toggle Pages: 1
colliding agents without poo (Read 467 times)
colliding agents without poo
Jul 5th, 2009, 1:20pm
 
Hello im trying to implement some agents that move randomly and when they collide they change their color. I need to implement this without using an objet oriented programming approach. Ive made this code  but just one agent change its color, Do anybody have an idea of what am i doing wrong?

thanks
Code:



float[] x;
float[] y;

float[] distance_points;

int numPoints = 20 ;

float colorr[];

void setup()
{
size(600, 600);
x = new float[numPoints];
y = new float[numPoints];
distance_points = new float[numPoints];
colorr = new float[numPoints];

for (int i = 0; i < numPoints; i++)
{
x[i] = random(242);
y[i] = random(282);
distance_points[i] = 0;
colorr[i] = 0;
}
}




void draw()
{
background(250, 250 , 250 );

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

x[i] = x[i] + random(2) - 1;
y[i] = y[i] + random(2) - 1;

fill(colorr[i] );
ellipse(x[i], y[i], 20, 20);


for (int k = 0; k < numPoints; k++){
if ( i != k) {
distance_points[k] = dist(x[k] , y[k] , x[i] , y[i]);
}
if (distance_points[i] < 10) {
colorr[i] = 0;
}
else {
colorr[i] = 250;
}
}
}
}



Re: colliding agents without poo
Reply #1 - Jul 5th, 2009, 4:10pm
 
The problem is that whilst an agent in proximity to another agent may have its colour changed by the condition, because it is not in proximity to another agent the condition in that loop changes it back to white.

To put it another way:

Agent A is near agent B so agent A and B are set to black.
However, neither A or B are near to agent C and since this is the last agent in the loop it changes the colours of A and B back to white.

There's a simple way around this problem.  Set the value of a boolean to false outside the inner 'k' loop which checks distance.  If the agent is near to another agent set the boolean to true otherwise do nothing. So just to labour the point: if it is near to ANY other agent the boolean will be true, otherwise it will be false.  Then simply test against the boolean to determine the colour:

Code:
float[] x;
float[] y;

float[] distance_points;

int numPoints = 10;

float colorr[];

void setup()
{
 size(300, 300);
 x = new float[numPoints];
 y = new float[numPoints];
 distance_points = new float[numPoints];
 colorr = new float[numPoints];

 for (int i = 0; i < numPoints; i++) {
   x[i] = random(242);
   y[i] = random(282);
   distance_points[i] = 0;
   colorr[i] = 0;
 }
}


void draw() {
 background(250);

 for (int i = 0; i < numPoints; i++){
 
   x[i] = x[i] + random(2) - 1;
   y[i] =  y[i] + random(2) - 1;
 
   fill(colorr[i] );
   ellipse(x[i], y[i], 20, 20);
 
   boolean near = false;
   
   for (int k = 0; k < numPoints; k++){
if ( i != k) {
 distance_points[k]  = dist(x[k] , y[k] , x[i] , y[i]);
}
if (distance_points[k] < 40) {
near = true;
}
   }
   
   if(near){
colorr[i] = 0;  
   }
   else {
colorr[i] = 250;
   }
 }
}


Note that you had also checked against distance_points[i] and not distance_points[k].  It's late here so I'm not going to try and explain that now...
Page Index Toggle Pages: 1