I want to add wind force on this code and also if possible changing the colours in every movement.Can someone help me or give a idea of that?

Flock flock;
void setup() {
size(500,500);
flock = new Flock();
for (int i=0; i<100; i++) {
Boid b = new Boid(width/1, height/1);
flock.addBoid(b);
}
}

void draw() {
background(100);
flock.run();
}
class Boid {

PVector location;
PVector velocity;
PVector acceleration;
float r;
float maxforce;
float maxspeed;

Boid(float x,float y){
acceleration = new PVector(0,0);
velocity = new PVector(random(-1,1),random(-1,1));
location = new PVector(x,y);
r = 3.0;
maxspeed = 5;
maxforce = 0.05;
}
void run(ArrayListboids){
flock(boids);
update();
borders();
display();
}

void flock(ArrayListboids){
PVector sep = separate(boids);
PVector ali = align(boids);
PVector coh = cohesion(boids);

sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);

applyForce(sep);
applyForce(ali);
applyForce(coh);

}
void applyForce(PVector force){
acceleration.add(force);
}
void update(){
velocity.add(acceleration);
velocity.limit(maxspeed);
location.add(velocity);
acceleration.mult(0);
}
void display(){
float theta = velocity.heading()+PI/2;
fill(random(175),random(200),random(255));
stroke(0);
pushMatrix();
translate(location.x,location.y);
rotate(theta);
beginShape();
vertex(0,-r2);
vertex(-r,r
2);
vertex(r,r*2);
endShape(CLOSE);
popMatrix();
}
void borders(){
if (location.x<-r)location.x=width+r;
if(location.y<-r)location.y=height+r;
if(location.x>width+r)location.x=-r;
if(location.y>height+r)location.y=-r;
}
PVector seek (PVector target){
PVector desired = PVector.sub(target,location);
desired.normalize();
desired.mult(maxspeed);
PVector steer =PVector.sub(desired,velocity);
steer.limit(maxforce);
return steer;
}
PVector separate(ArrayListboids){
float desiredseparation =150;
PVector sum =new PVector();
int count =0;
for (Boid other:boids){
float d =PVector.dist(location,other.location);
if((d>0)&&(d<desiredseparation)){
PVector diff = PVector.sub(location,other.location);
diff.normalize();
diff.div(d);
sum.add(diff);
count++;
}
}
if(count>0){
sum.div(count);
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum,velocity);
steer.limit(maxforce);
return steer;
}else{
return new PVector(0,0);
}
}
PVector align(ArrayListboids){
float neighbordist = 500;
PVector sum = new PVector(0,0);
int count = 0;
for(Boid other:boids){
float d = PVector.dist(location,other.location);
if((d>0)&&(d<neighbordist)){
sum.add(other.velocity);
count++;
}
}
if(count>0){
sum.div(count);
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum,velocity);
steer.limit(maxforce);
return steer;
}else{
return new PVector(0,0);
}
}
PVector cohesion(ArrayListboids){
float neighbordist = 500;
PVector sum = new PVector(0,0);
int count = 0;
for (Boid other : boids){
float d = PVector.dist(location,other.location);
if((d>0)&&(d<neighbordist)){
sum.add(other.location);
count++;
}
}
if(count>0){
sum.div(count);
return seek(sum);
}else{
return new PVector(0,0);
}
}
}
class Flock {
ArrayListboids;
Flock(){
boids = new ArrayList();
}
void run(){
for (Boid b:boids){
b.run(boids);
}
}
void addBoid(Boid b){
boids.add(b);
}
}

Define a new PVector that is the direction and magnitude of the wind you desire. Or write a function that returns a PVector based on a position. In either case, add that velocity to the Boid when its position is updated.

If you want the colors of the boids to depend on their movement, you’ll have to specify what sort of a mapping from movement to color you want. Then just draw each boid in a color based on its velocity.

The sort of lines of code you’ll need to add will look like:

PVector wind = new PVector( ???, ???, ???);

velocity.add(wind);

fill(get_color(velocity));

color get_color(PVector v){
    return color(???, ???, ???);
}
3 Likes