Processing与粒子系统的实现

ParticleSystem ps;

PShape s;

float angle=0;

float aVelocity=0;

float aAcceleration=0.001;

void setup() {

  size(240, 860);

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

}

void draw() {

  background(255);

  ps.addParticle();

  ps.run();

}

void mousePressed(){

  ps=(new ParticleSystem(new PVector(mouseX,mouseY)));

}

class Particle {

  PVector location;

  PVector velocity;

  PVector acceleration;

  float lifespan;

  Particle(PVector l) {

    // The acceleration

    acceleration = new PVector(0, 0.05);

    // circel's x and y ==> range

    velocity = new PVector(random(-1, 1), random(-14, -1));

    // apawn's position

    location = l.copy();

    // the circle life time

    lifespan = 255.0;

  }

  void run() {

    update();

    display();

  }

  void update() {

    velocity.add(acceleration);

    location.add(velocity);


  }

  boolean isDead() {

    if (lifespan <= 0) {

      return true;

    } else {

      return false;

    }

  }

  void display() {

    // border

    stroke(0, lifespan);

    // border's weight

    strokeWeight(1);

    float r = random(0,255);

    float g = random(0,255);

    float b = random(0,255);

    // random the circle's color

    fill(r,g,b, lifespan);

    rectMode(CENTER);

    pushMatrix();

  // translate(0,0);

    rotate(angle);


    float k=random(1,10);

    line(location.x-k,location.y,location.x+k,location.y);

    ellipse(location.x+k,location.y,8,8);

    ellipse(location.x-k,location.y,8,8);   

    line(location.x,location.y-k,location.x,location.y+k);

    ellipse(location.x,location.y+k,8,8);

    ellipse(location.x,location.y-k,8,8);


    s=createShape();

  s.beginShape();

  s.vertex(location.x+50/2,location.y+18/2);

  s.vertex(location.x+61/2,location.y+37/2);

  s.vertex(location.x+83/2,location.y+43/2);

  s.vertex(location.x+69/2,location.y+60/2);

  s.vertex(location.x+71/2,location.y+82/2);

  s.vertex(location.x+50/2,location.y+73/2);

  s.vertex(location.x+29/2,location.y+82/2);

  s.vertex(location.x+31/2,location.y+60/2);

  s.vertex(location.x+17/2,location.y+43/2);

  s.vertex(location.x+39/2,location.y+37/2);

  s.endShape();

    shape(s);

    popMatrix();



    // draw circle

    //float k=10;

  // ellipse(location.x, location.y, k, k);

  }

}

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

      }

    }

  }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,261评论 0 2
  • 大军西去,人去楼空。 独有茉莉香飘万里, 又有芳草青青杨柳依依, 唯有鸿雁鸣啾啾。 军从万里归, 庶民欢跃,远闻暴...
    陈旭_vivi阅读 1,796评论 3 3
  • 昏昏沉沉的一天又过去,身体一天比一天疲惫,自己最近有点太着急了。赶紧调整! 今天去维护资源的时候,发现有的顾客竟然...
    燕子_e711阅读 1,382评论 0 1
  • 迷雾渐失来路,不知青鸟归处。桥边倚栏人,望断西沉日暮。何故?何故?心头相思长驻。
    我是CC阅读 1,594评论 0 2

友情链接更多精彩内容