Created
November 8, 2013 15:11
-
-
Save dkgrieshammer/7372357 to your computer and use it in GitHub Desktop.
Processing: Example for Gravity
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
Node[] myNode = new Node[20]; | |
void setup(){ | |
size(1024, 600); | |
for (int i = 0; i<20; i++){ | |
myNode[i] = new Node(random(10, 20)); | |
} | |
// myNode = new Node(20); | |
} | |
void draw(){ | |
background(255); | |
for (int i = 0; i<20; i++){ | |
myNode[i].move(); | |
} | |
} |
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
class Node { | |
PVector location; | |
PVector velocity; | |
PVector acceleration; | |
PVector gravity; | |
float mass = 10.0; // | |
float myHeight, myWidth = 10; | |
float noizer = 0; //um Wind zu simulieren | |
Node (float mySize) { | |
myHeight = myWidth = mySize; | |
mass = mySize/2; | |
location = new PVector(random(width), random(height)); | |
// velocity = new PVector(0, 0); | |
velocity = PVector.random2D(); | |
acceleration = new PVector(0, 0); | |
gravity = new PVector(0, 0.3 * mass); | |
} | |
// public function to call from mainsketch / other objects | |
void move() { | |
update(); | |
draw(); | |
} | |
// function to calculate stuff | |
void update() { | |
if(location.x < 0 + myWidth/2 || location.x > width - myWidth/2) { | |
velocity.x *= -0.95; | |
} | |
if(location.y <= 0 + myHeight/2 || location.y >= height - myHeight/2) { | |
velocity.y *= -0.95; | |
} | |
if (mousePressed) { | |
randomWind(); | |
} | |
applyForce(gravity); | |
velocity.add(acceleration); | |
location.add(velocity); //always add velocity! | |
velocity.mult(0.99); //if no friction is added the nodes tend to speed up indefinately | |
acceleration.mult(0); | |
if(location.y > height - myHeight/2) { | |
location.y = height - myHeight/2; | |
} | |
} | |
//function for all drawing stuff | |
void draw() { | |
ellipse(location.x, location.y, myWidth, myHeight); | |
} | |
//function to apply any force | |
void applyForce(PVector force) { | |
PVector f = PVector.div(force, mass); | |
acceleration.add(f); | |
} | |
void randomWind() { | |
PVector rWind = new PVector(1, 1); | |
rWind.rotate(noise(noizer)*360); | |
applyForce(rWind); | |
noizer += 0.01; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment