Last active
February 12, 2016 19:37
-
-
Save helloCaller/4f075f7c74b17a8fe09b to your computer and use it in GitHub Desktop.
Processing sketch, adapted from another sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Original sketch http://openprocessing.org/sketch/288694 | |
I've taken this sketch and changed it to add colour for | |
moving particles, changed particle behaviour to spark more, and moved their positions | |
to form a more 3 dimensional figure. | |
changed lines wrapped in "//--------changed" | |
*/ | |
ArrayList particles = new ArrayList(); // create a new array to house particles | |
float am = 360; //indicates how far around the circle to create particles | |
void setup() {// setup | |
size(640, 640); // size of project | |
noStroke(); //disables outlines | |
for(int i = 0; i < am; i+=10){// for loop that starts at 0 and increases in increments of 10 while it's smaller than am (defined above) | |
// creating and adding particles into arrays with particles.add | |
//Particles objects take parameters of (vector based on location to start(using "radians(i)), sin(radians(i)" to space them out into a circle, vector limit, delay) | |
//-------changed | |
particles.add(new Particle(new PVector(cos(10), sin(radians(i))), new PVector(cos(radians(i))*50, sin(radians(i))*10), i)); | |
particles.add(new Particle(new PVector(cos(radians(i)*99), sin(radians(i)*99)), new PVector(cos(radians(i))*150, sin(radians(i))*15), i)); | |
//----- | |
} | |
} | |
void draw() { //void draw which is like void loop from arduino | |
background(15); //background set to blackish | |
translate(width/2, height/2); //make origin the center of project | |
for (int i = 0; i < particles.size(); i++) { //for loop that increases from 0 in increments of 1 while its smaller than the particle size (which is the amount of particles on display) | |
Particle p = (Particle) particles.get(i); //use the get method to select two particles at a time | |
p.draw(); //draw the particles, layered in twos | |
p.move(); //call function that controls movement | |
p.boundary(); //call function that limits how far particles move out before returning to their original spots | |
} | |
} | |
class Particle { //create a new class to control all behavior | |
// classes allow for the generation of many objects and have their characteristics defined when a new one is created | |
//setting up Pvectors for location, Velocity, Acceleration, Boundary, StartLocation | |
PVector loc, vel, acc, limit, startLocation; | |
float delay; // setup delay as a float (decimals are welcome here) | |
Particle(PVector l, PVector lim, float d) {// here the parameters of the class are defined | |
loc = new PVector(l.x, l.y); //Vectors are the distance between two points | |
limit = lim; // limit = lim...not sure why this needed to be done | |
vel = new PVector();// this is the set up for some clever vector of vector action below | |
acc = new PVector(); // PVector's establish a vector between two points | |
startLocation = loc.get(); //get function to find starting location | |
delay = d; // setting delay to d | |
} | |
//----Defining the functions called above | |
void draw(){ //another draw!! crazy, a draw within a draw. | |
ellipse(loc.x, loc.y, 3, 3); //draws the particles | |
} | |
void move() { //controls the movement of the particles | |
handleRepel(); // call handleRepel function | |
vel.add(acc); // make an array of vectors (this is where I get a bit hazy on what's happening) | |
loc.add(vel); //make a location array with vectors vel | |
acc.mult(0); //this multiplies the vector | |
} | |
void applyForce(PVector f) { | |
//----changed a value here | |
acc.add(PVector.div(f, 0.5));// adds one vector to another to create a limit on the particles bouncing back | |
//--------- | |
} | |
void boundary() { | |
if (compareValue(limit, loc, startLocation)) { //not totally sure what this does | |
loc = limit.get();//this sets the where the particles sit when they bounce back (without this they just float away) | |
//----change, added color white when the particles bounce back | |
fill(255); | |
//---- | |
vel.mult(-0.6); //multiply vector | |
} else{ | |
//---- change | |
fill(255,0,0); // turn particles red if they're bouncing out | |
} | |
//---- | |
} | |
void handleRepel() { | |
if (delay > 0) { //if delay is above 0 | |
delay-=2; //decrease it by two | |
} else { | |
delay = am; // delay starting a new round of particle popping by how big the circle is | |
} | |
if (delay < 10) { // not sure about this if and else | |
PVector f = new PVector(startLocation.x-limit.x, startLocation.y-limit.y); | |
f.normalize(); | |
applyForce(f); | |
} else { | |
PVector f = new PVector(limit.x-startLocation.x, limit.y-startLocation.y); | |
f.normalize(); | |
applyForce(f); | |
} | |
} | |
} | |
boolean compareValue(PVector limit, PVector loc, PVector startLocation) {// check if location has gone behind the limit. | |
boolean turnX = startLocation.x < limit.x; //i believe this all has to do with determening if the particle should go right or left..my changes mean this is less important | |
if (turnX && loc.x > limit.x)return true; | |
if (!turnX && loc.x < limit.x)return true; | |
boolean turnY = startLocation.y < limit.y; | |
if (turnY && loc.y > limit.y)return true; | |
if (!turnY && loc.y < limit.y)return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment