* Proper aging for particles emitted in a frame
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7958 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
6dcf5f2d50
commit
2767600fe3
@ -90,7 +90,7 @@ public class ParticleEmitter extends Geometry {
|
||||
private boolean selectRandomImage;
|
||||
private boolean facingVelocity;
|
||||
private float particlesPerSec = 20;
|
||||
private float emitCarry;
|
||||
private float timeDifference = 0;
|
||||
private float lowLife = 3f;
|
||||
private float highLife = 7f;
|
||||
private Vector3f gravity = new Vector3f(0.0f, 0.1f, 0.0f);
|
||||
@ -206,8 +206,6 @@ public class ParticleEmitter extends Geometry {
|
||||
|
||||
meshType = type;
|
||||
|
||||
|
||||
|
||||
// Must create clone of shape/influencer so that a reference to a static is
|
||||
// not maintained
|
||||
shape = shape.deepClone();
|
||||
@ -859,13 +857,13 @@ public class ParticleEmitter extends Geometry {
|
||||
// assert !unusedIndices.contains(index);
|
||||
// unusedIndices.add(index);
|
||||
// }
|
||||
private boolean emitParticle(Vector3f min, Vector3f max) {
|
||||
private Particle emitParticle(Vector3f min, Vector3f max) {
|
||||
// int idx = newIndex();
|
||||
// if (idx == -1)
|
||||
// return false;
|
||||
int idx = lastUsed + 1;
|
||||
if (idx >= particles.length) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
Particle p = particles[idx];
|
||||
@ -896,7 +894,7 @@ public class ParticleEmitter extends Geometry {
|
||||
|
||||
++lastUsed;
|
||||
firstUnUsed = idx + 1;
|
||||
return true;
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -924,7 +922,7 @@ public class ParticleEmitter extends Geometry {
|
||||
max.set(Vector3f.NEGATIVE_INFINITY);
|
||||
}
|
||||
|
||||
while (emitParticle(min, max));
|
||||
while (emitParticle(min, max) != null);
|
||||
|
||||
bbox.setMinMax(min, max);
|
||||
this.setBoundRefresh();
|
||||
@ -971,32 +969,7 @@ public class ParticleEmitter extends Geometry {
|
||||
particles[idx2] = p1;
|
||||
}
|
||||
|
||||
private void updateParticleState(float tpf) {
|
||||
// Force world transform to update
|
||||
this.getWorldTransform();
|
||||
|
||||
TempVars vars = TempVars.get();
|
||||
|
||||
|
||||
Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
|
||||
Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
|
||||
|
||||
for (int i = 0; i < particles.length; ++i) {
|
||||
Particle p = particles[i];
|
||||
if (p.life == 0) { // particle is dead
|
||||
// assert i <= firstUnUsed;
|
||||
continue;
|
||||
}
|
||||
|
||||
p.life -= tpf;
|
||||
if (p.life <= 0) {
|
||||
this.freeParticle(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
// position += velocity * tpf
|
||||
//p.distToCam = -1;
|
||||
|
||||
private void updateParticle(Particle p, float tpf, Vector3f min, Vector3f max){
|
||||
// applying gravity
|
||||
p.velocity.x -= gravity.x * tpf;
|
||||
p.velocity.y -= gravity.y * tpf;
|
||||
@ -1019,6 +992,31 @@ public class ParticleEmitter extends Geometry {
|
||||
if (!selectRandomImage) {
|
||||
p.imageIndex = (int) (b * imagesX * imagesY);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateParticleState(float tpf) {
|
||||
// Force world transform to update
|
||||
this.getWorldTransform();
|
||||
|
||||
TempVars vars = TempVars.get();
|
||||
|
||||
Vector3f min = vars.vect1.set(Vector3f.POSITIVE_INFINITY);
|
||||
Vector3f max = vars.vect2.set(Vector3f.NEGATIVE_INFINITY);
|
||||
|
||||
for (int i = 0; i < particles.length; ++i) {
|
||||
Particle p = particles[i];
|
||||
if (p.life == 0) { // particle is dead
|
||||
// assert i <= firstUnUsed;
|
||||
continue;
|
||||
}
|
||||
|
||||
p.life -= tpf;
|
||||
if (p.life <= 0) {
|
||||
this.freeParticle(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
updateParticle(p, tpf, min, max);
|
||||
|
||||
if (firstUnUsed < i) {
|
||||
this.swap(firstUnUsed, i);
|
||||
@ -1029,18 +1027,22 @@ public class ParticleEmitter extends Geometry {
|
||||
}
|
||||
}
|
||||
|
||||
float particlesToEmitF = particlesPerSec * tpf;
|
||||
int particlesToEmit = (int) particlesToEmitF;
|
||||
emitCarry += particlesToEmitF - particlesToEmit;
|
||||
|
||||
while (emitCarry > 1f) {
|
||||
++particlesToEmit;
|
||||
emitCarry -= 1f;
|
||||
// Spawns particles within the tpf timeslot with proper age
|
||||
float interval = 1f / particlesPerSec;
|
||||
tpf += timeDifference;
|
||||
while (tpf > interval){
|
||||
tpf -= interval;
|
||||
Particle p = emitParticle(min, max);
|
||||
if (p != null){
|
||||
p.life -= tpf;
|
||||
if (p.life <= 0){
|
||||
freeParticle(lastUsed);
|
||||
}else{
|
||||
updateParticle(p, tpf, min, max);
|
||||
}
|
||||
|
||||
for (int i = 0; i < particlesToEmit; ++i) {
|
||||
this.emitParticle(min, max);
|
||||
}
|
||||
}
|
||||
timeDifference = tpf;
|
||||
|
||||
BoundingBox bbox = (BoundingBox) this.getMesh().getBound();
|
||||
bbox.setMinMax(min, max);
|
||||
|
Loading…
x
Reference in New Issue
Block a user