How to make the triangles/boids align/follow; flocking
in
Contributed Library Questions
•
1 year ago
I am trying to make a flocking script, I have made a really basic one, but I'm not sure how to make the triangles follow each other. Has this got something to do with PVector? I have read about thins but don't fully understand it.
Does anyone know any good tutorials about it?
import toxi.geom.*;
//DECLARE
ArrayList agentCollection;
void setup() {
size (600,600);
smooth();
//INITIALISE
agentCollection=new ArrayList();
for(int i=0; i<100; i++) {
Vec3D origin= new Vec3D(random(width),random (200),0);
Agent myAgent= new Agent(origin);
agentCollection.add(myAgent);
}
}
void draw() {
background(0);
//CALL FUNCTIONALITY
for(int i=0; i< agentCollection.size(); i++) {
Agent mb =(Agent) agentCollection.get(i);
mb.run();
}
}
class Agent {
//GLOBAL VARIABLES
Vec3D loc = new Vec3D (0, 0, 0);
Vec3D speed = new Vec3D(random(-2,2), random(-2,2),0);
Vec3D acc = new Vec3D();
Vec3D grav = new Vec3D(0,0.2,0);
//CONSTRUCTORS
Agent(Vec3D _loc) {
loc = _loc;
}
//FUNCTIONS
void run() {
display();
move();
bounce();
//gravity();
lineBetween();
flock();
}
void flock(){
seperate(5);
cohesion(0.001);
align(1);
}
void align(float magnitude){
Vec3D steer = new Vec3D();
int count = 0;
for(int i = 0; i < agentCollection.size();i++) {
Agent other = (Agent) agentCollection.get(i);
float distance = loc.distanceTo(other.loc);
if (distance > 0 && distance < 40) {
steer.addSelf(other.speed);
count++;
}
}
if(count > 0){
steer.scaleSelf(1.0/count);
}
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void cohesion(float magnitude){
Vec3D sum = new Vec3D();
int count = 0;
for(int i = 0; i < agentCollection.size();i++) {
Agent other = (Agent) agentCollection.get(i);
float distance = loc.distanceTo(other.loc);
if (distance > 0 && distance < 60) {
sum.addSelf(other.loc);
count++;
}
}
if(count>0){
sum.scaleSelf(1.0/count);
}
Vec3D steer = sum.sub(loc);
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void seperate(float magnitude) {
Vec3D steer = new Vec3D();
int count = 0;
for(int i = 0; i < agentCollection.size();i++) {
Agent other = (Agent) agentCollection.get(i);
float distance = loc.distanceTo(other.loc);
if (distance > 0 && distance < 30) {
Vec3D diff = loc.sub(other.loc);
diff.normalizeTo(1.0/distance);
steer.addSelf(diff);
count++;
}
}
if(count > 0){
steer.scaleSelf(1.0/count);
}
steer.scaleSelf(magnitude);
acc.addSelf(steer);
}
void lineBetween() {
//ballCollection
for (int i=0; i < agentCollection.size();i++) {
Agent other = (Agent) agentCollection.get(i);
float distance = loc.distanceTo(other.loc);
if (distance > 0 && distance < 40) {
stroke(255,0,0);
strokeWeight(0.4);
line(loc.x,loc.y,other.loc.x,other.loc.y);
}
}
}
void gravity() {
speed.addSelf(grav);
}
void bounce() {
if (loc.x > width) {
speed.x =speed.x * -1;
}
if (loc.x < 0) {
speed.x =speed.x * -1;
}
if (loc.y > height) {
speed.y =speed.y * -1;
}
if (loc.y < 0) {
speed.y =speed.y * -1;
}
}
void move() {
speed.addSelf(acc);
speed.limit(2);
loc.addSelf(speed);
acc.clear();
}
void display() {
stroke(0);
triangle(loc.x,loc.y,loc.x+2,loc.y+10,loc.x-2,loc.y+10);
}
}
1