// boidFlock flock; void setup() { size(800,800); background(0,166,220); flock = new boidFlock(10, new Vector2D(width/2,height/2)); frameRate(20); } void draw() { flock.draw(); flock.update(); } class boidFlock { int flockCount; boid[] boids; boid[] boidnext; public boidFlock(int boidcount, Vector2D startpos) { boids = new boid[boidcount]; boidnext = new boid[boidcount]; for(int i = 0; i < boidcount; i++) { boids[i] = new boid(new Vector2D(startpos.x + random(10), startpos.y + random(10)),random(2 * PI), 10); boidnext[i] = new boid(new Vector2D(startpos.x + random(10), startpos.y + random(10)),1, 10); } } public void draw() { background(0,166,220); for(int i = 0; i < boids.length; i++) { boids[i].draw(); } } public void update() { for(int i = 0; i < boids.length; i++) { boidnext[i].update(this, i); } boid[] temp = boids; boids = boidnext; boidnext = temp; } } class boid { Vector2D position; float orientation; //z rotation in radians float speed; public boid(Vector2D initposition, float initorientation, float initspeed) { position = initposition; orientation = initorientation; speed = initspeed; } public void draw() { pushMatrix(); translate(position.x, position.y); fill(255,255,255); float coso = cos(orientation); float sino = sin(orientation); // point b = 5, -10 // [ sin cos] [5] = cos * 5 - sin * -10 // [ -cos sin ] [-10] = sin* 5 + cos* -10 float bvectx = sino * 5 - coso * 15; float bvecty = -coso * 5 - sino * 15; float cvectx = -sino * 5 - coso * 15; float cvecty = coso * 5 - sino * 15; noStroke(); triangle(0,0, bvectx, bvecty, cvectx, cvecty); popMatrix(); } public void update(boidFlock flock, int me) { boid oldme = flock.boids[me]; float coso = cos(oldme.orientation); float sino = sin(oldme.orientation); float bvectx = coso; float bvecty = sino; position.x = oldme.position.x + speed * bvectx; position.y = oldme.position.y + speed * bvecty; orientation = oldme.orientation + PI/12; } } class Vector2D { public float x; public float y; public Vector2D(float xcoord, float ycoord) { x = xcoord; y = ycoord; } public float dot(Vector2D first, Vector2D sec) { return first.x * sec.x + first.y * sec.y; } public Vector2D cross(Vector2D first, Vector2D sec) { return new Vector2D(first.x * sec.y, sec.x* first.y); } }