float interval = 20;
ArrayList<Roter> roters;
void setup(){
size(640, 480);
frameRate(30);
smooth();
rectMode(CENTER);
roters = new ArrayList<Roter>();
for(float y = interval / 2; y <= height - interval / 2; y += interval){
for(float x = interval / 2; x <= width - interval / 2; x+= interval){
PVector pos = new PVector(x, y);
roters.add(new Roter(pos));
}
}
}
void draw(){
background(0);
for(Roter roter: roters){
roter.display();
}
for(Roter roter: roters){
roter.update();
}
}
class Roter{
PVector pos;
float ang;
float angVel;
Roter(PVector pos){
this.pos = pos;
ang = random(360);
angVel = map(random(1), 0, 1, -5, 5);
}
void display(){
pushMatrix();
translate(pos.x, pos.y);
rotate(radians(ang));
noStroke();
fill(map(abs(angVel), 0, 20, 139, 255), map(abs(angVel), 0, 20, 0, 140), map(abs(angVel), 0, 20, 139, 0));
float rectSize = map(abs(angVel), 0, 20, 10, interval * 0.9);
rect(0, 0, rectSize, rectSize);
fill(64);
ellipse(0, 0, 2, 2);
popMatrix();
}
void update(){
angVel *= 0.99;
float distance = sqrt(sq(pos.x - mouseX) + sq(pos.y - mouseY));
float distanceV = map(pow(distance, 0.3), 0, pow(sqrt(sq(width) + sq(height)), 0.3), 1, 0);
float mouseXSpeed = mouseX - pmouseX;
float mouseXSpeedV = constrain(mouseXSpeed, -50, 50);
mouseXSpeedV = map(mouseXSpeed, -50, 50, -1, 1);
float angAcc = map(sqrt(distanceV * abs(mouseXSpeedV)), 0, 1, 0, 3.0);
if(mouseXSpeed > 0){
angVel += angAcc;
} else {
angVel -= angAcc;
}
constrain(angVel, -20, 20);
ang += angVel;
if(ang < 0){
ang += 360;
} else if(ang > 360){
ang -= 360;
}
}
}