处理:粒子和运动检测的问题

时间:2016-11-29 23:36:32

标签: java processing

我正在尝试创建一种效果,其中粒子系统出现在屏幕上检测到移动的位置。到目前为止,我有一个运动检测和粒子系统工作,但粒子都在同一点产卵,我需要它们从它们产生的地方随机。任何帮助表示赞赏

ps.addParticle(new PVector(argX,argY));   ps.run();

1 个答案:

答案 0 :(得分:0)

我猜你是从Daniel Shiffman's Simple Particle System演示开始的(也可以通过示例>主题>模拟>简单粒子系统处理)。

如果是这种情况,请注意ParticleSystem有一个origin PVector,在添加粒子时使用:

void addParticle() {
    particles.add(new Particle(origin));
  }

如果要更改所有粒子的生成位置,只需更改原点的x,y值即可。

如果要在随机位置生成新粒子,一个简单的选项是为addParticle()方法创建覆盖选项以传递随机x,y位置:

void addParticle(PVector customPosition) {
    particles.add(new Particle(customPosition));
  }

以下是调整的完整示例,因此您可以拖动粒子系统原点或按任意键在随机位置生成新粒子:

ParticleSystem ps;

void setup() {
  size(640, 360);
  ps = new ParticleSystem(new PVector(width/2, 50));
}

void draw() {
  background(0);
  ps.addParticle();
  ps.run();
}

void mouseDragged(){
  ps.origin.set(mouseX,mouseY);
}
void keyPressed(){
  ps.addParticle(new PVector(random(width),random(height)));
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles 

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 addParticle(PVector customPosition) {
    particles.add(new Particle(customPosition));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}


// A simple Particle class

class Particle {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float lifespan;

  Particle(PVector l) {
    acceleration = new PVector(0, 0.05);
    velocity = new PVector(random(-1, 1), random(-2, 0));
    position = l.copy();
    lifespan = 255.0;
  }

  void run() {
    update();
    display();
  }

  // Method to update position
  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    lifespan -= 1.0;
  }

  // Method to display
  void display() {
    stroke(255, lifespan);
    fill(255, lifespan);
    ellipse(position.x, position.y, 8, 8);
  }

  // Is the particle still useful?
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}

有关详细信息,请务必查看Particle Systems Nature of Code chapter

此外还可以查看Chapter 6关于

的优化说明
  

制作不必要的PVector对象。

更新:您可以运行p5.js的演示:

var system;

function setup() {
  createCanvas(720, 400);
  system = new ParticleSystem(createVector(width/2, 50));
}

function draw() {
  background(51);
  system.addParticle();
  system.run();
}
function mouseDragged(){
  system.origin.set(mouseX,mouseY);
}
function keyPressed(){
  system.addParticleAt(createVector(random(width),random(height)));
}

// A simple Particle class
var Particle = function(position) {
  this.acceleration = createVector(0, 0.05);
  this.velocity = createVector(random(-1, 1), random(-1, 0));
  this.position = position.copy();
  this.lifespan = 255.0;
};

Particle.prototype.run = function() {
  this.update();
  this.display();
};

// Method to update position
Particle.prototype.update = function(){
  this.velocity.add(this.acceleration);
  this.position.add(this.velocity);
  this.lifespan -= 2;
};

// Method to display
Particle.prototype.display = function() {
  stroke(200, this.lifespan);
  strokeWeight(2);
  fill(127, this.lifespan);
  ellipse(this.position.x, this.position.y, 12, 12);
};

// Is the particle still useful?
Particle.prototype.isDead = function(){
  if (this.lifespan < 0) {
    return true;
  } else {
    return false;
  }
};

var ParticleSystem = function(position) {
  this.origin = position.copy();
  this.particles = [];
};

ParticleSystem.prototype.addParticle = function() {
  this.particles.push(new Particle(this.origin));
};

ParticleSystem.prototype.addParticleAt = function(position) {
  this.particles.push(new Particle(position));
};

ParticleSystem.prototype.run = function() {
  for (var i = this.particles.length-1; i >= 0; i--) {
    var p = this.particles[i];
    p.run();
    if (p.isDead()) {
      this.particles.splice(i, 1);
    }
  }
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.4/p5.min.js"></script>

Particle System Preview