美文网首页工作生活
processing之粒子系统

processing之粒子系统

作者: 147d858e3063 | 来源:发表于2019-07-01 14:01 被阅读0次

    import java.util.*;

    ParticleSystem ps;

    Repeller repeller;

    void setup() {

      size(640,360);

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

      repeller = new Repeller(width/2-20,height/2);

    }

    void draw() {

      background(100);

      ps.addParticle();

      PVector gravity = new PVector(0,0.1);

      ps.applyForce(gravity);

      ps.applyRepeller(repeller);

      ps.run();

      repeller.display();

    }

    class ParticleSystem {

      ArrayList<Particle> particles;

      PVector origin;

      ParticleSystem(PVector location) {

        origin = location.copy();

        particles = new ArrayList<Particle>();

      }

      void addParticle() {

        particles.add(new Particle(origin));

      }

      void applyForce(PVector f) {

        for (Particle p: particles) {

          p.applyForce(f);

        }

      }

      void applyRepeller(Repeller r) {

        for (Particle p: particles) {

          PVector force = r.repel(p);

          p.applyForce(force);

        }

      }

      void run() {

        Iterator<Particle> it = particles.iterator();

        while (it.hasNext()) {

          Particle p =

    相关文章

      网友评论

        本文标题:processing之粒子系统

        本文链接:https://www.haomeiwen.com/subject/ruegcctx.html