Permalink
Browse files

Added flock centering behavior

  • Loading branch information...
1 parent 60fdad1 commit 348ce149d6db1d353bcb02f3386eca191b0e08fb @muraliavarma committed Feb 4, 2013
@@ -34,6 +34,11 @@ class Creature {
applyForces();
velX += forceX;
velY += forceY;
+
+ //clamp velocities
+ velX = max(-0.001, min(velX, 0.001));
+ velY = max(-0.001, min(velY, 0.001));
+
posX += velX;
posY += velY;
@@ -73,6 +78,24 @@ class Creature {
forceX = 0.0002 - random(0.0004);
forceY = 0.0002 - random(0.0004);
}
+
+ if (flockCenteringForce) {
+ float weightSum = 0;
+ float fx = 0;
+ float fy = 0;
+ for (int i = 0; i < neighbors.size(); i++) {
+ Creature neighbor = creatures[int(neighbors.get(i).toString())];
+ float weight = 1/(distSq(idx, neighbor.idx) + EPSILON);
+ weightSum += weight;
+ fx += weight * (neighbor.posX - posX);
+ fy += weight * (neighbor.posY - posY);
+ }
+ weightSum *= 100;
+ if (weightSum != 0) {
+ forceX += fx/weightSum;
+ forceY += fy/weightSum;
+ }
+ }
}
ArrayList getNeighbors(float radius) {
@@ -51,6 +51,12 @@ ArrayList getNearestNeighbors(int idx, float radius) {
return ret;
}
+float distSq(int i, int j) {
+ Creature creatureI = creatures[i];
+ Creature creatureJ = creatures[j];
+ return (creatureI.posX - creatureJ.posX) * (creatureI.posX - creatureJ.posX) + (creatureI.posY - creatureJ.posY) * (creatureI.posY - creatureJ.posY);
+}
+
//DEBUG stuff
void printGrid(int radius) {
Binary file not shown.
@@ -21,7 +21,9 @@
final int REFLECT_MODE = 0;
final int TOROIDAL_MODE = 1;
-final int NUM_CREATURES = 10;
+final int NUM_CREATURES = 100;
+
+final float EPSILON = 0.001f;
int edgeBehavior = REFLECT_MODE;
int backgroundAlpha = 100; //0 for full trail, 255 for no trail
@@ -129,6 +131,11 @@ public void update() {
applyForces();
velX += forceX;
velY += forceY;
+
+ //clamp velocities
+ velX = max(-0.001f, min(velX, 0.001f));
+ velY = max(-0.001f, min(velY, 0.001f));
+
posX += velX;
posY += velY;
@@ -168,6 +175,24 @@ public void applyForces() {
forceX = 0.0002f - random(0.0004f);
forceY = 0.0002f - random(0.0004f);
}
+
+ if (flockCenteringForce) {
+ float weightSum = 0;
+ float fx = 0;
+ float fy = 0;
+ for (int i = 0; i < neighbors.size(); i++) {
+ Creature neighbor = creatures[PApplet.parseInt(neighbors.get(i).toString())];
+ float weight = 1/(distSq(idx, neighbor.idx) + EPSILON);
+ weightSum += weight;
+ fx += weight * (neighbor.posX - posX);
+ fy += weight * (neighbor.posY - posY);
+ }
+ weightSum *= 100;
+ if (weightSum != 0) {
+ forceX += fx/weightSum;
+ forceY += fy/weightSum;
+ }
+ }
}
public ArrayList getNeighbors(float radius) {
@@ -228,6 +253,12 @@ public ArrayList getNearestNeighbors(int idx, float radius) {
return ret;
}
+public float distSq(int i, int j) {
+ Creature creatureI = creatures[i];
+ Creature creatureJ = creatures[j];
+ return (creatureI.posX - creatureJ.posX) * (creatureI.posX - creatureJ.posX) + (creatureI.posY - creatureJ.posY) * (creatureI.posY - creatureJ.posY);
+}
+
//DEBUG stuff
public void printGrid(int radius) {
@@ -6,7 +6,9 @@ final int CONTROLS_WIDTH = 100;
final int REFLECT_MODE = 0;
final int TOROIDAL_MODE = 1;
-final int NUM_CREATURES = 10;
+final int NUM_CREATURES = 100;
+
+final float EPSILON = 0.001;
int edgeBehavior = REFLECT_MODE;
int backgroundAlpha = 100; //0 for full trail, 255 for no trail

0 comments on commit 348ce14

Please sign in to comment.