[ Video ]
[ About ]
Processing部という勉強会グループで作成した課題
Particle System.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
ParticleSystem particleSystemR; ParticleSystem particleSystemG; ParticleSystem particleSystemB; void setup() { size(500, 500); particleSystemR = new ParticleSystem(new PVector(50, 50), new PVector(150, 150), color(255, 0, 0)); particleSystemG = new ParticleSystem(new PVector(0, 250), new PVector(250, 250), color(0, 255, 0)); particleSystemB = new ParticleSystem(new PVector(250, 0), new PVector(250, 500), color(0, 128, 255)); } void draw() { background(39); particleSystemR.run(); particleSystemG.run(); particleSystemB.run(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Particle { PVector position; PVector velocity; color myColor; Particle(PVector position, color myColor) { this.position = position; this.velocity = new PVector(random(-1, 1), random(-1, 1)); this.velocity = this.velocity.normalize().mult(random(3, 5)); this.myColor = myColor; } void update(PVector origin, PVector size) { if(this.position.x < origin.x) { this.velocity.x *= -1; } if(this.position.x > origin.x + size.x) { this.velocity.x *= -1; } if(this.position.y < origin.y) { this.velocity.y *= -1; } if(this.position.y > origin.y + size.y) { this.velocity.y *= -1; } this.position.add(this.velocity); } void display() { stroke(239); fill(this.myColor); ellipse(this.position.x, this.position.y, 15, 15); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class ParticleSystem { ArrayList<Particle> particles; PVector position; PVector size; ParticleSystem(PVector position, PVector size, color particleColor) { this.position = position; this.size = size; this.particles = new ArrayList<Particle>(); for(int i = 0; i < 15; i++) { PVector particlePosition = new PVector(random(position.x, position.x + size.x), random(position.y, position.y + size.y)); Particle particle = new Particle(particlePosition, particleColor); this.particles.add(particle); } } void run() { for(Particle particle : this.particles) { particle.update(this.position, this.size); particle.display(); } stroke(255); noFill(); rect(this.position.x, this.position.y, this.size.x, this.size.y); } } |
[ Link ]