亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要幫助將我的處理代碼轉換為 p5.js(ArrayList + 其他?。?/h1>

我是 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();    }因此,如果有人有解決方案或可以告訴我我需要采取的步驟,請告訴我!
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

在 JavaScript 中,您根本不需要像 an 之類的東西ArrayList。JavaScript 數組是動態的。此外,沒有必要聲明屬性。當分配了某些東西時,屬性會自動“創建”。


PVector您可以使用 uss代替對象p5.Vector。Ap5.Vector由createVector. 此外,請閱讀JavaScript 中的類。


看例子:


class Particle {

  

    constructor(l) {

        this.acceleration = createVector(0, 0);

        this.velocity = createVector(0,0);//random(-0.0001, 0.00001), random(-0.001, 0.0001));

        this.position = l ? l.copy() : createVector(Math.random()*1200, Math.random()*1200,);

        this.home = this.position.copy();

    }


    run() {

        this.update();

        this.display();

    }


    // Method to update position

    update() {

        this.acceleration.x = -0.01*(this.position.x - this.home.x);

        this.acceleration.y = -0.01*(this.position.y - this.home.y);

        this.velocity.add(this.acceleration);

        this.velocity.mult(0.9);

        this.position.add(this.velocity);

        //   lifespan -= 1.0;

    }


    // Method to display

    display() {

        noStroke();//stroke(255, lifespan);

        //fill(255, lifespan);

        fill(0);

        ellipse(this.position.x, this.position.y, 25, 25);

    }

}


class ParticleSystem {

    constructor(position) {

        this.origin = position.copy();

        this.particles = [];

    }


    addParticle() {

        //this.particles.push(new Particle(this.origin));

        this.particles.push(new Particle());

    }


    run() {

        for (let i = this.particles.length-1; i >= 0; i--) {

            this.particles[i].run();

    //      if (p.isDead()) {

            //    particles.remove(i);

    //      }

        }

    }


    move_away_from(x, y){

        for (let i = 0; i < this.particles.length; i++) {

            let p = this.particles[i];

            let 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);

            }

        }

    }

}


var ps;


function setup() {

    createCanvas(1200, 800);

    ps = new ParticleSystem(createVector(width/2, 50));

    for (var i=0; i<1200; i++) {

        ps.addParticle();

    }

}


function draw() {

    background(255);

    ps.move_away_from(mouseX, mouseY);

    ps.run();

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>


查看完整回答
反對 回復 2021-12-02
  • 1 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

了解更多

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號