Infinite Loop in EmitterSphereShape

The following method in EmitterSphereShape can cause an infinite loop:



[java]

@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);

}

[/java]



Given a center whose distance from origin is bigger then radius.



Is the intended result of this method to pick a point within the radius and offset by center? If so it should be like this:



[java]

@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(Vector3f.ZERO) > radius);

store.addLocal( center );

}

[/java]

And since store is calculated relative to center then you can get rid of the sqrt in distance().



With:

[java]

while( store.lengthSquared() > radius * radius )

[/java]

Nice optimisation, thanks.