hey people, just finished reading this awsome book called critical mass does a fantastic job of linking science to society (in my opinion lol read it!). Anyway there is a chapter on how people act when in large groups and there was a brief mention of boids which was a program written in the late 80s to simulate flocking and swarming, things along those lines. I find this kind of stuff quite cool so I wrote my own version in flash. Its not very complex so far but if you want you can tweak it however you want. I don’t think the code is very well written, doesn’t necessaraly have to be done in vectors and stuff (don’t know why i did was just playing around lol).
So far the rules which each of the boids follows is
move towards the center of mass of boids within the reaction area…
if this boids get too close move away from each other…
try to mach the speed of other surrounding boids…
it emerges some quite cool flock type patterns which could be used in games or whatever.
other mods i could imagine being done are predetor interactions. ie run away from predetor, maybe landing occasionally, birds can’t stay in the air forever, or hunting food, maybe several types of birds which stay away from each other.
so here is the swf and actionscript
http://www.kitegallery.net/misc/Slideth/Sam/boids_001.swf.html
flock_size = 10;
reactivity =100;
collision = 20;
//velocity vectors (cohesion, seperation, and speed matching)
a={x:0,y:0}
b={x:0,y:0}
c={x:0,y:0}
for(i=0;i<flock_size;i++){
boid = attachMovie("boid", "boid"+i, i);
boid.pos={x:Math.random()*Stage.width, y:Math.random()*Stage.height}
boid.vel={x:Math.random()*5-2.5, y:Math.random()*5-2.5}
boid.id = i;
boid.cohesion_pos={x:0,y:0}
boid.target_angle = 0;
boid.target_counter = 0;
boid.onEnterFrame = function(){
for(i=0;i<flock_size;i++){
if(i!=this.id){
dx = Math.round(this.pos.x - _root["boid"+i].pos.x)
dy = Math.round(this.pos.y - _root["boid"+i].pos.y)
d = Math.sqrt(dx*dx+dy*dy);
if(d<reactivity){
//cohesion
this.target_counter+=1
this.cohesion_pos.x += _root["boid"+i].pos.x
this.cohesion_pos.y += _root["boid"+i].pos.y
//seperation
if(d<collision){
b.x += (_root["boid"+i].pos.x - this.pos.x)/30
b.y += (_root["boid"+i].pos.y - this.pos.y)/30
}
//speed;
c.x+=_root["boid"+i].vel.x-this.vel.x
c.y+=_root["boid"+i].vel.y-this.vel.y
}
}
}
if(this.target_counter>0){
this.cohesion_pos.x = this.cohesion_pos.x/this.target_counter
this.cohesion_pos.y = this.cohesion_pos.y/this.target_counter
a.x = (this.pos.x - this.cohesion_pos.x)/100
a.y = (this.pos.y - this.cohesion_pos.y)/100
c.x=(c.x/100)/this.target_counter
c.y=(c.y/100)/this.target_counter
this.vel.x -= a.x + b.x - c.x
this.vel.y -= a.y + b.y - c.y
}
this.pos.x += this.vel.x
this.pos.y += this.vel.y
if(this.pos.x>550)this.pos.x=0
if(this.pos.x<0)this.pos.x=550
if(this.pos.y>400)this.pos.y=0
if(this.pos.y<0)this.pos.y=400
if(this.vel.x>2.5) this.vel.x*=0.8
if(this.vel.y>2.5) this.vel.y*=0.8
if(this.vel.x<-2.5) this.vel.x*=0.8
if(this.vel.y<-2.5) this.vel.y*=0.8
this._rotation = Math.atan2(this.vel.y, this.vel.x)*180/Math.PI
this._x = this.pos.x
this._y = this.pos.y
a={x:0,y:0}
b={x:0,y:0}
c={x:0,y:0}
this.cohesion_pos={x:0,y:0}
this.target_counter = 0;
}
}
have fun and make any tweaks you want to the code, show me if you come up with something cool.
I’m gonna try it in 3d now
Sam