Skip to content

Commit 9a2d950

Browse files
authored
ParticleEmitter: improve code readability. Apply the DRY principle (jMonkeyEngine#1912)
1 parent 76a55d5 commit 9a2d950

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

jme3-core/src/main/java/com/jme3/effect/ParticleEmitter.java

+19-17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
package com.jme3.effect;
3333

34+
import java.io.IOException;
35+
3436
import com.jme3.bounding.BoundingBox;
3537
import com.jme3.effect.ParticleMesh.Type;
3638
import com.jme3.effect.influencers.DefaultParticleInfluencer;
@@ -56,7 +58,6 @@
5658
import com.jme3.util.TempVars;
5759
import com.jme3.util.clone.Cloner;
5860
import com.jme3.util.clone.JmeCloneable;
59-
import java.io.IOException;
6061

6162
/**
6263
* <code>ParticleEmitter</code> is a special kind of geometry which simulates
@@ -909,6 +910,7 @@ private Particle emitParticle(Vector3f min, Vector3f max) {
909910
p.size = startSize;
910911
//shape.getRandomPoint(p.position);
911912
particleInfluencer.influenceParticle(p, shape);
913+
912914
if (worldSpace) {
913915
worldTransform.transformVector(p.position, p.position);
914916
worldTransform.getRotation().mult(p.velocity, p.velocity);
@@ -921,10 +923,8 @@ private Particle emitParticle(Vector3f min, Vector3f max) {
921923
p.rotateSpeed = rotateSpeed * (0.2f + (FastMath.nextRandomFloat() * 2f - 1f) * .8f);
922924
}
923925

924-
temp.set(p.position).addLocal(p.size, p.size, p.size);
925-
max.maxLocal(temp);
926-
temp.set(p.position).subtractLocal(p.size, p.size, p.size);
927-
min.minLocal(temp);
926+
// Computing bounding volume
927+
computeBoundingVolume(p, min, max);
928928

929929
++lastUsed;
930930
firstUnUsed = idx + 1;
@@ -1038,15 +1038,19 @@ protected void updateParticle(Particle p, float tpf, Vector3f min, Vector3f max)
10381038
p.angle += p.rotateSpeed * tpf;
10391039

10401040
// Computing bounding volume
1041-
temp.set(p.position).addLocal(p.size, p.size, p.size);
1042-
max.maxLocal(temp);
1043-
temp.set(p.position).subtractLocal(p.size, p.size, p.size);
1044-
min.minLocal(temp);
1041+
computeBoundingVolume(p, min, max);
10451042

10461043
if (!selectRandomImage) {
10471044
p.imageIndex = (int) (b * imagesX * imagesY);
10481045
}
10491046
}
1047+
1048+
private void computeBoundingVolume(Particle p, Vector3f min, Vector3f max) {
1049+
temp.set(p.position).addLocal(p.size, p.size, p.size);
1050+
max.maxLocal(temp);
1051+
temp.set(p.position).subtractLocal(p.size, p.size, p.size);
1052+
min.minLocal(temp);
1053+
}
10501054

10511055
private void updateParticleState(float tpf) {
10521056
// Force world transform to update
@@ -1180,16 +1184,14 @@ private void renderFromControl(RenderManager rm, ViewPort vp) {
11801184
this.getMaterial().setFloat("Quadratic", C);
11811185
}
11821186

1183-
Matrix3f inverseRotation = Matrix3f.IDENTITY;
1184-
TempVars vars = null;
1185-
if (!worldSpace) {
1186-
vars = TempVars.get();
1187-
1188-
inverseRotation = this.getWorldRotation().toRotationMatrix(vars.tempMat3).invertLocal();
1189-
}
1190-
particleMesh.updateParticleData(particles, cam, inverseRotation);
11911187
if (!worldSpace) {
1188+
TempVars vars = TempVars.get();
1189+
Matrix3f inverseRotation = this.getWorldRotation().toRotationMatrix(vars.tempMat3).invertLocal();
1190+
particleMesh.updateParticleData(particles, cam, inverseRotation);
11921191
vars.release();
1192+
1193+
} else {
1194+
particleMesh.updateParticleData(particles, cam, Matrix3f.IDENTITY);
11931195
}
11941196
}
11951197

0 commit comments

Comments
 (0)