Permalink
Browse files

Added NN grid init code for flocking

  • Loading branch information...
1 parent 13ebd53 commit e082e6e4ca026044853c74423a952bc050fc524a @muraliavarma committed Feb 1, 2013
@@ -54,7 +54,7 @@ class Creature {
void applyForces() {
//apply the 3 forces to the creature and update its x and y velocities
-
+
}
Creature[] getNeighbors(float radius) {
@@ -1,4 +1,24 @@
+final float FLOCK_CENTERING_RADIUS = 0.2;
+
+HashMap flockCenterGrid;
+
+void initNeighborGrids() {
+ flockCenterGrid = new HashMap();
+}
+
void computeNeighborGrids() {
//use active forces and compute separate neighbor grids for each force
-
+ float radius = FLOCK_CENTERING_RADIUS;
+ flockCenterGrid.clear();
+ for (int i = 0; i < NUM_CREATURES; i++) {
+ String key = int(creatures[i].posX/radius) + "," + int(creatures[i].posY/radius);
+ ArrayList val = (ArrayList)flockCenterGrid.get(key);
+ if (val != null) {
+ val.add(i);
+ }
+ else {
+ flockCenterGrid.put(key, new ArrayList());
+ }
+ }
+
}
Binary file not shown.
@@ -37,6 +37,7 @@ public void setup() {
background(0);
stroke(100);
+ initNeighborGrids();
initCreatures();
}
@@ -130,17 +131,37 @@ public void update() {
public void applyForces() {
//apply the 3 forces to the creature and update its x and y velocities
-
+
}
public Creature[] getNeighbors(float radius) {
return null;
}
};
+final float FLOCK_CENTERING_RADIUS = 0.2f;
+
+HashMap flockCenterGrid;
+
+public void initNeighborGrids() {
+ flockCenterGrid = new HashMap();
+}
+
public void computeNeighborGrids() {
//use active forces and compute separate neighbor grids for each force
- print(wanderingForce);
+ float radius = FLOCK_CENTERING_RADIUS;
+ flockCenterGrid.clear();
+ for (int i = 0; i < NUM_CREATURES; i++) {
+ String key = PApplet.parseInt(creatures[i].posX/radius) + "," + PApplet.parseInt(creatures[i].posY/radius);
+ ArrayList val = (ArrayList)flockCenterGrid.get(key);
+ if (val != null) {
+ val.add(i);
+ }
+ else {
+ flockCenterGrid.put(key, new ArrayList());
+ }
+ }
+
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "murali_varma_hw2" };
@@ -22,6 +22,7 @@ void setup() {
background(0);
stroke(100);
+ initNeighborGrids();
initCreatures();
}

0 comments on commit e082e6e

Please sign in to comment.