Skip to content

Instantly share code, notes, and snippets.

@chbrown
Created May 14, 2011 19:31
Show Gist options
  • Save chbrown/972542 to your computer and use it in GitHub Desktop.
Save chbrown/972542 to your computer and use it in GitHub Desktop.
// Creating an array of objects.
Mover[] movers = new Mover[20];
void setup() {
size(200,200);
smooth();
background(255);
// Initializing all the elements of the array
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover();
}
}
void draw() {
noStroke();
fill(255,10);
rect(0,0,width,height);
// Calling functions of all of the objects in the array.
for (int i = 0; i < movers.length; i++) {
movers[i].update();
movers[i].checkEdges();
movers[i].display();
}
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover() {
location = new PVector(random(width),random(height));
velocity = new PVector(0,0);
topspeed = 4;
}
void update() {
// Our algorithm for calculating acceleration:
PVector mouse = new PVector(mouseX,mouseY);
PVector dir = PVector.sub(mouse,location); // Find vector pointing towards mouse
dir.normalize(); // Normalize
dir.mult(0.5); // Scale
acceleration = dir; // Set to acceleration
// Motion 101! Velocity changes by acceleration. Location changes by velocity.
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}
void display() {
stroke(0);
fill(175);
ellipse(location.x,location.y,16,16);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment