Permalink
Browse files

Added velocity matching force

  • Loading branch information...
1 parent 633a236 commit f52033c4a0be4066df678d9d1a84ff86872b4ebd @muraliavarma committed Feb 5, 2013
View
20 murali_varma_hw2/Creature.pde
@@ -16,6 +16,7 @@ class Creature {
ArrayList neighborsFC;
ArrayList neighborsCA;
+ ArrayList neighborsVM;
Creature(int i) {
idx = i;
@@ -34,6 +35,7 @@ class Creature {
void update() {
neighborsFC = getNeighbors(FLOCK_CENTERING_RADIUS, flockCenterGrid);
neighborsCA = getNeighbors(COLLISION_AVOIDANCE_RADIUS, collisionAvoidanceGrid);
+ neighborsVM = getNeighbors(VELOCITY_MATCHING_RADIUS, velocityMatchingGrid);
applyForces();
velX += forceX;
velY += forceY;
@@ -117,6 +119,24 @@ class Creature {
forceY += fy/weightSum;
}
}
+
+ if (velocityMatchingForce) {
+ float weightSum = 0;
+ float fx = 0;
+ float fy = 0;
+ for (int i = 0; i < neighborsVM.size(); i++) {
+ Creature neighbor = creatures[int(neighborsVM.get(i).toString())];
+ float weight = 1;
+ weightSum += weight;
+ fx += weight * (neighbor.velX - velX);
+ fy += weight * (neighbor.velY - velY);
+ }
+ weightSum *= 100;
+ if (weightSum != 0) {
+ forceX += fx/weightSum;
+ forceY += fy/weightSum;
+ }
+ }
}
ArrayList getNeighbors(float radius, HashMap grid) {
View
14 murali_varma_hw2/Neighbors.pde
@@ -1,12 +1,15 @@
final float FLOCK_CENTERING_RADIUS = 0.2;
final float COLLISION_AVOIDANCE_RADIUS = 0.1;
+final float VELOCITY_MATCHING_RADIUS = 0.1;
HashMap flockCenterGrid;
HashMap collisionAvoidanceGrid;
+HashMap velocityMatchingGrid;
void initNeighborGrids() {
flockCenterGrid = new HashMap();
collisionAvoidanceGrid = new HashMap();
+ velocityMatchingGrid = new HashMap();
}
void computeNeighborGrids() {
@@ -34,6 +37,17 @@ void computeNeighborGrids() {
}
((ArrayList)collisionAvoidanceGrid.get(key)).add(i);
}
+
+ radius = VELOCITY_MATCHING_RADIUS;
+ velocityMatchingGrid.clear();
+ for (int i = 0; i < NUM_CREATURES; i++) {
+ String key = int(creatures[i].posX/radius) + "," + int(creatures[i].posY/radius);
+ ArrayList val = (ArrayList)velocityMatchingGrid.get(key);
+ if (val == null) {
+ velocityMatchingGrid.put(key, new ArrayList());
+ }
+ ((ArrayList)velocityMatchingGrid.get(key)).add(i);
+ }
}
ArrayList getNearestNeighbors(int idx, float radius, HashMap grid) {
View
BIN murali_varma_hw2/build-tmp/murali_varma_hw2$Creature.class
Binary file not shown.
View
BIN murali_varma_hw2/build-tmp/murali_varma_hw2.class
Binary file not shown.
View
40 murali_varma_hw2/build-tmp/source/murali_varma_hw2.java
@@ -29,10 +29,10 @@
int backgroundAlpha = 100; //0 for full trail, 255 for no trail
//flock centering, velocity matching, collision avoidance, wandering force
-boolean flockCenteringForce = true;
-boolean velocityMatchingForce = true;
+boolean flockCenteringForce = false;
+boolean velocityMatchingForce = false;
boolean collisionAvoidanceForce = true;
-boolean wanderingForce = true;
+boolean wanderingForce = false;
public void setup() {
size(SCREEN_WIDTH + CONTROLS_WIDTH, SCREEN_HEIGHT);
@@ -122,6 +122,7 @@ else if(key == 'w') {
ArrayList neighborsFC;
ArrayList neighborsCA;
+ ArrayList neighborsVM;
Creature(int i) {
idx = i;
@@ -140,6 +141,7 @@ public void draw() {
public void update() {
neighborsFC = getNeighbors(FLOCK_CENTERING_RADIUS, flockCenterGrid);
neighborsCA = getNeighbors(COLLISION_AVOIDANCE_RADIUS, collisionAvoidanceGrid);
+ neighborsVM = getNeighbors(VELOCITY_MATCHING_RADIUS, velocityMatchingGrid);
applyForces();
velX += forceX;
velY += forceY;
@@ -223,6 +225,24 @@ public void applyForces() {
forceY += fy/weightSum;
}
}
+
+ if (velocityMatchingForce) {
+ float weightSum = 0;
+ float fx = 0;
+ float fy = 0;
+ for (int i = 0; i < neighborsVM.size(); i++) {
+ Creature neighbor = creatures[PApplet.parseInt(neighborsVM.get(i).toString())];
+ float weight = 1;
+ weightSum += weight;
+ fx += weight * (neighbor.velX - velX);
+ fy += weight * (neighbor.velY - velY);
+ }
+ weightSum *= 10000;
+ if (weightSum != 0) {
+ forceX += fx/weightSum;
+ forceY += fy/weightSum;
+ }
+ }
}
public ArrayList getNeighbors(float radius, HashMap grid) {
@@ -232,13 +252,16 @@ public ArrayList getNeighbors(float radius, HashMap grid) {
final float FLOCK_CENTERING_RADIUS = 0.2f;
final float COLLISION_AVOIDANCE_RADIUS = 0.1f;
+final float VELOCITY_MATCHING_RADIUS = 0.1f;
HashMap flockCenterGrid;
HashMap collisionAvoidanceGrid;
+HashMap velocityMatchingGrid;
public void initNeighborGrids() {
flockCenterGrid = new HashMap();
collisionAvoidanceGrid = new HashMap();
+ velocityMatchingGrid = new HashMap();
}
public void computeNeighborGrids() {
@@ -266,6 +289,17 @@ public void computeNeighborGrids() {
}
((ArrayList)collisionAvoidanceGrid.get(key)).add(i);
}
+
+ radius = VELOCITY_MATCHING_RADIUS;
+ velocityMatchingGrid.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)velocityMatchingGrid.get(key);
+ if (val == null) {
+ velocityMatchingGrid.put(key, new ArrayList());
+ }
+ ((ArrayList)velocityMatchingGrid.get(key)).add(i);
+ }
}
public ArrayList getNearestNeighbors(int idx, float radius, HashMap grid) {
View
6 murali_varma_hw2/murali_varma_hw2.pde
@@ -14,10 +14,10 @@ int edgeBehavior = REFLECT_MODE;
int backgroundAlpha = 100; //0 for full trail, 255 for no trail
//flock centering, velocity matching, collision avoidance, wandering force
-boolean flockCenteringForce = true;
-boolean velocityMatchingForce = true;
+boolean flockCenteringForce = false;
+boolean velocityMatchingForce = false;
boolean collisionAvoidanceForce = true;
-boolean wanderingForce = true;
+boolean wanderingForce = false;
void setup() {
size(SCREEN_WIDTH + CONTROLS_WIDTH, SCREEN_HEIGHT);

0 comments on commit f52033c

Please sign in to comment.