these code works with the hashgrid class:
Code:Vector agents;
HashGrid hashGrid;
boolean useNSquaredSearch;
static final int NUM_ANTS = 156; //con esto se puede optimizar?
//**********<agentes>************
import processing.opengl.*;
import codeanticode.glgraphics.*;
import javax.media.opengl.GL;
GLCamera cam;
GLModel model;
float[] coords;
float[] colors;
float[] x;
float[] y;
float[] delta_x;
float[] delta_y;
float velocidad = 3;
float direccion = random(TWO_PI);
int numPoints = 4000;
float distance = 4200;
//***********</agentes>***************
void setup()
{
size(600,600, GLConstants.GLGRAPHICS);
cam = new GLCamera(this, 0, 0, distance, 0, 0, 0);
model = new GLModel(this, numPoints, POINTS, GLModel.DYNAMIC);
model.initColors();
agents = new Vector();
hashGrid = new HashGrid(width,height,round(width/Agent_values.SENSOR_DIST),round(height/Agent_values.SENSOR_DIST));
// for (int i=0; i<NUM_ANTS; i++)
// {
//ants.add(new Ant(random(0,width-1),random(0,width-1)));
// }
//*****************<agentes>
x = new float[numPoints];
y = new float[numPoints];
delta_x = new float[numPoints];
delta_y = new float[numPoints];
coords = new float[4 * numPoints];
colors = new float[4 * numPoints];
for (int i = 0; i < numPoints; i++)
{
//*************<agente>
float random_x = random(642) ; // esto lo hago para que el objeto comparta el mismo valor x que mi punto de glmodel
float random_y = random(482) ;
agents.add(new Agent_values(random_x, random_y));
//***************</agente>
x[i] = random_x;
y[i] = random_y;
colors[4 * i + 3] = 0.9;
}
model.updateVertices(coords);
model.updateColors(colors);
//******************</agentes>********
}
void draw()
{
//********************<glgraphics>*************************
GLGraphics renderer = (GLGraphics)g;
GL gl = renderer.beginGL();
// renderer.beginGL();
for (int i = 0; i < numPoints; i++){
x[i] = x[i] + random(-0.5,0.5)*3 + delta_x[i]/4 /4 ;
y[i] = y[i] + random(-0.5,0.5)*3 + delta_y[i]/4 / 4 ;
float x_para_gl_y_objeto = x[i]/6.5 - 48 ;
float y_para_gl_y_objeto = y[i]/6.5 * -1 + 38;
Agent_values agent = (Agent_values)agents.get(i);
agent.x = x[i];
agent.y = y[i];
//aca debo de pasarle x_para_gl_y_objeto y y_para_gl_y_objeto a cada objeto
coords[4 * i ] = x_para_gl_y_objeto;
coords[4 * i + 1 ] = y_para_gl_y_objeto ;
}
//******************************</glgraphics>************************
hashGrid.clear();
for (int i=0; i<agents.size(); i++)
{
Agent_values agent = (Agent_values)agents.get(i);
hashGrid.add(agent,agent.x,agent.y);
// Compare this ant with any others in the same grid cell
Vector antList = (Vector)hashGrid.get(agent.x, agent.y);
if (antList != null)
{
for (int j=0; j<antList.size(); j++)
{
Agent_values otherAnt = (Agent_values)antList.get(j);
if (agent != otherAnt)
{
if (agent.distToAnt(otherAnt) < Agent_values.SENSOR_DIST)
{
// agent.setAngry(true);
// otherAnt.setAngry(true);
//aca debo poner el punto gl model que cambiara de color?
for (int k = 0; k < 3; k++) {
colors[4 * i + k ]= 0.9 ;
}
}
else
{
for (int k = 0; k < 3; k++){
colors[4 * i + k ]= 0.0;
}
}
}
}
}
// Get each ant to draw itself.
// ant.display();
}
model.updateVertices(coords);
model.updateColors(colors);
//model.updateVertices(colors);
cam.feed();
cam.clear(255, 25 , 25);
gl.glPointSize(5);
model.render();
cam.done();
renderer.endGL();
println(frameRate);
}
class Agent_values
{
// float x,y; // Ant location.
float direction; // Direction of travel.
float speed; // Speed of ant.
boolean isAngry; // Indicates whether ant is angry or not.
float size; // Size of ant.
static final float SENSOR_DIST=2; // Distance within which ant can sense other beings.
//*************<agente>
float coords;
float colors;
float x;
float y;
float delta_x;
float delta_y;
float velocidad = 3;
float direccion = random(TWO_PI);
//*************</agente>************
// Creates a new ant.
Agent_values(float x, float y)
{
this.x = x;
this.y = y;
size = 10;
direction = random(-PI,PI);
speed = 1;
isAngry = false;
}
// Draws the ant at its current location.
void display()
{
if (isAngry)
{
//fill(150,50,50);
isAngry = false;
}
else
{
// fill(50);
}
}
// Determines whether or not ant is angry.
void setAngry(boolean isAngry)
{
this.isAngry = isAngry;
}
// Measures the distance between this and the given ant.
float distToAnt(Agent_values otherAnt)
{
return dist(x,y,otherAnt.x,otherAnt.y);
//return abs(x - otherAnt.x) + abs(y - otherAnt.y) ;
}
}