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 & HelpOpenGL and 3D Libraries › collisions with collections and glmodel
Page Index Toggle Pages: 1
collisions with collections and glmodel (Read 824 times)
collisions with collections and glmodel
Apr 24th, 2010, 11:09am
 
Hello i been working on a new version of my colliding points program using collections, but  this time im using the GLModel object from glgraphics library, GLModel allows to calculate points on the gpu, so its much faster and i can have much more points  than  when using just point( ) commands from processing .

GLModel works in a diferent way , if you want to draw 2 points you need use an array with all the variables of all the points like this "coords[ x, y , z,  w  , x, y , z,  w] " for example:

coords[ 30 , 50, 0, 0, 220, 10, 0, 0 ]  ---> this would draw 2 points at coordinates 30, 50  and 220, 10

When i run my program  not the right points are changing the color when they collide, I think the problem im having in my program is because when using  iterators in collections  there's no guarantee that they'll return objects in the same order, and for drawing my moving points and colors with glmodel i need to use an external iterator  like this:

 int iterate = 0;  -> out side the loop
iterate++ --> inside the loop

for example :

 int iterate = 0;
for (Iterator i=hashGrid.iterator(); i.hasNext()Wink
 {
   Dot o = (Dot)i.next();
 
   coords[4 * iterate ] = coords[4 * iterate ]  +  ((random(20) - 10) * 0.05 ) ;   // this is x
   coords[4 * iterate  + 1  ] = coords[4 * iterate + 1]  +  ((random(20) - 10) * 0.05 ) ;   //this is y

  iterate++
}


Do anybody have an idea is my presuptions are ok? and it this is the problem how can i solve this?
I would like to point out that i made the same code that calculate collisions with collections  without glmodel and it worked fine but for that and didnt need to use a self made iterator


THANKS!!!

there is the code:

Code:
import org.gicentre.processing.utils.*; 
import processing.opengl.*;
import codeanticode.glgraphics.*;
import javax.media.opengl.GL;

GLCamera cam;
GLModel model;



float[] coords;
float[] colors;
float color_cambia;
int numPoints = 2224;
float distance = 26600;

//solo puedo tener pocos agentes
HashGrid hashGrid;
static final float RADIUS = 30; // Search radius for hash grid

void setup()
{
size(700,700, GLConstants.GLGRAPHICS);
cam = new GLCamera(this, 0, 0, distance, 400, 400, 0);
cam = new GLCamera(this, 0, 0, distance, 0, 0, 0);
model = new GLModel(this, numPoints, POINTS, GLModel.DYNAMIC);
model.initColors();

hashGrid = new HashGrid(width,height,RADIUS);

coords = new float[4 * numPoints];
colors = new float[4 * numPoints];

for (int i=0; i< numPoints; i++)
{
hashGrid.add(new Dot(random(width),random(height) , 250, 0 ));
coords[4 * i ] = random(width) - width/2 ;// le resto 350 que es la mitad;
coords[4 * i + 1] = random(height) - height/2;
colors[4 * i + 3] = 0.9;
}

model.updateVertices(coords);
model.updateColors(colors);
}

void draw()
{
GLGraphics renderer = (GLGraphics)g;
GL gl = renderer.beginGL();

int iterate = 0;

for (Iterator i=hashGrid.iterator(); i.hasNext();)
{
Dot o = (Dot)i.next();
for (int k = 0; k < 3; k++) {
colors[4 * iterate + k ] = o.getColor() ;
}

coords[4 * iterate ] = coords[4 * iterate ] + ((random(20) - 10) * 0.05 ) ; // this is x
coords[4 * iterate + 1 ] = coords[4 * iterate + 1] + ((random(20) - 10) * 0.05 ) ; // this is y

//here i send the x , y values to mueve function, i sum it with width/2 and height/2 to fix the coordinates
o.mueve(coords[4 * iterate ]+ width/2, coords[4 * iterate + 1] + height/2);

iterate = iterate + 1;
Collection neighbours = hashGrid.get(o.getLocation());
for (Iterator j= neighbours.iterator(); j.hasNext();)
{
// See if the neighbouring balls are about to bounce off each other.
Dot otherBall = (Dot)j.next();
o.collision(otherBall);
}
}
hashGrid.updateAll(); // se requiere esto cuando hay movimiento para refrescar los objetos en el hashgrid
model.updateVertices(coords);
model.updateColors(colors);
cam.feed();
cam.clear(150, 50 , 220);
model.setPointSize(5);
model.render();
cam.done();
renderer.endGL();
}

class Dot implements Locatable
{
private PVector d;
private float point_color;
float dista;
boolean prendido;

Dot(float x, float y , float r, float g )
{
d = new PVector(x,y); // parece que vector siempre recibe las variables x , y , z
point_color = 0;
prendido = false;

}

void mueve(float xx, float yy){
d.x = xx;
d.y = yy;
}

void collision(Dot otherPoint ) {
if (otherPoint != this){
dista = dist(otherPoint.d.x , otherPoint.d.y , d.x , d.y);
if (( dista < 20 ) ){
point_color = 0.9; // turn color white
}
else {
point_color = 0 ; // turn color black
}
}
}

float getColor()
{
return point_color;
}

PVector getLocation()
{
return d;
}
}




Page Index Toggle Pages: 1