Objektorientiertes Animieren #02 (wandern) in Processing
Powered by Processing.js
Der Sketch
Gizmo giz[] = new Gizmo[10];
void setup () {
size (550, 240);
background (76);
for (int i=0; i < giz.length; i++) {
float x = random (width);
float y = random (height);
giz[i] = new Gizmo (x, y);
}
}
void draw () {
noStroke ();
fill (76, 2);
rect (0, 0, width, height);
stroke (236);
for (int i=0; i < giz.length; i++) {
giz[i].move ();
point (giz[i].position.x, giz[i].position.y);
}
}class Gizmo {
PVector position;
PVector direction;
float spin = 0.15;
Gizmo (float theX, float theY) {
position = new PVector (theX, theY);
direction = new PVector ();
direction.x = random (-1, 1);
direction.y = random (-1, 1);
}
void move () {
direction.x += random (-spin, spin);
direction.y += random (-spin, spin);
direction.normalize ();
position.add (direction);
if (position.x < 0 || position.x > width) {
direction.x *= -1;
}
if (position.y < 0 || position.y > height) {
direction.y *= -1;
}
}
}