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 › what am i doing wrong
Page Index Toggle Pages: 1
what am i doing wrong? (Read 710 times)
what am i doing wrong?
Jun 30th, 2009, 3:10am
 
hi list ,   im  calculating the distance between agents that moves randomly  and i want that when my agents collide they change the color.  Ive been trying the following code but i dont know what am i doing wrong, any idea?

thanks

Code:


ball[] bolas = new ball[20];


void setup(){
size(400,400);
for(int i = 0; i<20; i++){
bolas[i] = new ball(i);
}
}


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

for(int i = 0; i<20; i++){
bolas[i].dibuja();
bolas[i].mueve();
bolas[i].collision();
}
}


class ball {
float x = random(200);
float y = random(300);
float colorr = 70;
int rank;

float distancia;

ball(int i ) {
rank = i;
}

void dibuja(){
fill(colorr);
ellipse(x, y, 20, 20);
}

void mueve() {
x = x + random(2) - 1;
y = y + random(2) - 1;
}

void collision() {
for(int i = 0; i < 20 ; i++){
if (i != rank){
distancia = dist( x , y, bolas[i].x , bolas[i].y );
// print(" " + distancia);
if (distancia < 20 ) {
bolas[i].colorr = 0;
}
else {
bolas[i].colorr = 250;
}
}
}
}
}


Re: what am i doing wrong?
Reply #1 - Jun 30th, 2009, 3:42am
 
collision() method should change only the color of the current ball, not the color of the other balls: that's better encapsulation, and it would avoid changing the color multiple times per ball...
Re: what am i doing wrong?
Reply #2 - Jun 30th, 2009, 3:48am
 
The problem is in the collision detection. You test each ball against every other ball changing the color if there is a collision but also changing the color if no collision.

The ball will only stay black if the collision is with the last ball in the array.

The code below works. Smiley

Code:
class ball {
 float x = random(200);
 float y = random(300);
 float colorr = 70;
 int rank;
 boolean collided;
 
 float distancia;
 
 ball(int i ) {
   rank = i;
 }
 
 void dibuja(){
   if(collided)
   fill(0);
   else
   fill(250);
   ellipse(x, y, 20, 20);
 }
 
 void mueve() {
   x = x + random(2) - 1;
   y = y + random(2) - 1;
 }
 
 void collision() {
   collided = false;
   for(int i = 0; i < 20 ; i++){
if (i != rank){
 distancia = dist( x , y, bolas[i].x , bolas[i].y );
 // print(" " + distancia);
 if (distancia < 20 ) {
   collided = true;
 }
}
   }
 }
}
Re: what am i doing wrong?
Reply #3 - Jul 4th, 2009, 3:16am
 
i got a new question, i was trying to optimize the code, but i cannot notice any difference in performance between this 2 codes, Is there any difference between these 2 versions? and is there any way to see how many cpu cycles consume each version? i cannot see any differece in speed

Code:


int numero_agentes = 600;

ball[] bolas = new ball[numero_agentes];

void setup(){
size(400,400);
for(int i = 0; i<numero_agentes; i++){
bolas[i] = new ball(i);
}
}


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

for(int i = 0; i<numero_agentes; i++){
bolas[i].collision(i);
bolas[i].dibuja();
bolas[i].mueve();

}
}


class ball {
float x = random(200);
float y = random(300);
float colorr = 70;
int rank;
float distancia;
boolean collided ;

ball(int i) {
rank = i;
}

void dibuja() {
if (collided)
{
colorr = 0;
collided = false;}
else
colorr = 250;
fill(colorr);
ellipse(x, y , 20, 20);


}



void mueve(){
x = x + random(2) - 1;
y = y + random(2) - 1;


}


void collision(int start){
//collided = false;
for(int i = start + 1; i < numero_agentes ; i++){

if(bolas[i] != this) {
distancia = dist(x, y , bolas[i].x, bolas[i].y) ;

if (distancia < 20 ) {
this.collided = true;
bolas[i].collided = true;

}

}


}

}
}






Code:

int numero_agentes = 600;


ball[] bolas = new ball[numero_agentes];

void setup(){
size(400,400);
for(int i = 0; i<numero_agentes; i++){
bolas[i] = new ball(i);
}
}


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

for(int i = 0; i<numero_agentes; i++){
bolas[i].collision(i);
bolas[i].dibuja();
bolas[i].mueve();

}
}


class ball {
float x = random(200);
float y = random(300);
float colorr = 70;
int rank;
float distancia;
boolean collided ;

ball(int i) {
rank = i;
}

void dibuja() {
if (collided)
{
colorr = 0;
collided = false;}
else
colorr = 250;
fill(colorr);
ellipse(x, y , 20, 20);


}



void mueve(){
x = x + random(2) - 1;
y = y + random(2) - 1;


}


void collision(int start){
//collided = false;
for(int i = 0; i < numero_agentes ; i++){
//i != rank
if(bolas[i] != this) {
distancia = dist(x, y , bolas[i].x, bolas[i].y) ;

if (distancia < 20 ) {
this.collided = true;


}

}


}

}
}

Re: what am i doing wrong?
Reply #4 - Jul 4th, 2009, 10:34am
 
Here is a way to measure the difference:
Code:
static final int numero_agentes = 600;
static final int BALL_SIZE = 20;

long totalTime;

ball[] bolas = new ball[numero_agentes];

void setup(){
 size(400,400);
 for(int i = 0; i<numero_agentes; i++){
   bolas[i] = new ball(i);
 }
}


void draw(){
 background(250, 250, 0);
 
 for(int i = 0; i<numero_agentes; i++){
   bolas[i].collision(i);
   bolas[i].dibuja();
   bolas[i].mueve();

 }
 if (frameCount > 1000)
 {
   println("Total: " + totalTime);
   exit();
 }
}


class ball {
 float x = random(200);
 float y = random(300);
 float colorr = 70;
 int rank;
 float distancia;
 boolean collided ;
 
 ball(int i)  {
 rank = i;
 }
 
 void dibuja() {
   if (collided)
   {
   colorr = 0;
   collided = false;}
   else
   colorr = 250;
   fill(colorr);
   ellipse(x, y , BALL_SIZE, BALL_SIZE);
 }
 
 void mueve(){
   x  = x + random(2) - 1;
   y = y + random(2) - 1;
 }
 
 void collision(int start){
   long t = millis();
   //collided = false;
   for(int i = start + 1; i < numero_agentes ; i++){
   
if(bolas[i] != this)  {
distancia = dist(x, y , bolas[i].x, bolas[i].y) ;

if (distancia < BALL_SIZE )  {
 collided = true;
   bolas[i].collided = true;
   }
 }
}
totalTime += millis() - t;
}
}
.
Second, naive code: 7141, 6711, 6983
First code: 3752, 3528, 3817
My code: 3518, 3697, 3370
The last optimization (using constants) isn't really significant...
Note I do (at least) three runs, because of system activity.
The visual difference isn't great, but with much more particles, it might be significant.

There is another well known optimization: avoid the square root calculation done in dist().
Code:
  void collision(int start){
   long t = millis();
   //collided = false;
   for(int i = start + 1; i < numero_agentes ; i++){
   
if(bolas[i] != this)  {
distancia = (x - bolas[i].x)*(x - bolas[i].x) + (y - bolas[i].y)*(y - bolas[i].y);

if (distancia < BALL_SIZE*BALL_SIZE )  {
 collided = true;
   bolas[i].collided = true;
   }
 }
}
totalTime += millis() - t;
}

Results: 1819, 1751, 2221
Re: what am i doing wrong?
Reply #5 - Jul 4th, 2009, 11:08am
 
thanks mate, this is really useful
Page Index Toggle Pages: 1