序言
粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术,而这些现象用其它传统的渲染技术难以实现真实感的物理运动规律。经常使用粒子系统模拟的现象有火、爆炸、烟、水流、火花、落叶、云、雾、雪、尘、流星尾迹或者象发光轨迹这样的抽象视觉效果等等。
在我看来,粒子系统其实就是,粒子受力不同,下面我们开始说说粒子系统的生成,并且与鼠标交互
单个粒子系统
首先,我们可以看出来,这个粒子向下掉的途中也慢慢消失了,因此,我们还需要判断它的生命值。
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(-1, 0));
position = l.get();
lifespan = 255.0;
}
void run()
{
update();
display();
}
// Method to update position
void update() {
velocity.add(acceleration);
position.add(velocity);
lifespan -= 2.0; } // Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(position.x, position.y, 12, 12); }
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0)
{
return true;
}
else {
return false;
}
}
}
以上代码为粒子类,其中的lifespan为粒子的生命周期,当其小于零之后则消失
Particle p;
void setup() {
size(640,360);
p = new Particle(new
PVector(width/2,20));
background(255);
smooth();
}
void draw()
{
background(255);
p.run();
if (p.isDead()) {
p = new Particle(new PVector(width/2,20)); //println("Particle dead!");
}
}
当然我们还需要斥力,加入斥力之后,并且我们加入鼠标的交互,并且还得生成粒子类,从一个粒子变成多个粒子。
代码如下
class ParticleSystem {
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(PVector position) {
origin = position.get();
particles = new ArrayList<Particle>(); }
void addParticle() {
particles.add(new Particle(origin)); }
void addParticle(float x, float y) {
particles.add(new Particle(new
PVector(x, y)));
}
void applyForce(PVector f) {
for (Particle p: particles) {
p.applyForce(f);
}
}
void applyRepeller(Repeller r) {
for (Particle p: particles) {
PVector force = r.repel(p);
p.applyForce(force);
}
}
void run()
{
for (int i =
particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
}
粒子加力
class Repeller {
// Gravitational Constant
float G = 100; // position
PVector position;
Repeller(float x, float y) {
position = new PVector(x,y); }
void display() {
stroke(0);
strokeWeight(0);
}
PVector repel(Particle p) { PVector dir =
PVector.sub(position,p.position); // Calculate direction of force
float d = dir.mag(); // Distance between objects
dir.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector
for direction)
d = constrain(d,5,100); // Keep distance within a reasonable range
float force = -1 * G / (d * d); // Repelling force is inverselyproportional to distance
dir.mult(force); // Get force vector --> magnitude * direction
return dir;
}
}
然后现在需要和鼠标交互
ParticleSystem ps;
Repeller repeller;
void setup() {
size(640,640);
ps = new ParticleSystem(new
PVector(width/2,10));
repeller=new Repeller(mouseX,mouseY); }
void draw() {
background(255);
PVector gravity = new PVector(0.05,0);
ps.applyForce(gravity);
ps.applyRepeller(repeller);
ps.origin.set(mouseX,mouseY,0);
ps.addParticle();
ps.run();
repeller.display();
ps.addParticle(mouseX,mouseY); }
/
粒子系统可以做成很多东西,是很好的模拟自然系统的一种方法,因此我们可以很好地利用粒子系统去模拟烟花、下雨、下雪等景色。
啊啊啊啊啊啊啊啊啊啊,终于写完了!!!!