工作生活

processing之粒子系统

2019-07-01  本文已影响0人  147d858e3063

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 =

上一篇下一篇

猜你喜欢

热点阅读