2016-11-29 7 views
0

画面上で動きが検出されたパーティクルシステムが表示されるエフェクトを作成しようとしています。これまでは動き検出とパーティクルシステムが動作していましたが、パーティクルはすべて同じ場所にスポーンされていました。どんな助けもありがたいです処理:パーティクルとモーション検出の問題

ps.addParticle(新しいPVector(argX、argY)); ps.run();

+2

は[MCVE]投稿してください問題の代わりに焦点を当ててあなたのスケッチ全体たとえば、最小または動画コード​​を確認する必要はありません。パーティクルシステムにハードコードされた値を使用するだけで、コードをコピーして貼り付けることで問題を確認できます。 –

+0

私はここに私の値が存在しないコードの一部を持って、私はこれが起こっている理由、任意の提案に不明ですか? – JHewitt

答えて

0

デモで始まると思います(の例題>トピック>シミュレーション>単純パーティクルシステム)。あなたはすべての粒子のためのスポーン位置を変更したい場合は

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

あなたは、単に、原点の変更することができます。その場合は

は、ParticleSystemは粒子が追加されるときに使用されているorigin P-VECTORを持っていることに気づきます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

をチェックしてください10

はまた、不要なP-VECTORオブジェクトのgajillionsを作る

Chapter 6の最適化のノートでのぞき見を持っています。

更新:あなたがp5.jsのデモ怒鳴るcurtesy実行することができます。

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