模拟粒子系统,粒子的组成,粒子系统的组成,粒子系统的多样性;
粒子的设计:
class Particle{粒子系统构造:
PVector location; //粒子的位置
PVector velocity; //粒子的速度
PVector acceleration; //粒子的加速度
float mass; //粒子的质量
float lifespan; //粒子的生命周期
float R = random(255); //粒子的颜色
float G = random(255);
float B = random(255);
//粒子的初始化方式,给定位置的初始化,和随机位置的初始化
Particle(){
location = new PVector(random(width), random(height));
velocity = new PVector(random(-1, 1), random(-2, 0));
acceleration = new PVector(0, 0);
mass = 1;
lifespan = 255;
}
Particle(PVector l){
location = l.get();
acceleration = new PVector(0, 0);
velocity = new PVector(random(-1, 1),random(-2, 0));
mass = 1;
lifespan = 255;
}
void applyForce(PVector force){
acceleration.add(PVector.div(force, mass));
}
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
lifespan -= 1.0;
}
void display(){
stroke(R,G,B,lifespan);
fill(R,G,B,lifespan);
ellipse(location.x, location.y, 8, 8);
}
boolean isDead(){
if(lifespan < 0.0){
return true;
}else{
return false;
}
}
void run(){
update();
display();
}
}
class ParticleSystem{继承实现粒子的多样性:
ArrayList<Particle> particles; //由粒子链表组成的粒子系统
PVector origin; //系统的初始位置
float aliveTime;
ParticleSystem(PVector location_){
origin = location_.get();
particles = new ArrayList<Particle>();
aliveTime = 255;
}
void update(){
origin = new PVector(mouseX, mouseY);
}
void addParticle(){ //添加粒子
float Rate = random(1);
if(Rate < 0.5)
particles.add(new Particle(origin));
else
particles.add(new Confetti(origin));
}
void run(){
Iterator<Particle> it = particles.iterator();
while(it.hasNext()){
Particle p = it.next();
p.run();
if(p.isDead()){
it.remove();
}
}
aliveTime -= 1;
}
boolean isDead(){
if(aliveTime <= 0){
return true;
}else{
return false;
}
}
void applyForce(PVector force){ //粒子受到力的作用
for(Particle p: particles){
p.applyForce(force);
}
}
void applyRepeller(Repeller re){ //粒子受到恒定物体的斥力
for(Particle p: particles){
PVector force = re.repel(p);
p.applyForce(force);
}
}
}
class Confetti extends Particle{作为排斥的物质
float R = random(255);
float G = random(255);
float B = random(255);
Confetti(PVector l){
super(l);
}
void display(){
stroke(R, G, B, lifespan);
fill(R,G,B,lifespan);
//ellipse(location.x, location.y, 8, 8);
rect(location.x, location.y, 8, 8);
}
}
class Repeller{ PVector location; float r = 10; float G = 100; Repeller(float x, float y){ location = new PVector(x, y); } void display(){ stroke(0); fill(0,0); ellipse(location.x, location.y, r*2, r*2); } PVector repel(Particle p){ PVector dir = PVector.sub(location, p.location); float d = dir.mag(); d = constrain(d, 5, 100); dir.normalize(); float force = -1*G/(d*d); dir.mult(force); return dir; }}主窗口函数:
//粒子的随机运行
/*int total = 10;
Particale[] parray = new Particale[total];
void setup(){
size(600, 600);
background(255);
smooth();
for(int i = 0; i < parray.length; i++){
parray[i] = new Particale();
}
}
void draw(){
for(int i = 0; i < parray.length; i++){
parray[i].run();
}
}*/
//组成链表的粒子的自然运行和消亡
/*import java.util.*;
int total = 10;
ArrayList<Particale> plist = new ArrayList<Particale>();
void setup(){
size(600, 600);
smooth();
background(255);
frameRate(180);
}
void draw(){
background(255);
plist.add(new Particale(new PVector(random(width), random(height/2))));
Iterator<Particale> it = plist.iterator();
while(it.hasNext()){
Particale p = it.next();
p.run();
if(p.isDead()){
it.remove();
}
}
println("plist.size:",plist.size());
}*/
//点击鼠标触发,单个粒子系统的运行
/*import java.util.*;
ParticleSystem ps;
void mousePressed(){
ps.update();
}
void setup(){
size(600, 600);
smooth();
background(255);
ps = new ParticleSystem(new PVector(width/2, height/2));
}
void draw(){1
background(255);
ps.run();
ps.addParticle();
}*/
//多个粒子系统的在斥力的影响下自然消亡
import java.util.*;
ArrayList<ParticleSystem> systems;
PVector gravity;
Repeller repeller;
void mousePressed(){
systems.add(new ParticleSystem(new PVector(mouseX, mouseY)));
//gravity = new PVector(0, 0.1);
}
void setup(){
size(640, 240);
systems = new ArrayList<ParticleSystem>();
repeller = new Repeller(width/2-20, height/2);
}
void draw(){
background(255);
Iterator<ParticleSystem> it = systems.iterator();
while(it.hasNext()){
ParticleSystem p = it.next();
gravity = new PVector(random(-0.1, 0.1), random(-0.1, 0.2));
p.applyRepeller(repeller);
p.applyForce(gravity);
p.run();
p.addParticle();
if(p.isDead()){
it.remove();
}
}
repeller.display();
}