我不熟悉处理,我希望我的粒子系统跟随我的鼠标。目前,它落后于鼠标(我想很好),但它也绘制了两次。这是我到目前为止的内容:我也有一个我未包括的“星形”类,它绘制了跟随鼠标的简单星形。
ParticleSystem ps;
void setup() {
ps = new ParticleSystem(new PVector(mouseX,mouseY));
size(1000,1000);
frameRate(30);
noStroke();
}
void draw(){
background(30);
ps.followMouse();
ps.addParticle();
ps.run();
}
class ParticleSystem {
ArrayList<Particle> particles = new ArrayList<Particle>();
PVector origin;
ParticleSystem(PVector position) {
origin = position.copy();
}
void addParticle() {
particles.add(new Particle(origin));
}
void run() {
for (int i = particles.size()-1; i >= 0; i--)
particles.get(i).run();
}
void followMouse() {
PVector mouse = new PVector(mouseX, mouseY);
origin = mouse.sub(origin);
}
}
class Particle {
PVector pos, velocity = new PVector(random(-1, 1), random(-2, 0)), acceleration = new PVector(0, 0.05);
Particle(PVector l) {
pos = l.copy();
}
void run() {
update();
display();
}
void update() {
velocity.add(acceleration);
pos.add(velocity);
}
void display() {
noStroke();
fill(random(255),random(255), random(255));
ellipse(pos.x,pos.y, 8, 8);
}
}
将followMouse()
更改为此:
void followMouse() {
origin = new PVector(mouseX, mouseY);
}