Last active
February 26, 2016 07:37
-
-
Save tado/0f281c69e00a967f60d9 to your computer and use it in GitHub Desktop.
3D Particle for Processing
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
int NUM = 2000; | |
ParticleVec3[] particles = new ParticleVec3[NUM]; | |
void setup() { | |
size(1280, 720, P3D); | |
frameRate(60); | |
for (int i = 0; i < NUM; i++) { | |
particles[i] = new ParticleVec3(); | |
particles[i].radius = 4.0; | |
particles[i].position.set(random(width), random(height), random(height)); | |
} | |
} | |
void draw() { | |
background(0); | |
noStroke(); | |
fill(255); | |
for (int i = 0; i < NUM; i++) { | |
particles[i].update(); | |
particles[i].draw(); | |
particles[i].bounceOffWalls(); | |
if (mousePressed) { | |
PVector mouseVec = new PVector(mouseX, mouseY, 0); | |
particles[i].addAttractionForce(mouseVec, width, 1.0); | |
} | |
} | |
} | |
class ParticleVec3 { | |
PVector position; | |
PVector velocity; | |
PVector acceleration; | |
PVector min; | |
PVector max; | |
float friction; | |
float radius; | |
float mass; | |
ParticleVec3() { | |
radius = 4.0; | |
friction = 0.01; | |
mass = 1.0; | |
position = new PVector(width/2.0, height/2.0, 0); | |
velocity = new PVector(0, 0, 0); | |
acceleration = new PVector(0, 0, 0); | |
min = new PVector(0, 0, 0); | |
max = new PVector(width, height, height); | |
} | |
void update() { | |
velocity.add(acceleration); | |
velocity.mult(1.0 - friction); | |
position.add(velocity); | |
acceleration.set(0, 0, 0); | |
} | |
void draw() { | |
pushMatrix(); | |
translate(position.x, position.y, position.z); | |
ellipse(0, 0, radius * 2, radius * 2); | |
popMatrix(); | |
} | |
void addForce(PVector force) { | |
force.div(mass); | |
acceleration.add(force); | |
} | |
void bounceOffWalls() { | |
if (position.x > max.x) { | |
position.x = max.x; | |
velocity.x *= -1; | |
} | |
if (position.x < min.x) { | |
position.x = min.x; | |
velocity.x *= -1; | |
} | |
if (position.y > max.y) { | |
position.y = max.y; | |
velocity.y *= -1; | |
} | |
if (position.y < min.y) { | |
position.y = min.y; | |
velocity.y *= -1; | |
} | |
if (position.z > max.z) { | |
position.z = max.z; | |
velocity.z *= -1; | |
} | |
if (position.z < min.z) { | |
position.z = min.z; | |
velocity.z *= -1; | |
} | |
} | |
void throughOffWalls() { | |
if (position.x < min.x) { | |
position.x = max.x; | |
} | |
if (position.y < min.y) { | |
position.y = max.y; | |
} | |
if (position.z < min.z) { | |
position.z = max.z; | |
} | |
if (position.x > max.x) { | |
position.x = min.x; | |
} | |
if (position.y > max.y) { | |
position.y = min.y; | |
} | |
if (position.z > max.z) { | |
position.z = min.z; | |
} | |
} | |
void addAttractionForce(PVector force, float radius, float scale) { | |
float length = PVector.dist(position, force); | |
PVector diff = new PVector(); | |
diff = position.get(); | |
diff.sub(force); | |
boolean bAmCloseEnough = true; | |
if (radius > 0) { | |
if (length > radius) { | |
bAmCloseEnough = false; | |
} | |
} | |
if (bAmCloseEnough == true) { | |
float pct = 1 - (length / radius); | |
diff.normalize(); | |
diff.mult(scale); | |
diff.mult(pct); | |
acceleration.sub(diff); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment