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.Matrix3f; 11 import java.io.IOException; 12 13 /** 14 * This influencer calculates initial velocity with the use of the emitter's shape. 15 * @author Marcin Roguski (Kaelthas) 16 */ 17 public class NewtonianParticleInfluencer extends DefaultParticleInfluencer { 18 19 /** Normal to emitter's shape factor. */ 20 protected float normalVelocity; 21 /** Emitter's surface tangent factor. */ 22 protected float surfaceTangentFactor; 23 /** Emitters tangent rotation factor. */ 24 protected float surfaceTangentRotation; 25 26 /** 27 * Constructor. Sets velocity variation to 0.0f. 28 */ 29 public NewtonianParticleInfluencer() { 30 this.velocityVariation = 0.0f; 31 } 32 33 @Override 34 public void influenceParticle(Particle particle, EmitterShape emitterShape) { 35 emitterShape.getRandomPointAndNormal(particle.position, particle.velocity); 36 // influencing the particle's velocity 37 if (surfaceTangentFactor == 0.0f) { 38 particle.velocity.multLocal(normalVelocity); 39 } else { 40 // calculating surface tangent (velocity contains the 'normal' value) 41 temp.set(particle.velocity.z * surfaceTangentFactor, particle.velocity.y * surfaceTangentFactor, -particle.velocity.x * surfaceTangentFactor); 42 if (surfaceTangentRotation != 0.0f) {// rotating the tangent 43 Matrix3f m = new Matrix3f(); 44 m.fromAngleNormalAxis(FastMath.PI * surfaceTangentRotation, particle.velocity); 45 temp = m.multLocal(temp); 46 } 47 // applying normal factor (this must be done first) 48 particle.velocity.multLocal(normalVelocity); 49 // adding tangent vector 50 particle.velocity.addLocal(temp); 51 } 52 if (velocityVariation != 0.0f) { 53 this.applyVelocityVariation(particle); 54 } 55 } 56 57 /** 58 * This method returns the normal velocity factor. 59 * @return the normal velocity factor 60 */ 61 public float getNormalVelocity() { 62 return normalVelocity; 63 } 64 65 /** 66 * This method sets the normal velocity factor. 67 * @param normalVelocity 68 * the normal velocity factor 69 */ 70 public void setNormalVelocity(float normalVelocity) { 71 this.normalVelocity = normalVelocity; 72 } 73 74 /** 75 * This method sets the surface tangent factor. 76 * @param surfaceTangentFactor 77 * the surface tangent factor 78 */ 79 public void setSurfaceTangentFactor(float surfaceTangentFactor) { 80 this.surfaceTangentFactor = surfaceTangentFactor; 81 } 82 83 /** 84 * This method returns the surface tangent factor. 85 * @return the surface tangent factor 86 */ 87 public float getSurfaceTangentFactor() { 88 return surfaceTangentFactor; 89 } 90 91 /** 92 * This method sets the surface tangent rotation factor. 93 * @param surfaceTangentRotation 94 * the surface tangent rotation factor 95 */ 96 public void setSurfaceTangentRotation(float surfaceTangentRotation) { 97 this.surfaceTangentRotation = surfaceTangentRotation; 98 } 99 100 /** 101 * This method returns the surface tangent rotation factor. 102 * @return the surface tangent rotation factor 103 */ 104 public float getSurfaceTangentRotation() { 105 return surfaceTangentRotation; 106 } 107 108 @Override 109 protected void applyVelocityVariation(Particle particle) { 110 temp.set(FastMath.nextRandomFloat() * velocityVariation, FastMath.nextRandomFloat() * velocityVariation, FastMath.nextRandomFloat() * velocityVariation); 111 particle.velocity.addLocal(temp); 112 } 113 114 @Override 115 public ParticleInfluencer clone() { 116 NewtonianParticleInfluencer result = new NewtonianParticleInfluencer(); 117 result.normalVelocity = normalVelocity; 118 result.startVelocity = startVelocity; 119 result.velocityVariation = velocityVariation; 120 result.surfaceTangentFactor = surfaceTangentFactor; 121 result.surfaceTangentRotation = surfaceTangentRotation; 122 return result; 123 } 124 125 @Override 126 public void write(JmeExporter ex) throws IOException { 127 super.write(ex); 128 OutputCapsule oc = ex.getCapsule(this); 129 oc.write(normalVelocity, "normalVelocity", 0.0f); 130 oc.write(surfaceTangentFactor, "surfaceTangentFactor", 0.0f); 131 oc.write(surfaceTangentRotation, "surfaceTangentRotation", 0.0f); 132 } 133 134 @Override 135 public void read(JmeImporter im) throws IOException { 136 super.read(im); 137 InputCapsule ic = im.getCapsule(this); 138 normalVelocity = ic.readFloat("normalVelocity", 0.0f); 139 surfaceTangentFactor = ic.readFloat("surfaceTangentFactor", 0.0f); 140 surfaceTangentRotation = ic.readFloat("surfaceTangentRotation", 0.0f); 141 } 142 } 143