Permalink
Browse files

Mouse repel/attraction forces added. Param tweaking required

  • Loading branch information...
1 parent dc68b0e commit 7c408773de66c12fb142c18b0d512493541e4807 @muraliavarma committed Feb 6, 2013
View
22 murali_varma_hw2/Creature.pde
@@ -82,16 +82,20 @@ class Creature {
float distSqTo(int j) {
Creature other = creatures[j];
- float diffX = abs(posX - other.posX);
+ return distSqTo(other.posX, other.posY);
+ }
+
+ float distSqTo(float x, float y) {
+ float diffX = abs(posX - x);
if (edgeBehavior == TOROIDAL_MODE) {
diffX = min(diffX, 1 - diffX);
}
- float diffY = abs(posY - other.posY);
+ float diffY = abs(posY - y);
if (edgeBehavior == TOROIDAL_MODE) {
diffY = min(diffY, 1 - diffY);
}
- return diffX * diffX + diffY * diffY;
+ return diffX * diffX + diffY * diffY;
}
void applyForces() {
@@ -157,6 +161,18 @@ class Creature {
forceY += fy/weightSum;
}
}
+
+ if (mousePressed) {
+ float x = (1.0 * mouseX)/SCREEN_WIDTH;
+ float y = (1.0 * mouseY)/SCREEN_HEIGHT;
+ int sign = mouseMode == ATTRACT_MODE ? -1 : 1;
+ float dist = distSqTo(x, y);
+ if (dist < MOUSE_RADIUS) {
+ float weight = 1/(dist + EPSILON);
+ forceX += sign * MOUSE_WEIGHT * (posX - x) * weight;
+ forceY += sign * MOUSE_WEIGHT * (posY - y) * weight;
+ }
+ }
}
ArrayList getNeighbors(float radius, HashMap grid) {
View
14 murali_varma_hw2/Input.pde
@@ -42,11 +42,17 @@ void keyPressed() {
//reinit this newly added creature
creatures[NUM_CREATURES-1].init();
}
-
- if (key == '-' || key == '-') {
+ if (key == '-' || key == '_') {
NUM_CREATURES = max(NUM_CREATURES-1, MIN_CREATURES);
}
+ if (key == 'a' || key == 'A') {
+ mouseMode = ATTRACT_MODE;
+ }
+ if (key == 'r' || key == 'R') {
+ mouseMode = REPEL_MODE;
+ }
+
//forces
if (key == '1') {
flockCenteringForce = !flockCenteringForce;
@@ -61,7 +67,3 @@ void keyPressed() {
wanderingForce = !wanderingForce;
}
}
-
-void mousePressed() {
-
-}
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
44 murali_varma_hw2/build-tmp/source/murali_varma_hw2.java
@@ -21,6 +21,9 @@
final int REFLECT_MODE = 0;
final int TOROIDAL_MODE = 1;
+final int ATTRACT_MODE = 0;
+final int REPEL_MODE = 1;
+
final int MIN_CREATURES = 1;
final int MAX_CREATURES = 100;
@@ -29,16 +32,19 @@
final float FLOCK_CENTERING_RADIUS = 0.1f;
final float COLLISION_AVOIDANCE_RADIUS = 0.05f;
final float VELOCITY_MATCHING_RADIUS = 0.1f;
+final float MOUSE_RADIUS = 0.1f;
final float FLOCKING_CENTERING_WEIGHT = 0.0001f;
final float COLLISION_AVOIDANCE_WEIGHT = 0.001f;
final float VELOCITY_MATCHING_WEIGHT = 0.1f;
final float WANDERING_WEIGHT = 0.0002f;
+final float MOUSE_WEIGHT = 0.00001f;
-int NUM_CREATURES = 10;
+int NUM_CREATURES = 100;
boolean isLoop = true;
int edgeBehavior = TOROIDAL_MODE;
+int mouseMode = ATTRACT_MODE;
int backgroundAlpha = 10; //0 for full trail, 255 for no trail
//flock centering, velocity matching, collision avoidance, wandering force
@@ -180,16 +186,20 @@ public void update() {
public float distSqTo(int j) {
Creature other = creatures[j];
- float diffX = abs(posX - other.posX);
+ return distSqTo(other.posX, other.posY);
+ }
+
+ public float distSqTo(float x, float y) {
+ float diffX = abs(posX - x);
if (edgeBehavior == TOROIDAL_MODE) {
diffX = min(diffX, 1 - diffX);
}
- float diffY = abs(posY - other.posY);
+ float diffY = abs(posY - y);
if (edgeBehavior == TOROIDAL_MODE) {
diffY = min(diffY, 1 - diffY);
}
- return diffX * diffX + diffY * diffY;
+ return diffX * diffX + diffY * diffY;
}
public void applyForces() {
@@ -255,6 +265,18 @@ public void applyForces() {
forceY += fy/weightSum;
}
}
+
+ if (mousePressed) {
+ float x = (1.0f * mouseX)/SCREEN_WIDTH;
+ float y = (1.0f * mouseY)/SCREEN_HEIGHT;
+ int sign = mouseMode == ATTRACT_MODE ? -1 : 1;
+ float dist = distSqTo(x, y);
+ if (dist < MOUSE_RADIUS) {
+ float weight = 1/(dist + EPSILON);
+ forceX += sign * MOUSE_WEIGHT * (posX - x) * weight;
+ forceY += sign * MOUSE_WEIGHT * (posY - y) * weight;
+ }
+ }
}
public ArrayList getNeighbors(float radius, HashMap grid) {
@@ -306,11 +328,17 @@ public void keyPressed() {
//reinit this newly added creature
creatures[NUM_CREATURES-1].init();
}
-
- if (key == '-' || key == '-') {
+ if (key == '-' || key == '_') {
NUM_CREATURES = max(NUM_CREATURES-1, MIN_CREATURES);
}
+ if (key == 'a' || key == 'A') {
+ mouseMode = ATTRACT_MODE;
+ }
+ if (key == 'r' || key == 'R') {
+ mouseMode = REPEL_MODE;
+ }
+
//forces
if (key == '1') {
flockCenteringForce = !flockCenteringForce;
@@ -325,10 +353,6 @@ public void keyPressed() {
wanderingForce = !wanderingForce;
}
}
-
-public void mousePressed() {
-
-}
HashMap flockCenterGrid;
HashMap collisionAvoidanceGrid;
HashMap velocityMatchingGrid;
View
8 murali_varma_hw2/murali_varma_hw2.pde
@@ -6,6 +6,9 @@ final int CONTROLS_WIDTH = 100;
final int REFLECT_MODE = 0;
final int TOROIDAL_MODE = 1;
+final int ATTRACT_MODE = 0;
+final int REPEL_MODE = 1;
+
final int MIN_CREATURES = 1;
final int MAX_CREATURES = 100;
@@ -14,16 +17,19 @@ final float EPSILON = 0.01;
final float FLOCK_CENTERING_RADIUS = 0.1;
final float COLLISION_AVOIDANCE_RADIUS = 0.05;
final float VELOCITY_MATCHING_RADIUS = 0.1;
+final float MOUSE_RADIUS = 0.1;
final float FLOCKING_CENTERING_WEIGHT = 0.0001;
final float COLLISION_AVOIDANCE_WEIGHT = 0.001;
final float VELOCITY_MATCHING_WEIGHT = 0.1;
final float WANDERING_WEIGHT = 0.0002;
+final float MOUSE_WEIGHT = 0.00001;
-int NUM_CREATURES = 10;
+int NUM_CREATURES = 100;
boolean isLoop = true;
int edgeBehavior = TOROIDAL_MODE;
+int mouseMode = ATTRACT_MODE;
int backgroundAlpha = 10; //0 for full trail, 255 for no trail
//flock centering, velocity matching, collision avoidance, wandering force

0 comments on commit 7c40877

Please sign in to comment.