Skip to content
Browse files

add modified files for previous commit

  • Loading branch information...
1 parent a5ae6af commit b406e34454cc093f8cdd9f2c9c09cdde6e566b3b @barthez committed Mar 19, 2012
Showing with 51 additions and 88 deletions.
  1. +4 −75 boids.js
  2. +1 −0 index.html
  3. +14 −1 init.js
  4. +31 −10 simulation.js
  5. +1 −2 style.css
View
79 boids.js
@@ -52,7 +52,7 @@ function Boid(id, x, y) {
this.id = id;
this.velocity = new Vector(0,0);
this.color = "green";
-
+ this.rules = [];
}
Boid._last_id = 0;
@@ -91,93 +91,22 @@ Boid.prototype.draw = function(ctx) {
}
Boid.prototype.update = function(boids) {
-// console.log(this.toString());
-// console.log("Updating " + this.id);
-// if (this.color == '#000') {
-// debugger
-// }
+
var v = new Vector(0,0);
for(var i=0; i<this.rules.length; ++i) {
- var v1 = this.rules[i](this, boids);
-// debugger;
+ var v1 = this.rules[i].computeVelocity(this, boids, []);
v.add(v1);
}
-// debugger
this.velocity.add(v);
var len = this.velocity.length();
//Limit the speed
- var speed_limit = 5;
+ var speed_limit = 3;
if (len > speed_limit) this.velocity.times(speed_limit/len);
this.add(this.velocity);
-// console.log(this.toString());
}
-Boid.prototype.rules = [
- //RULE1: Go to center of swarm
- function(b, boids) {
- var vel = new Vector(0,0);
-
- for(var i=0; i < boids.length; ++i) {
- if (boids[i].id != b.id) {
- //var v_diff = b.copy().diff(boids[i]);
- //if (v_diff.length() < 500) {
- vel.add(boids[i]);
- //}
- }
- }
- //debugger;
- return vel.times(1/(boids.length-1)).diff(b).times(1/500);
- },
- //RULE2: Keep distance from nearest boids
- function(b, boids) {
- var vel = new Vector(0,0);
-
- for(var i=0; i < boids.length; ++i) {
- if (boids[i].id != b.id) {
- var v_diff = b.copy().diff(boids[i]);
- if (v_diff.length() < 40) {
- vel.diff(v_diff);
- }
- }
- }
- //debugger;
- return vel.times(-1/100);
- },
- //RULE3: Keep mean velocity of swarm
- function(b, boids) {
- var vel = new Vector(0,0);
-
- for(var i=0; i < boids.length; ++i) {
- if (boids[i].id != b.id) {
- vel.add(b.velocity);
- }
- }
- //debugger;
- return vel.times( 1/(boids.length-1) ).diff(b.velocity).times(1/50);
- },
- //RULE4: Don't go out of box
- function(b) {
- var vel = new Vector(0,0);
- var power = 0.05;
- var d = 50;
- if (b.x < d) {
- vel.x = (d - b.x)*power;
- } else if (b.x > 800 - d) {
- vel.x = (800 - d - b.x)*power;
- }
-
- if (b.y < d) {
- vel.y = (d - b.y)*power;
- } else if (b.y > 600 -d) {
- vel.y = (600 -d - b.y)*power;
- }
-
- return vel;
- }
-];
-
Boid.prototype.toString = function() {
return this.id + " (" + this.x + ", " + this.y +")";
}
View
1 index.html
@@ -5,6 +5,7 @@
<title>Boids</title>
<script type="text/javascript" src="./requestAnimationFrame.js" ></script>
<script type="text/javascript" src="./utils.js" ></script>
+<script type="text/javascript" src="./rules.js" ></script>
<script type="text/javascript" src="./boids.js" ></script>
<script type="text/javascript" src="./simulation.js" ></script>
<script type="text/javascript" src="./init.js" ></script>
View
15 init.js
@@ -5,8 +5,15 @@
window.addEventListener('load', function() {
var canvas = document.getElementById('boids_canvas');
var context = canvas.getContext('2d');
+
+ var canvas_size = new Vector(canvas.width -10, canvas.height-10);
+ var boids_rules = [ new SteerToCenter(),
+ new KeepDistance(),
+ new KeepSpeed(),
+ new KeepInsideBox(new Vector(10,10), canvas_size),
+ new StaticForce( new Vector(0.02, 0) ) ];
- var sim = new Simulation(context, 10);
+ var sim = new Simulation(context, 10, boids_rules);
var settings_form = document.getElementById('settings');
settings.addEventListener('submit', function(event) {
@@ -30,6 +37,12 @@
}
}, true);
+ canvas.addEventListener('click', function(event) {
+ console.log(event);
+ var point = new Vector(event.offsetX, event.offsetY);
+ sim.addBoidsRule( new AttractToPoint(point, 5000) );
+ }, true);
+
//Run simulation
sim.run();
View
41 simulation.js
@@ -1,6 +1,9 @@
-function Simulation(ctx, boids_number) {
+function Simulation(ctx, boids_number, boids_rules) {
if (!boids_number) boids_number = 10;
+ if (!boids_rules || boids_rules.length == 0) {
+ boids_rules = BehaviourRule.DefaultBoidsRules();
+ }
this._ctx = ctx;
this._boids = [];
this._boids_number = boids_number;
@@ -23,6 +26,7 @@ function Simulation(ctx, boids_number) {
'#67e667'
]);
+ this.boids_rules = boids_rules;
this.__init();
}
@@ -50,20 +54,30 @@ Simulation.prototype.updateBoidsNumber = function(number) {
if(!number || number < 2) number = 2;
var diff = number - this._boids.length;
if (diff > 0) {
- for(var i=0; i<diff; ++i) {
- var x = (0.5 + Math.random() * 800) | 0;
- var y = (0.5 + Math.random() * 600) | 0;
- var b = new Boid(Boid.getNextId(), x, y);
- b.color = this._boids_color_generator.nextColor();
- this._boids.push(b);
- }
-
+ for(var i=0; i<diff; ++i) {
+ var x = (0.5 + Math.random() * 800) | 0;
+ var y = (0.5 + Math.random() * 600) | 0;
+ var b = new Boid(Boid.getNextId(), x, y);
+ b.color = this._boids_color_generator.nextColor();
+ b.rules = this.boids_rules;
+ this._boids.push(b);
+ }
+ return diff;
} else if (diff < 0) {
var d = Math.abs(diff);
this._boids.splice(this._boids.length-d,d);
this._boids_number = this._boids.length;
+ return diff;
}
- return false;
+ return 0;
+};
+
+Simulation.prototype.addBoidsRule = function(rule) {
+ this.boids_rules.push(rule);
+
+// for(var i=0; i < this._boids.length; i++) {
+// this._boids[i].rules.push(rule);
+// }
};
@@ -76,6 +90,7 @@ Simulation.prototype.__init = function() {
var y = (0.5 + Math.random() * 600) | 0;
var b = new Boid(Boid.getNextId(), x, y);
b.color = this._boids_color_generator.nextColor();
+ b.rules = this.boids_rules;
this._boids.push(b);
}
};
@@ -97,6 +112,12 @@ Simulation.prototype.__update = function() {
Simulation.prototype.__draw = function() {
this._ctx.setTransform(1,0,0,1,0,0);
this._ctx.clearRect(0,0,800,600);
+
+ for(var i=0; i < this.boids_rules.length; i++) {
+ this.boids_rules[i].draw(this._ctx);
+ }
+
+
for(var i=0; i < this._boids.length; i++) {
this._boids[i].draw(this._ctx);
}
View
3 style.css
@@ -1,11 +1,10 @@
body {
- width: 802px;
+ width: 800px;
margin: 0 auto;
font-family: Tahoma, Verdana, sans-serif;
}
canvas#boids_canvas {
- border: #000 1px solid;
width: 800px;
height: 600px;
display: block;

0 comments on commit b406e34

Please sign in to comment.
Something went wrong with that request. Please try again.