Last active
December 27, 2017 20:25
-
-
Save Siunami/5e863c7bb2f7a97666d135498a9ba98e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.Calendar; | |
class Node extends PVector { | |
PVector velocity = new PVector(); | |
float minX=5, minY=5, maxX=width-5,maxY=height-5; | |
float damping = 0.000001; | |
int r,g,b; | |
int trailLength = 5; | |
Node (float theX, float theY){ | |
x = theX; | |
y = theY; | |
r = round(random(0,255)); | |
g = round(random(0,255)); | |
b = round(random(0,255)); | |
} | |
void display(){ | |
for (int i = 0; i < trailLength; i++){ | |
fill(r,g,b,0 + 30*i); | |
ellipse(x + 2*i*velocity.x, y + 2*i*velocity.y, 10, 10); | |
} | |
} | |
void update(){ | |
x += velocity.x; | |
y += velocity.y; | |
if (x < minX) { | |
x = minX - (x - minX); | |
velocity.x = -velocity.x; | |
} | |
if (x > maxX) { | |
x = maxX - (x - maxX); | |
velocity.x = -velocity.x; | |
} | |
if (y < minY) { | |
y = minY - (y - minY); | |
velocity.y = -velocity.y; | |
} | |
if (y > maxY) { | |
y = maxY - (y - maxY); | |
velocity.y = -velocity.y; | |
} | |
//velocity.x *= (1 - damping); | |
//velocity.y *= (1 - damping); | |
} | |
} | |
int nodeCount = 100; | |
Node[] myNodes = new Node[nodeCount]; | |
// image output | |
boolean saveOneFrame = false; | |
void setup() { | |
// other stuff here | |
noStroke(); | |
size(600,600); | |
background(255,255,255); | |
for (int i = 0; i < nodeCount; i++){ | |
myNodes[i] = new Node(random(width), random(height)); | |
myNodes[i].velocity.x = random(-3,3); | |
myNodes[i].velocity.y = random(-3,3); | |
//myNodes[i].damping = 0.01; | |
} | |
} | |
void draw(){ | |
background(255,255,255); | |
for (int i = 0; i < nodeCount; i++){ | |
myNodes[i].update(); | |
myNodes[i].display(); | |
} | |
if (saveOneFrame == true) { | |
saveFrame("ParticleSimulation"+timestamp()+".png"); | |
saveOneFrame = false; | |
} | |
} | |
void keyPressed() { | |
if (key=='s' || key=='S') { | |
saveOneFrame = true; | |
} | |
} | |
String timestamp() { | |
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", Calendar.getInstance()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment