为了账号安全,请及时绑定邮箱和手机立即绑定

需要帮助将我的处理代码转换为 p5.js(ArrayList +其他!)

需要帮助将我的处理代码转换为 p5.js(ArrayList +其他!)

陪伴而非守候 2023-12-13 15:09:01
我是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 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信