-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboid_node.js
More file actions
99 lines (94 loc) · 2.05 KB
/
boid_node.js
File metadata and controls
99 lines (94 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var vector = require('./vector');
function boid(p,v){
this.pos=p;
this.vel=v;
this.rules=[];
this.separation=function(boidset, c){
var ans=new vector(0,0);
var i;
for(i=0; i<boidset.length;i++){
var current=boidset[i];
if (current != this){
if ((new vector).vectorDist(current.pos, this.pos)<5){
ans.sub((new vector).vectorSub(current.pos, this.pos));
}
}
}
this.rules.push(ans);
}
this.cohesion=function(boidset, c){
var ans = new vector(0,0);
var count = 0;
var i;
for(i=0;i<boidset.length;i++){
var current=boidset[i];
if (current != this){
if ((new vector).vectorDist(current.pos,this.pos)<80){
count++;
ans.add(current.pos);
}
}
}
if (count > 0){
ans.div(count);
ans.sub(this.pos);
}
this.rules.push(ans.mult(c));
}
this.adhesion=function(boidset, c){
var ans = new vector(0,0);
var count = 0;
var i;
for (i=0;i<boidset.length;i++){
var current = boidset[i];
if (current != this){
if ((new vector).vectorDist(current.pos,this.pos)<50){
count++;
ans.add(current.vel);
}
}
}
if (count>0){
ans.div(count);
ans.sub(this.vel);
}
this.rules.push(ans.mult(c));
}
this.boundary=function(c){
var ans = new vector(0,0);
if (this.pos.x > 500){
ans.add(new vector(50-this.pos.x/10,0));
}
if (this.pos.x < 0){
ans.add(new vector(-this.pos.x/10,0));
}
if (this.pos.y > 500){
ans.add(new vector(0,50-this.pos.y/10));
}
if (this.pos.y < 0){
ans.add(new vector(0,-this.pos.y/10));
}
this.rules.push(ans.mult(c));
}
this.interact=function(boidset){
this.rules = [];
this.separation(boidset,1.2);
this.cohesion(boidset,0.01);
this.adhesion(boidset,0.3);
this.boundary(3.6);
//console.log(this.rules);
var i;
for (i=0;i<this.rules.length;i++){
this.vel.add(this.rules[i]);
}
this.vel.limit(4,30);
this.pos.add(this.vel);
}
this.draw=function(ctx){
ctx.moveTo(this.pos.x,this.pos.y);
ctx.lineTo(this.pos.x-this.vel.x,this.pos.y-this.vel.y);
ctx.stroke();
}
return this;
}
module.exports=boid;