From 1b2c84da2eb7b2bc2aff3c11534bb39988167cb1 Mon Sep 17 00:00:00 2001 From: Yan <115050813@qq.com> Date: Sun, 14 Jan 2018 18:08:23 +0800 Subject: [PATCH] [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. --- .../com/jme3/effect/shapes/EmitterSphereShape.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/effect/shapes/EmitterSphereShape.java b/jme3-core/src/main/java/com/jme3/effect/shapes/EmitterSphereShape.java index 99d76205d..a74eeaf39 100644 --- a/jme3-core/src/main/java/com/jme3/effect/shapes/EmitterSphereShape.java +++ b/jme3-core/src/main/java/com/jme3/effect/shapes/EmitterSphereShape.java @@ -96,10 +96,12 @@ public class EmitterSphereShape implements EmitterShape { @Override public void getRandomPoint(Vector3f store) { do { - store.x = (FastMath.nextRandomFloat() * 2f - 1f) * radius; - store.y = (FastMath.nextRandomFloat() * 2f - 1f) * radius; - store.z = (FastMath.nextRandomFloat() * 2f - 1f) * radius; - } while (store.distance(center) > radius); + 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); } @Override