add flocking functions to this sketch?
in
Programming Questions
•
1 year ago
Hey guys.
The patch below holds five points that follow the mouse, but are held within circular constraints.
What im trying to do is add some flocking functionality to these points; starting with separation. I realise i must first get these points to communicate to each other their vector positions. I really dont know where to start doing this. Can anyone help?
thanks in advance
Rich.
int closestToAudience = 3;
Motor[] motors = {
new Motor(150, 150),
new Motor(350, 150),
new Motor(250, 250),
new Motor(150, 350),
new Motor(350, 350)
};
void setup() {
size(500, 500);
smooth();
strokeWeight(10);
stroke(0, 100);
}
void draw() {
float motorDistanceToAudience = 100000;
background(226);
noFill();
for(int i=0; i<5; i=i+1){
// find which motor is closes to the audience
float c = motors[i].distToAudience();
if(c < motorDistanceToAudience) {
motorDistanceToAudience = c;
closestToAudience = i;
}
motors[i].draw();
}
}
//////second tab -
class Motor {
float x;
float y;
float angle = 0.0;
float segLength = 60;
float speakerX;
float speakerY;
float armlength = 200;
PVector circle;
int radius;
float easing;
Motor(int xArg, int yArg){
x = xArg;
y = yArg;
easing = 0.05;
circle = new PVector(x, y);
radius = 200;
}
void update(){
}
void draw () {
ellipse(circle.x, circle.y, radius, radius);
// println(circle.x);
PVector m = new PVector(mouseX, mouseY);
if (dist(m.x, m.y, circle.x, circle.y) > radius/2) {
m.sub(circle);
m.normalize();
m.mult(radius/2);
m.add(circle);
}
x = x + (m.x - x) * easing;
y = y + (m.y - y) * easing;
//fill(255, 0, 0);
ellipse(x, y, 24, 24);
}
float distToAudience() {
return dist(x, y, mouseX, mouseY);
}
}
1