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 › calculating collisions with collections
Page Index Toggle Pages: 1
calculating collisions with collections (Read 1160 times)
calculating collisions with collections
Apr 23rd, 2010, 12:09pm
 
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;
}
}





Re: calculating collisions with collections
Reply #1 - Apr 23rd, 2010, 1:00pm
 
I don't know what this HashGrid is, nor what it is supposed to do. (I can guess, but guesses doesn't make good programming!)
Re: calculating collisions with collections
Reply #2 - Apr 23rd, 2010, 1:17pm
 
I don't know what your problem is, but I'd like to point out (just in case) that for Iterators, there's no guarantee that they'll return objects in the same order, so "if ( count_i++ !=  count_j++){" could not be doing what you'd hope. You should be able to just compare d_1==d_2.

After another read, I think that you're doing your checking in the wrong place.. each and every Dot is checking every dot against every other dot.. so dot 1 is checking dot[1,2,3,4...] against dot[1,2,3,4..] and then when moving dot 2 it is comparing dot[1,2,3,4...] against dot[1,2,3,4...] etc...

Each dot should only check itself against every other dot. So you only need to iterate once, and then check:"if(nextIter = this)" (but with iter.next put into a variable etc...)
Re: calculating collisions with collections
Reply #3 - Apr 23rd, 2010, 1:34pm
 
hi philo hashgrid is supposed to use when you want a  lot of interacting objects and you dont want to calculate the collision of each one with all the rest , and just the between the ones that are near you.

there is an easy explanation

http://www.soi.city.ac.uk/~jwo/processing/hashGrid/index.html
Re: calculating collisions with collections
Reply #4 - Apr 23rd, 2010, 2:15pm
 
So I was right in my guessing... But if you just have an issue with a library without indicating which one or where to find it, we have difficulty to help you.
Beside, your usage of the HashGrid is inefficient, you are supposed to check distance / collision only against the nearest objects, not against the whole HashGrid content...
Re: calculating collisions with collections
Reply #5 - Apr 24th, 2010, 9:16am
 
thanx i fixed and worked perfect
Page Index Toggle Pages: 1