Skip to content

Commit 01d0725

Browse files
jmecnNehon
authored andcommitted
[taken]Fix issue #764
Fix infinity loop in EmitterSphereShape. issue #764 I test on both method: public void getRandomPoint1(Vector3f store) { float l = FastMath.pow(FastMath.nextRandomFloat(), 1f / 3f); float u = FastMath.nextRandomFloat() * 2f - 1f; float o = FastMath.nextRandomFloat() * FastMath.TWO_PI; store.z = l * u; u = 1f / FastMath.fastInvSqrt(1f - u * u); store.x = l * u * FastMath.cos(o); store.y = l * u * FastMath.sin(o); store.multLocal(radius); store.addLocal(center); } public void getRandomPoint2(Vector3f store) { do { store.x = (FastMath.nextRandomFloat() * 2f - 1f); store.y = (FastMath.nextRandomFloat() * 2f - 1f); store.z = (FastMath.nextRandomFloat() * 2f - 1f); } while (store.lengthSquared() > 1); store.multLocal(radius); store.addLocal(center); } // Test public void testGetRandomPoint() { int n = 1000000; long start = System.nanoTime(); for (int i = 0; i < n; i++) { getRandomPoint1(store); } long time1 = System.nanoTime() - start; start = System.nanoTime(); for (int i = 0; i < n; i++) { getRandomPoint2(store); } long time2 = System.nanoTime() - start; System.out.println("t1:" + time1); System.out.println("t2:" + time2); System.out.println("t1/t2:" + (float) time1 / time2); } Result: t1:352272158 t2:94436324 t1/t2:3.7302613 Method2 seems nearly 4 times faster than method1.
1 parent 5a471f7 commit 01d0725

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

jme3-core/src/main/java/com/jme3/effect/shapes/EmitterSphereShape.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ public void cloneFields( Cloner cloner, Object original ) {
9696
@Override
9797
public void getRandomPoint(Vector3f store) {
9898
do {
99-
store.x = (FastMath.nextRandomFloat() * 2f - 1f) * radius;
100-
store.y = (FastMath.nextRandomFloat() * 2f - 1f) * radius;
101-
store.z = (FastMath.nextRandomFloat() * 2f - 1f) * radius;
102-
} while (store.distance(center) > radius);
99+
store.x = (FastMath.nextRandomFloat() * 2f - 1f);
100+
store.y = (FastMath.nextRandomFloat() * 2f - 1f);
101+
store.z = (FastMath.nextRandomFloat() * 2f - 1f);
102+
} while (store.lengthSquared() > 1);
103+
store.multLocal(radius);
104+
store.addLocal(center);
103105
}
104106

105107
@Override

0 commit comments

Comments
 (0)