1 package com.jme3.effect.influencers;
2 
3 import com.jme3.effect.Particle;
4 import com.jme3.effect.shapes.EmitterShape;
5 import com.jme3.export.InputCapsule;
6 import com.jme3.export.JmeExporter;
7 import com.jme3.export.JmeImporter;
8 import com.jme3.export.OutputCapsule;
9 import com.jme3.math.FastMath;
10 import com.jme3.math.Vector3f;
11 import java.io.IOException;
12 
13 /**
14  * This emitter influences the particles so that they move all in the same direction.
15  * The direction may vary a little if the velocity variation is non zero.
16  * This influencer is default for the particle emitter.
17  * @author Marcin Roguski (Kaelthas)
18  */
19 public class DefaultParticleInfluencer implements ParticleInfluencer {
20 
21     /** Temporary variable used to help with calculations. */
22     protected transient Vector3f temp = new Vector3f();
23     /** The initial velocity of the particles. */
24     protected Vector3f startVelocity = new Vector3f();
25     /** The velocity's variation of the particles. */
26     protected float velocityVariation = 0.2f;
27 
28     @Override
influenceParticle(Particle particle, EmitterShape emitterShape)29     public void influenceParticle(Particle particle, EmitterShape emitterShape) {
30         emitterShape.getRandomPoint(particle.position);
31         this.applyVelocityVariation(particle);
32     }
33 
34     /**
35      * This method applies the variation to the particle with already set velocity.
36      * @param particle
37      *        the particle to be affected
38      */
applyVelocityVariation(Particle particle)39     protected void applyVelocityVariation(Particle particle) {
40     	particle.velocity.set(startVelocity);
41         temp.set(FastMath.nextRandomFloat(), FastMath.nextRandomFloat(), FastMath.nextRandomFloat());
42         temp.multLocal(2f);
43         temp.subtractLocal(1f, 1f, 1f);
44         temp.multLocal(startVelocity.length());
45         particle.velocity.interpolate(temp, velocityVariation);
46     }
47 
48     @Override
write(JmeExporter ex)49     public void write(JmeExporter ex) throws IOException {
50         OutputCapsule oc = ex.getCapsule(this);
51         oc.write(startVelocity, "startVelocity", Vector3f.ZERO);
52         oc.write(velocityVariation, "variation", 0.2f);
53     }
54 
55     @Override
read(JmeImporter im)56     public void read(JmeImporter im) throws IOException {
57         InputCapsule ic = im.getCapsule(this);
58         startVelocity = (Vector3f) ic.readSavable("startVelocity", Vector3f.ZERO.clone());
59         velocityVariation = ic.readFloat("variation", 0.2f);
60     }
61 
62     @Override
clone()63     public ParticleInfluencer clone() {
64         try {
65             DefaultParticleInfluencer clone = (DefaultParticleInfluencer) super.clone();
66             clone.startVelocity = startVelocity.clone();
67             return clone;
68         } catch (CloneNotSupportedException e) {
69             throw new AssertionError();
70         }
71     }
72 
73     @Override
setInitialVelocity(Vector3f initialVelocity)74     public void setInitialVelocity(Vector3f initialVelocity) {
75         this.startVelocity.set(initialVelocity);
76     }
77 
78     @Override
getInitialVelocity()79     public Vector3f getInitialVelocity() {
80         return startVelocity;
81     }
82 
83     @Override
setVelocityVariation(float variation)84     public void setVelocityVariation(float variation) {
85         this.velocityVariation = variation;
86     }
87 
88     @Override
getVelocityVariation()89     public float getVelocityVariation() {
90         return velocityVariation;
91     }
92 }
93