Processing 无法理解的加工行为
我只是无法理解这个微不足道的脚本在处理过程中的行为 对于大小为1的粒子系统,它是有效的。一旦大小超过1,粒子就会变得疯狂。为什么? 要运行的草图:Processing 无法理解的加工行为,processing,Processing,我只是无法理解这个微不足道的脚本在处理过程中的行为 对于大小为1的粒子系统,它是有效的。一旦大小超过1,粒子就会变得疯狂。为什么? 要运行的草图: float dt = 1; ParticleSystem ps; void setup(){ size(700,700); // PARTICLE SYSTEM PVector origin = new PVector(width/2, height/2); ps = new ParticleSystem(12, origin);
float dt = 1;
ParticleSystem ps;
void setup(){
size(700,700);
// PARTICLE SYSTEM
PVector origin = new PVector(width/2, height/2);
ps = new ParticleSystem(12, origin); // Change the number here to see the weird behavior !
}
void draw(){
background(255);
// PARTICLE SYSTEM
ps.run();
}
粒子类别:
class Particle {
private PVector pos;
private PVector prevPos;
private PVector vel;
private PVector force;
private float m = 10;
private float r = 60;
private boolean dead = false;
Particle(PVector pos, PVector vel) {
this.prevPos = pos;
this.vel = vel;
this.force = new PVector(0, 0);
this.pos = new PVector();
this.pos.x = pos.x + vel.x * dt + 0.5 * force.x / m * sq(dt);
this.pos.y = pos.y + vel.y * dt + 0.5 * force.y / m * sq(dt);
}
void display() {
color c = color(0);
fill(c);
ellipse(this.pos.x, this.pos.y, this.r * 2, this.r * 2);
}
void run() {
this.update();
this.display();
}
void update() {
this.moveVerlet();
}
void moveVerlet() {
PVector tempPos = new PVector(this.pos.x, this.pos.y);
this.pos.x = this.pos.x * 2 - this.prevPos.x + sq(dt) * this.force.x / this.m;
this.pos.y = this.pos.y * 2 - this.prevPos.y + sq(dt) * this.force.y / this.m;
this.prevPos.set(tempPos);
}
}
粒子系统类别:
class ParticleSystem {
private ArrayList<Particle> particles;
private PVector origin;
ParticleSystem(int nb, PVector origin) {
this.origin = origin;
this.particles = new ArrayList<Particle>(nb);
for (int i = 0 ; i < nb ; i++) {
float k = 0.5;
float vx = random(-k, k);
float vy = random(-k, k);
this.particles.add(new Particle(origin, new PVector(vx, vy)));
}
}
void checkBoundaries() {
for (int i = this.particles.size() - 1 ; i >= 0 ; i--) {
if (this.particles.get(i).pos.x - this.particles.get(i).r <= 0
|| this.particles.get(i).pos.x + this.particles.get(i).r >= width) {
this.particles.get(i).prevPos.x = this.particles.get(i).pos.x + this.particles.get(i).pos.x
- this.particles.get(i).prevPos.x;
}
else if (this.particles.get(i).pos.y - this.particles.get(i).r <= 0
|| this.particles.get(i).pos.y + this.particles.get(i).r >= height) {
this.particles.get(i).prevPos.y = this.particles.get(i).pos.y + this.particles.get(i).pos.y
- this.particles.get(i).prevPos.y;
}
}
}
void run() {
checkBoundaries();
for (Particle p : this.particles) {
p.run();
}
}
}
类分词系统{
私有阵列列表粒子;
私人PVector来源;
粒子系统(int nb,PVector原点){
this.origin=origin;
this.particles=新阵列列表(nb);
对于(int i=0;i=0;i--){
if(this.particles.get(i).pos.x-this.particles.get(i).r=宽度){
this.particles.get(i).prevPos.x=this.particles.get(i).pos.x+this.particles.get(i).pos.x
-这个.particles.get(i).prevPos.x;
}
else if(this.particles.get(i).pos.y-this.particles.get(i).r=高度){
this.particles.get(i).prevPos.y=this.particles.get(i).pos.y+this.particles.get(i).pos.y
-此.particles.get(i).prevPos.y;
}
}
}
无效运行(){
检查边界();
对于(粒子p:this.particles){
p、 run();
}
}
}
请注意,您将原点
传递到ParticleSystem
构造函数中。然后将其传递到Particle
构造函数中,Particle
类将其存储在prevPos
变量中,该变量用于更新每个Particle
的位置
因此,有多个Particle
实例共享相同的prevPos
变量。哦
问题是,粒子
类也修改了prevPos
变量。因此,现在有了多个Particle
实例,它们都在修改相同的prevPos
变量,然后使用这些变量更新位置,并开始累积错误
解决方案是只复制原点
PVector
,然后将其传递给每个粒子
构造函数。幸运的是,PVector
有一个copy()
函数,它可以准确地执行以下操作:
this.particles.add(new Particle(origin.copy(), new PVector(vx, vy)));
更多信息可在中找到。请缩小您的问题范围。我不知道问题来自何方。只是它必须在粒子类中,我猜在moveVerlet()方法中。。。