Skip to content

Instantly share code, notes, and snippets.

@masakick
Created December 24, 2015 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masakick/f783fca8eed8b8268751 to your computer and use it in GitHub Desktop.
Save masakick/f783fca8eed8b8268751 to your computer and use it in GitHub Desktop.
/*
* Swarm trajectory
Copyright (c) 2015 Masaki Yamabe.
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
* note
it's required 'Processing v2.2.1' and 'iGeo v0.9.1.6'
http://igeo.jp/download.html
*/
import processing.opengl.*;
import igeo.*;
void setup(){
size(1200, 768, IG.GL);
IG.bg(0,0,0);
for(int i=0; i<80; i++){
MyBoid b = new MyBoid(IRand.pt(80,80,80,120,120,120), IRand.dir(5));
}
new myAttractor(100,100,100).intensity(150).gaussianDecay(200);
}
class MyBoid extends IBoidTrajectory{
IVec prevPos;
IVec c;
float lifeTime = 300;
MyBoid(IVec p, IVec v){
super(p,v);
cohesionDist(60);
cohesionRatio(2);
separationDist(40);
separationRatio(15);
alignmentDist(40);
alignmentRatio(2);
float t = 0.95*sin(IG.time()*0.001)+0.4;
float t2 = t+0.2;
if(t2 > 1.0) t2 = 1.0;
c = new IVec(IRand.get(t, t2), 1.0, 1.0);
hsb(c.x,c.y,c.z,0.7);
}
void update(){
super.update();
if(time > lifeTime){
del();
}
else{
hsb(c.x,c.y,c.z, 0.85 * pow((lifeTime - time)/lifeTime,0.7));
if(IRand.pct(0.35)){
IVec pos = pos().cp();
IVec dir = vel().cp();
dir.rot(PI/3).mul(1.5);
if(IRand.pct(50)){
dir.flip();
}
MyBoid b = new MyBoid(pos, dir);
}
}
}
}
class myAttractor extends IAttractor{
IFieldVisualizer ifv, pifv;
myAttractor(double x, double y, double z){
super(x,y,z);
}
void update(){
double n = noise(time*0.1);
double n2 = noise(time);
IVec pos2 = new IVec(
100*sin(IG.time()*0.012) + 10*sin(IG.time()*0.1),
100*cos(IG.time()*0.012) + 10*cos(IG.time()*0.1),
50*cos(IG.time()*0.01));
pos().set(pos2);
super.update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment