Permalink
Browse files

Fish are now repelled by edges.

  • Loading branch information...
ctbarna committed May 18, 2012
1 parent 7fa6cbd commit 3c1e0a48454801a1235d9d50268a5df2ac04411c
Showing with 43 additions and 15 deletions.
  1. +43 −15 js/script.js
View
@@ -8,21 +8,22 @@
var props = {
alpha: 0.5,
beta: 1,
- cr: 3,
- ca: 3,
- lr: 20,
- la: 50,
- lc: 4,
- dt: 0.05,
+ cr: 25,
+ ca: 15,
+ lr: 30,
+ la: 60,
+ lc: 10,
+ dt: 0.5,
+ velocity: 1,
play: true
};
// Initialize dat.gui.
var gui = new dat.GUI();
gui.remember(props);
gui.add(props, "play");
- gui.add(props, "alpha", 0, 5);
- gui.add(props, "beta", 0, 5);
+ gui.add(props, "alpha", 0, 10);
+ gui.add(props, "beta", 0, 10);
gui.add(props, "cr", 0, 25);
gui.add(props, "ca", 0, 25);
gui.add(props, "lr", 0, 100);
@@ -56,6 +57,15 @@
this.vx = this.vx + props.dt * this.ax;
this.vy = this.vy + props.dt * this.ay;
+ // Let's set the velocities to a constant magnitude.
+ var v_len = Math.sqrt(Math.pow(this.vx, 2) +
+ Math.pow(this.vy, 2));
+ if (v_len < props.velocity) {
+ this.vx = this.vx / v_len * props.velocity;
+ this.vy = this.vy / v_len * props.velocity;
+ }
+
+ // Move the fish!
this.x = this.x + this.vx;
this.y = this.y + this.vy;
this.element.animate({cx: this.x, cy: this.y}, 5);
@@ -93,25 +103,43 @@
if (fish[i] !== this) {
this.ax = this.ax
- + (props.cr * Math.pow(Math.E, -d / props.lr)
+ + (props.cr * Math.exp(-d / props.lr)
* (-1 / (2 * props.lr))
* (1/d) * (2 * (this.x - fish[i].x)))
- - (props.ca * Math.pow(Math.E, -d / props.la)
+ - (props.ca * Math.exp(-d / props.la)
* (-1 / (2 * props.la))
* (1/d) * (2 * (this.x - fish[i].x)));
- fx = fx + fish[i].vx * Math.pow(Math.E, -d / props.lc);
+ fx = fx + fish[i].vx * Math.exp(-d / props.lc);
this.ay = this.ay
- + (props.cr * Math.pow(Math.E, -d / props.lr)
+ + (props.cr * Math.exp(-d / props.lr)
* (-1 / (2 * props.lr))
* (1/d) * (2 * (this.y - fish[i].y)))
- - (props.ca * Math.pow(Math.E, -d / props.la)
+ - (props.ca * Math.exp(-d / props.la)
* (-1 / (2 * props.la))
* (1/d) * (2 * (this.y - fish[i].y)));
- fy = fy + fish[i].vy * Math.pow(Math.E, -d / props.lc);
+ fy = fy + fish[i].vy * Math.exp(-d / props.lc);
}
}
+ // Avoid the edges.
+ this.ax = this.ax
+ + (props.cr * Math.exp(-this.x / props.lr)
+ * (-1 / (2 * props.lr))
+ * (1 / this.x) * (2 * this.x))
+ - (props.cr * Math.exp(-(width - this.x) / props.lr)
+ * (-1 / (2 * props.lr))
+ * (1 / (width - this.x)) * (2 * (width - this.x)));
+
+ this.ay = this.ay
+ + (props.cr * Math.exp(-this.y / props.lr)
+ * (-1 / (2 * props.lr))
+ * (1/this.y) * (2 * this.y))
+ - (props.cr * Math.exp(-(height - this.y) / props.lr)
+ * (-1 / (2 * props.lr))
+ * (1 / (height - this.y)) * (2 * (height - this.y)));
+
+
this.ax = props.alpha * fx - props.beta
* this.vx - this.ax;
@@ -124,7 +152,7 @@
window.fish = [];
- for (var i = 0; i < 50; i += 1) {
+ for (var i = 0; i < 100; i += 1) {
var randx = Math.random() * width;
var randy = Math.random() * height;
fish.push(new Fish(randx, randy));

0 comments on commit 3c1e0a4

Please sign in to comment.