Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Creating an array of objects.
int oldx;
int oldy;
boolean recording = false;
Mover[] movers = new Mover[400];
void setup() {
size(600,600);
strokeWeight(2);
strokeJoin(ROUND);
smooth(8);
background(255);
// Initializing all the elements of the array
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover();
}
}
void keyPressed() {
if (key == 'r' || key == 'R') {
recording = !recording;
}
}
void draw() {
noStroke();
fill(134, 254, 145, 10);
rect(0,0,width,height);
background(255);
// 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();
}
if (recording) {
saveFrame("Beam/fram_####.png");
}
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
Mover() {
location = new PVector(random(width),random(height));
velocity = new PVector(0,0);
topspeed = 3;
}
void update() {
oldx = int(location.x);
oldy = int(location.y);
// Our algorithm for calculating acceleration:
// PVector mouse = new PVector(width+int(random(10, 40)), random(height));
PVector mouse = new PVector(random(width), random(height));
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,1,1);
if (location.x > width-5 || location.x < 5 || location.y < 5 || location.y > height-5) {
} else {
line(location.x,location.y,oldx,oldy);
}
/*
if (location.x > width-5) {
} else if (location.x < 5) {
}
if (location.y > height+5) {
} else if (location.y < 5) {
} else {
line(location.x,location.y,oldx,oldy);
}*/
}
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
You can’t perform that action at this time.