Permalink
Browse files

Added wandering force

  • Loading branch information...
1 parent 9811314 commit 60fdad1196d73585273aa5d33d31314d9cb4fa0b @muraliavarma committed Feb 2, 2013
@@ -9,6 +9,9 @@ class Creature {
float velX;
float velY;
+ float forceX;
+ float forceY;
+
float radius = 10;
ArrayList neighbors;
@@ -17,8 +20,6 @@ class Creature {
idx = i;
posX = random(1);
posY = random(1);
- velX = 0.002 - random(0.004);
- velY = 0.002 - random(0.004);
neighbors = new ArrayList();
}
@@ -31,6 +32,8 @@ class Creature {
void update() {
neighbors = getNeighbors(FLOCK_CENTERING_RADIUS);
applyForces();
+ velX += forceX;
+ velY += forceY;
posX += velX;
posY += velY;
@@ -62,8 +65,14 @@ class Creature {
}
void applyForces() {
- //apply the 3 forces to the creature and update its x and y velocities
+ forceX = 0;
+ forceY = 0;
+ //apply the 4 forces to the creature and update its x and y velocities
+ if (wanderingForce) {
+ forceX = 0.0002 - random(0.0004);
+ forceY = 0.0002 - random(0.0004);
+ }
}
ArrayList getNeighbors(float radius) {
Binary file not shown.
Binary file not shown.
@@ -76,6 +76,7 @@ public void draw() {
}
public void keyPressed() {
+ //simulation
if (key == 'q') {
noLoop();
}
@@ -86,6 +87,11 @@ else if(key == 'w') {
else {
loop();
}
+
+ //forces
+ if (key == '4') {
+ wanderingForce = !wanderingForce;
+ }
}
Creature[] creatures;
@@ -98,6 +104,9 @@ else if(key == 'w') {
float velX;
float velY;
+ float forceX;
+ float forceY;
+
float radius = 10;
ArrayList neighbors;
@@ -106,8 +115,6 @@ else if(key == 'w') {
idx = i;
posX = random(1);
posY = random(1);
- velX = 0.002f - random(0.004f);
- velY = 0.002f - random(0.004f);
neighbors = new ArrayList();
}
@@ -120,6 +127,8 @@ public void draw() {
public void update() {
neighbors = getNeighbors(FLOCK_CENTERING_RADIUS);
applyForces();
+ velX += forceX;
+ velY += forceY;
posX += velX;
posY += velY;
@@ -151,8 +160,14 @@ public void update() {
}
public void applyForces() {
- //apply the 3 forces to the creature and update its x and y velocities
+ forceX = 0;
+ forceY = 0;
+ //apply the 4 forces to the creature and update its x and y velocities
+ if (wanderingForce) {
+ forceX = 0.0002f - random(0.0004f);
+ forceY = 0.0002f - random(0.0004f);
+ }
}
public ArrayList getNeighbors(float radius) {
@@ -61,6 +61,7 @@ void draw() {
}
void keyPressed() {
+ //simulation
if (key == 'q') {
noLoop();
}
@@ -71,4 +72,9 @@ void keyPressed() {
else {
loop();
}
+
+ //forces
+ if (key == '4') {
+ wanderingForce = !wanderingForce;
+ }
}

0 comments on commit 60fdad1

Please sign in to comment.