1. Improvement in cooperation with mesh-shaped emitters. 2. Added particle influencers that calculate initial velocity. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7564 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
5a79adc4fe
commit
49116be02d
@ -0,0 +1,89 @@ |
|||||||
|
package com.jme3.effect.influencers; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import com.jme3.effect.EmitterShape; |
||||||
|
import com.jme3.effect.Particle; |
||||||
|
import com.jme3.export.InputCapsule; |
||||||
|
import com.jme3.export.JmeExporter; |
||||||
|
import com.jme3.export.JmeImporter; |
||||||
|
import com.jme3.export.OutputCapsule; |
||||||
|
import com.jme3.math.FastMath; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
|
||||||
|
/** |
||||||
|
* This emitter influences the particles so that they move all in the same direction. |
||||||
|
* The direction may vary a little if the velocity variation is non zero. |
||||||
|
* This influencer is default for the particle emitter. |
||||||
|
* @author Marcin Roguski (Kaelthas) |
||||||
|
*/ |
||||||
|
public class DefaultParticleInfluencer implements ParticleInfluencer { |
||||||
|
/** Temporary variable used to help with calculations. */ |
||||||
|
protected transient Vector3f temp = new Vector3f(); |
||||||
|
/** The initial velocity of the particles. */ |
||||||
|
protected Vector3f startVelocity = new Vector3f(); |
||||||
|
/** The velocity's variation of the particles. */ |
||||||
|
protected float velocityVariation = 0.2f; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void influenceParticle(Particle particle, EmitterShape emitterShape) { |
||||||
|
emitterShape.getRandomPoint(particle.position); |
||||||
|
this.applyVelocityVariation(particle); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method applies the variation to the particle with already set velocity. |
||||||
|
* @param particle |
||||||
|
* the particle to be affected |
||||||
|
*/ |
||||||
|
protected void applyVelocityVariation(Particle particle) { |
||||||
|
temp.set(FastMath.nextRandomFloat(), FastMath.nextRandomFloat(), FastMath.nextRandomFloat()); |
||||||
|
temp.multLocal(2f); |
||||||
|
temp.subtractLocal(1f, 1f, 1f); |
||||||
|
temp.multLocal(startVelocity.length()); |
||||||
|
particle.velocity.interpolate(temp, velocityVariation); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.write(startVelocity, "startVelocity", Vector3f.ZERO); |
||||||
|
oc.write(velocityVariation, "variation", 0.2f); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
InputCapsule ic = im.getCapsule(this); |
||||||
|
startVelocity = (Vector3f) ic.readSavable("startVelocity", Vector3f.ZERO); |
||||||
|
velocityVariation = ic.readFloat("variation", 0.2f); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ParticleInfluencer clone() { |
||||||
|
try { |
||||||
|
return (ParticleInfluencer) super.clone(); |
||||||
|
} catch (CloneNotSupportedException e) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setInitialVelocity(Vector3f initialVelocity) { |
||||||
|
this.startVelocity.set(initialVelocity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Vector3f getInitialVelocity() { |
||||||
|
return startVelocity; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setVelocityVariation(float variation) { |
||||||
|
this.velocityVariation = variation; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public float getVelocityVariation() { |
||||||
|
return velocityVariation; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
package com.jme3.effect.influencers; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import com.jme3.effect.EmitterShape; |
||||||
|
import com.jme3.effect.Particle; |
||||||
|
import com.jme3.export.JmeExporter; |
||||||
|
import com.jme3.export.JmeImporter; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
|
||||||
|
/** |
||||||
|
* This influencer does not influence particle at all. |
||||||
|
* It makes particles not to move. |
||||||
|
* @author Marcin Roguski (Kaelthas) |
||||||
|
*/ |
||||||
|
public class EmptyParticleInfluencer implements ParticleInfluencer { |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException {} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException {} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void influenceParticle(Particle particle, EmitterShape emitterShape) {} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setInitialVelocity(Vector3f initialVelocity) {} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Vector3f getInitialVelocity() { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setVelocityVariation(float variation) {} |
||||||
|
|
||||||
|
@Override |
||||||
|
public float getVelocityVariation() { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ParticleInfluencer clone() { |
||||||
|
try { |
||||||
|
return (ParticleInfluencer) super.clone(); |
||||||
|
} catch (CloneNotSupportedException e) { |
||||||
|
return new EmptyParticleInfluencer(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,142 @@ |
|||||||
|
package com.jme3.effect.influencers; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import com.jme3.effect.EmitterShape; |
||||||
|
import com.jme3.effect.Particle; |
||||||
|
import com.jme3.export.InputCapsule; |
||||||
|
import com.jme3.export.JmeExporter; |
||||||
|
import com.jme3.export.JmeImporter; |
||||||
|
import com.jme3.export.OutputCapsule; |
||||||
|
import com.jme3.math.FastMath; |
||||||
|
import com.jme3.math.Matrix3f; |
||||||
|
|
||||||
|
/** |
||||||
|
* This influencer calculates initial velocity with the use of the emitter's shape. |
||||||
|
* @author Marcin Roguski (Kaelthas) |
||||||
|
*/ |
||||||
|
public class NewtonianParticleInfluencer extends DefaultParticleInfluencer { |
||||||
|
/** Normal to emitter's shape factor. */ |
||||||
|
protected float normalVelocity; |
||||||
|
/** Emitter's surface tangent factor. */ |
||||||
|
protected float surfaceTangentFactor; |
||||||
|
/** Emitters tangent rotation factor. */ |
||||||
|
protected float surfaceTangentRotation; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. Sets velocity variation to 0.0f. |
||||||
|
*/ |
||||||
|
public NewtonianParticleInfluencer() { |
||||||
|
this.velocityVariation = 0.0f; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void influenceParticle(Particle particle, EmitterShape emitterShape) { |
||||||
|
emitterShape.getRandomPointAndNormal(particle.position, particle.velocity); |
||||||
|
// influencing the particle's velocity
|
||||||
|
if (surfaceTangentFactor == 0.0f) { |
||||||
|
particle.velocity.multLocal(normalVelocity); |
||||||
|
} else { |
||||||
|
// calculating surface tangent (velocity contains the 'normal' value)
|
||||||
|
temp.set(particle.velocity.z * surfaceTangentFactor, particle.velocity.y * surfaceTangentFactor, -particle.velocity.x * surfaceTangentFactor); |
||||||
|
if (surfaceTangentRotation != 0.0f) {// rotating the tangent
|
||||||
|
Matrix3f m = new Matrix3f(); |
||||||
|
m.fromAngleNormalAxis(FastMath.PI * surfaceTangentRotation, particle.velocity); |
||||||
|
temp = m.multLocal(temp); |
||||||
|
} |
||||||
|
// applying normal factor (this must be done first)
|
||||||
|
particle.velocity.multLocal(normalVelocity); |
||||||
|
// adding tangent vector
|
||||||
|
particle.velocity.addLocal(temp); |
||||||
|
} |
||||||
|
if (velocityVariation != 0.0f) { |
||||||
|
this.applyVelocityVariation(particle); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method returns the normal velocity factor. |
||||||
|
* @return the normal velocity factor |
||||||
|
*/ |
||||||
|
public float getNormalVelocity() { |
||||||
|
return normalVelocity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method sets the normal velocity factor. |
||||||
|
* @param normalVelocity |
||||||
|
* the normal velocity factor |
||||||
|
*/ |
||||||
|
public void setNormalVelocity(float normalVelocity) { |
||||||
|
this.normalVelocity = normalVelocity; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method sets the surface tangent factor. |
||||||
|
* @param surfaceTangentFactor |
||||||
|
* the surface tangent factor |
||||||
|
*/ |
||||||
|
public void setSurfaceTangentFactor(float surfaceTangentFactor) { |
||||||
|
this.surfaceTangentFactor = surfaceTangentFactor; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method returns the surface tangent factor. |
||||||
|
* @return the surface tangent factor |
||||||
|
*/ |
||||||
|
public float getSurfaceTangentFactor() { |
||||||
|
return surfaceTangentFactor; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method sets the surface tangent rotation factor. |
||||||
|
* @param surfaceTangentRotation |
||||||
|
* the surface tangent rotation factor |
||||||
|
*/ |
||||||
|
public void setSurfaceTangentRotation(float surfaceTangentRotation) { |
||||||
|
this.surfaceTangentRotation = surfaceTangentRotation; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This method returns the surface tangent rotation factor. |
||||||
|
* @return the surface tangent rotation factor |
||||||
|
*/ |
||||||
|
public float getSurfaceTangentRotation() { |
||||||
|
return surfaceTangentRotation; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void applyVelocityVariation(Particle particle) { |
||||||
|
temp.set(FastMath.nextRandomFloat() * velocityVariation, FastMath.nextRandomFloat() * velocityVariation, FastMath.nextRandomFloat() * velocityVariation); |
||||||
|
particle.velocity.addLocal(temp); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ParticleInfluencer clone() { |
||||||
|
NewtonianParticleInfluencer result = new NewtonianParticleInfluencer(); |
||||||
|
result.normalVelocity = normalVelocity; |
||||||
|
result.startVelocity = startVelocity; |
||||||
|
result.velocityVariation = velocityVariation; |
||||||
|
result.surfaceTangentFactor = surfaceTangentFactor; |
||||||
|
result.surfaceTangentRotation = surfaceTangentRotation; |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void write(JmeExporter ex) throws IOException { |
||||||
|
super.write(ex); |
||||||
|
OutputCapsule oc = ex.getCapsule(this); |
||||||
|
oc.write(normalVelocity, "normalVelocity", 0.0f); |
||||||
|
oc.write(surfaceTangentFactor, "surfaceTangentFactor", 0.0f); |
||||||
|
oc.write(surfaceTangentRotation, "surfaceTangentRotation", 0.0f); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void read(JmeImporter im) throws IOException { |
||||||
|
super.read(im); |
||||||
|
InputCapsule ic = im.getCapsule(this); |
||||||
|
normalVelocity = ic.readFloat("normalVelocity", 0.0f); |
||||||
|
surfaceTangentFactor = ic.readFloat("surfaceTangentFactor", 0.0f); |
||||||
|
surfaceTangentRotation = ic.readFloat("surfaceTangentRotation", 0.0f); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,60 @@ |
|||||||
|
package com.jme3.effect.influencers; |
||||||
|
|
||||||
|
import com.jme3.effect.EmitterShape; |
||||||
|
import com.jme3.effect.Particle; |
||||||
|
import com.jme3.effect.ParticleEmitter; |
||||||
|
import com.jme3.export.Savable; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
|
||||||
|
/** |
||||||
|
* An interface that defines the methods to affect initial velocity of the particles. |
||||||
|
* @author Marcin Roguski (Kaelthas) |
||||||
|
*/ |
||||||
|
public interface ParticleInfluencer extends Savable, Cloneable { |
||||||
|
/** |
||||||
|
* This method influences the particle. |
||||||
|
* @param particle |
||||||
|
* particle to be influenced |
||||||
|
* @param emitterShape |
||||||
|
* the shape of it emitter |
||||||
|
*/ |
||||||
|
void influenceParticle(Particle particle, EmitterShape emitterShape); |
||||||
|
|
||||||
|
/** |
||||||
|
* This method clones the influencer instance. |
||||||
|
* @return cloned instance |
||||||
|
*/ |
||||||
|
public ParticleInfluencer clone(); |
||||||
|
|
||||||
|
/** |
||||||
|
* @param initialVelocity |
||||||
|
* Set the initial velocity a particle is spawned with, |
||||||
|
* the initial velocity given in the parameter will be varied according |
||||||
|
* to the velocity variation set in {@link ParticleEmitter#setVelocityVariation(float) }. |
||||||
|
* A particle will move toward its velocity unless it is effected by the |
||||||
|
* gravity. |
||||||
|
*/ |
||||||
|
void setInitialVelocity(Vector3f initialVelocity); |
||||||
|
|
||||||
|
/** |
||||||
|
* This method returns the initial velocity. |
||||||
|
* @return the initial velocity |
||||||
|
*/ |
||||||
|
Vector3f getInitialVelocity(); |
||||||
|
|
||||||
|
/** |
||||||
|
* @param variation |
||||||
|
* Set the variation by which the initial velocity |
||||||
|
* of the particle is determined. <code>variation</code> should be a value |
||||||
|
* from 0 to 1, where 0 means particles are to spawn with exactly |
||||||
|
* the velocity given in {@link ParticleEmitter#setStartVel(com.jme3.math.Vector3f) }, |
||||||
|
* and 1 means particles are to spawn with a completely random velocity. |
||||||
|
*/ |
||||||
|
void setVelocityVariation(float variation); |
||||||
|
|
||||||
|
/** |
||||||
|
* This method returns the velocity variation. |
||||||
|
* @return the velocity variation |
||||||
|
*/ |
||||||
|
float getVelocityVariation(); |
||||||
|
} |
Loading…
Reference in new issue