处理:为什么我的粒子系统跟随鼠标在不同的位置绘制两次?
Processing: Why is my particle system drawing twice at a different location as it follows the mouse?
我是处理新手,我希望我的粒子系统跟随我的鼠标。目前,它拖在鼠标后面(我认为这很好),但它也绘制了两次。这是我到目前为止所拥有的:
我还有一个 'star' class 我没有包括在内,它绘制了一个跟随鼠标的简单星星。
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);
}
我是处理新手,我希望我的粒子系统跟随我的鼠标。目前,它拖在鼠标后面(我认为这很好),但它也绘制了两次。这是我到目前为止所拥有的: 我还有一个 'star' class 我没有包括在内,它绘制了一个跟随鼠标的简单星星。
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);
}