我是Processing 和p5.js 的新手,我尝试将此代码从Processing 转换为p5,但没有成功。我遇到的主要问题是第 21 和 26 行的 ArrayList,以及 ParticleSystem 类中的函数。注意:我知道这可能是一个非常菜鸟的问题,但是我已经尝试了很多方法,但似乎没有任何效果,因此我请求你们的帮助。这是工作处理代码:ParticleSystem ps;void setup() { size(1200, 800); ps = new ParticleSystem(new PVector(width/2, 50)); for (int i=0; i<1200; i++) { ps.addParticle(); }}void draw() { background(255); ps.move_away_from(mouseX, mouseY); ps.run();}class ParticleSystem { ArrayList<Particle> particles; PVector origin; ParticleSystem(PVector position) { origin = position.copy(); particles = new ArrayList<Particle>(); } void addParticle() { particles.add(new Particle(origin)); } void run() { for (int i = particles.size()-1; i >= 0; i--) { Particle p = particles.get(i); p.run(); // if (p.isDead()) { // particles.remove(i); // } } } void move_away_from( float x, float y){ for(Particle p : particles){ float d = dist(x,y,p.position.x, p.position.y); if( d < 200 ){ p.velocity.x += map(d,0,200,0.5,0.1)*(p.position.x - x); p.velocity.y += map(d,0,200,0.5,0.1)*(p.position.y - y); } } }}class Particle { PVector position; PVector velocity; PVector acceleration; PVector home; Particle(PVector l) { acceleration = new PVector(0, 0); velocity = new PVector(0,0);//random(-0.0001, 0.00001), random(-0.001, 0.0001)); l=new PVector(random(0, 1200), random(0, 800)); position = l.copy(); home = position.copy(); } void run() { update(); display(); } // Method to update position void update() { acceleration.x = -0.01*(position.x - home.x); acceleration.y = -0.01*(position.y - home.y); velocity.add(acceleration); velocity.mult(0.9); position.add(velocity); }因此,如果有人找到解决方案或可以告诉我需要采取的步骤,请告诉我!
添加回答
举报
0/150
提交
取消