processing爱心粒子的代码
时间: 2023-12-01 12:43:25 浏览: 190
以下是processing实现爱心粒子效果的代码:
```processing
ArrayList<Particle> particles = new ArrayList<Particle>();
float pulsateMult = 0;
void setup() {
size(600, 600, P3D);
for (int i = 0; i < 100; i++) {
particles.add(new Particle());
}
}
void draw() {
background(0);
translate(width/2, height/2);
for (Particle particle : particles) {
particle.update();
particle.display();
}
}
class Particle {
PVector pos;
PVector vel;
float zDepth;
float size;
Particle() {
pos = new PVector(random(-width/2, width/2), random(-height/2, height/2), random(-width/2, width/2));
vel = new PVector(0, 0, 0);
zDepth = map(pos.z, -width/2, width/2, 1, 0);
size = map(zDepth, 0, 1, 10, 2);
}
void update() {
PVector dir = new PVector(-width / 2, -height / 2, 0);
dir.add(pos);
dir.normalize();
dir.mult(pulsateMult * zDepth * 0.05 + cos(frameCount * 0.05) * 1.5);
pos.add(dir);
}
void display() {
pushMatrix();
translate(pos.x, pos.y, pos.z);
noStroke();
fill(255, 0, 0);
ellipse(0, 0, size, size);
popMatrix();
}
}
```
阅读全文