Hello , i been trying to calculate collisions between moving points but using collections instead of arrays and "for" for iterating.
Ive made this code, but i cannot figure out what am i doing wrong, the problem is that the points dont change the color when they are too near as its expected .
I made the same code without collections and it works ok.
Do anybody have an idea of what is going on?
i also cant understand why im getting 0's when im print the distance.
i solved this using this code if (( dista < 200 ) && (dista != 0.0 ) ) , but i cannot understand why im getting 0.0's . Any idea?
thank you!
M.
Code:
import org.gicentre.processing.utils.*;
HashGrid hashGrid;
static final float RADIUS = 50; // Search radius for hash grid
void setup()
{
size(400,400);
smooth();
noFill();
hashGrid = new HashGrid(width,height,RADIUS);
for (int i=0; i< 23; i++)
{
hashGrid.add(new Dot(random(width),random(height) , 250, 0 ));
}
}
void draw()
{
background(255);
strokeWeight(2);
for (Iterator i=hashGrid.iterator(); i.hasNext();)
{
Dot o = (Dot)i.next();
o.mueve();
stroke(o.getColor() );
point(o.getLocation().x, o.getLocation().y);
}
hashGrid.updateAll(); // se requiere esto cuando hay movimiento para refrescar los objetos en el hashgrid
}
class Dot implements Locatable
{
private PVector d;
private color l;
float dista;
Dot(float x, float y , float r, float g )
{
d = new PVector(x,y); // parece que vector siempre recibe las variables x , y , z
l = color(250, 0, 0);
}
void mueve(){
d.x = d.x + (random(20) - 10) * 0.1 ;
d.y = d.y + (random(20) - 10) * 0.1 ;
l = color(250, 0, 0);
//colision
int count_i = 0;
for (Iterator i=hashGrid.iterator(); i.hasNext();)
{
count_i++;
Dot d_1 = (Dot)i.next();
//point(d.getLocation().x,d.getLocation().y);
int count_j = count_i + 1;
for (Iterator j=hashGrid.iterator(); j.hasNext();)
{
count_j++ ;
Dot d_2 = (Dot)j.next();
if ( count_i++ != count_j++){
dista = dist(d_1.getLocation().x , d_1.getLocation().y, d_2.getLocation().x , d_2.getLocation().y);
// dista = dist(d.x , d.y, d_2.getLocation().x , d_2.getLocation().y);
//dista = dist(d_1.getLocation().x , d_1.getLocation().y, d.x , d.y);
//print(" " + dista);
if (( dista < 200 ) && (dista != 0.0 ) ){
d_1.l = color(0, 250, 0);
d_2.l = color(0, 250, 0);
}
else {
l = color(250, 0, 0);
}
}
}
}
}
color getColor()
{
return l;
}
PVector getLocation()
{
return d;
}
}