the previous week: particle system
Exercise :
modify the sample code to simulate "wind" vector
sample:
ParticleSystem ps;
ParticleSystem ps2;
Wind w;
void setup() {
size(640, 360);
ps = new ParticleSystem(new PVector(width/2, 50));
ps2 = new ParticleSystem(new PVector(width/3, 200));
w = new Wind();
}
void draw() {
background(0);
ps.addParticle();
ps.run();
ps2.addParticle();
ps2.run();
w.update();
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
// A simple Particle class
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
if (!w.isDead()) velocity.add(w.acceleration);
position.add(velocity);
lifespan -= 1.0;
}
// Method to display
void display() {
stroke(255, lifespan);
fill(255, lifespan);
ellipse(position.x, position.y, 8, 8);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
class Wind {
PVector acceleration;
float lifespan = 200;
Wind() {
acceleration = new PVector(0.05, 0);
}
void update(){
lifespan -= 1.0;
}
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
additional sample :
ParticleSystem ps;
ParticleSystem ps2;
Wind w;
void setup() {
size(640, 360);
ps = new ParticleSystem(new PVector(width/2, 50));
ps2 = new ParticleSystem(new PVector(width/3, 200));
w = new Wind();
}
void draw() {
background(0);
ps.addParticle();
ps.run();
ps2.addParticle();
ps2.run();
w.update();
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
particles = new ArrayList<Particle>();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
// A simple Particle class
class Particle {
PVector position;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
position = l.copy();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
if (!w.isDead()) velocity.add(w.acceleration);
position.add(velocity);
lifespan -= 1.0;
}
// Method to display
void display() {
stroke(255, lifespan);
fill(255, lifespan);
ellipse(position.x, position.y, 8, 8);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
class Wind {
PVector acceleration;
float lifespan = 200;
Wind() {
acceleration = new PVector(0.05, 0);
}
void update(){
lifespan -= 1.0;
}
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
----------------------------------------------------------
https://www.processing.org/reference/PImage.html
https://processing.org/tutorials/pixels/
Exercise:
Create a particle system with an image's color
沒有留言:
張貼留言