[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.
v3.2
Yan 7 years ago committed by Nehon
parent 5a471f7ef7
commit 01d0725a78
  1. 10
      jme3-core/src/main/java/com/jme3/effect/shapes/EmitterSphereShape.java

@ -96,10 +96,12 @@ public class EmitterSphereShape implements EmitterShape {
@Override @Override
public void getRandomPoint(Vector3f store) { public void getRandomPoint(Vector3f store) {
do { do {
store.x = (FastMath.nextRandomFloat() * 2f - 1f) * radius; store.x = (FastMath.nextRandomFloat() * 2f - 1f);
store.y = (FastMath.nextRandomFloat() * 2f - 1f) * radius; store.y = (FastMath.nextRandomFloat() * 2f - 1f);
store.z = (FastMath.nextRandomFloat() * 2f - 1f) * radius; store.z = (FastMath.nextRandomFloat() * 2f - 1f);
} while (store.distance(center) > radius); } while (store.lengthSquared() > 1);
store.multLocal(radius);
store.addLocal(center);
} }
@Override @Override

Loading…
Cancel
Save