Skip to content

Instantly share code, notes, and snippets.

# wonderburg7/particle beam.pde Created Sep 16, 2018

 // Creating an array of objects. int oldx; int oldy; Mover[] movers = new Mover; void setup() { size(1000,250); 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 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(); } } 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(40)), 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; } } }
 // Creating an array of objects. int oldx; int oldy; Mover[] movers = new Mover; void setup() { size(1000,250); strokeWeight(2); smooth(8); 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(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(); } } 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(40)), 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; } } }
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.