mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Merge pull request #1646 from ybakos/example_ParticleSystem
Example particle system
This commit is contained in:
@@ -12,7 +12,6 @@ class CrazyParticle extends Particle {
|
||||
super(l);
|
||||
// One more line of code to deal with the new variable, theta
|
||||
theta = 0.0;
|
||||
|
||||
}
|
||||
|
||||
// Notice we don't have the method run() here; it is inherited from Particle
|
||||
@@ -21,27 +20,21 @@ class CrazyParticle extends Particle {
|
||||
void update() {
|
||||
super.update();
|
||||
// Increment rotation based on horizontal velocity
|
||||
float theta_vel = (vel.x * vel.mag()) / 10.0f;
|
||||
float theta_vel = (velocity.x * velocity.mag()) / 10.0f;
|
||||
theta += theta_vel;
|
||||
}
|
||||
|
||||
// Override timer
|
||||
void timer() {
|
||||
timer -= 0.5;
|
||||
}
|
||||
|
||||
// Method to display
|
||||
void render() {
|
||||
// This display() method overrides the parent class display() method
|
||||
void display() {
|
||||
// Render the ellipse just like in a regular particle
|
||||
super.render();
|
||||
|
||||
super.display();
|
||||
// Then add a rotating line
|
||||
pushMatrix();
|
||||
translate(loc.x,loc.y);
|
||||
translate(location.x,location.y);
|
||||
rotate(theta);
|
||||
stroke(255,timer);
|
||||
stroke(255,lifespan);
|
||||
line(0,0,25,0);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**
|
||||
* Multiple Particle Systems
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Click the mouse to generate a burst of particles
|
||||
* at mouse location.
|
||||
*
|
||||
* at mouse location.
|
||||
*
|
||||
* Each burst is one instance of a particle system
|
||||
* with Particles and CrazyParticles (a subclass of Particle)
|
||||
* Note use of Inheritance and Polymorphism here.
|
||||
* Note use of Inheritance and Polymorphism here.
|
||||
*/
|
||||
|
||||
ArrayList<ParticleSystem> systems;
|
||||
@@ -24,7 +24,6 @@ void draw() {
|
||||
ps.run();
|
||||
ps.addParticle();
|
||||
}
|
||||
|
||||
if (systems.isEmpty()) {
|
||||
fill(255);
|
||||
textAlign(CENTER);
|
||||
@@ -35,13 +34,3 @@ void draw() {
|
||||
void mousePressed() {
|
||||
systems.add(new ParticleSystem(1, new PVector(mouseX, mouseY)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -31,14 +31,10 @@ class Particle {
|
||||
fill(255,lifespan);
|
||||
ellipse(location.x,location.y,8,8);
|
||||
}
|
||||
|
||||
|
||||
// Is the particle still useful?
|
||||
boolean isDead() {
|
||||
if (lifespan < 0.0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return (lifespan < 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// An ArrayList is used to manage the list of Particles
|
||||
// An ArrayList is used to manage the list of Particles
|
||||
// An Iterator is used to remove "dead" particles while iterating over the list
|
||||
import java.util.Iterator;
|
||||
|
||||
class ParticleSystem {
|
||||
|
||||
@@ -14,7 +16,7 @@ class ParticleSystem {
|
||||
}
|
||||
|
||||
void run() {
|
||||
// Cycle through the ArrayList using Iterator we are deleting
|
||||
// Cycle through the ArrayList using Iterator, because we are deleting while iterating
|
||||
Iterator<Particle> it = particles.iterator();
|
||||
while (it.hasNext()) {
|
||||
Particle p = it.next();
|
||||
@@ -26,7 +28,14 @@ class ParticleSystem {
|
||||
}
|
||||
|
||||
void addParticle() {
|
||||
particles.add(new Particle(origin));
|
||||
Particle p;
|
||||
// Add either a Particle or CrazyParticle to the system
|
||||
if (int(random(0, 2)) == 0) {
|
||||
p = new Particle(origin);
|
||||
} else {
|
||||
p = new CrazyParticle(origin);
|
||||
}
|
||||
particles.add(p);
|
||||
}
|
||||
|
||||
void addParticle(Particle p) {
|
||||
@@ -35,11 +44,7 @@ class ParticleSystem {
|
||||
|
||||
// A method to test if the particle system still has particles
|
||||
boolean dead() {
|
||||
if (particles.isEmpty()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return particles.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user