Generating random points in a sphere

Started by
7 comments, last by iMalc 10 years, 2 months ago

I am implementing volumetric cloud rendering.

So I need to generate a lot of particles in shape of a cloud.

Right now I am using this formula:

for I:=1 To ParticleCount Do

Points := VectorCreate( Random(-Radius, Radius), Random(-Radius, Radius), Random(-Radius, Radius));

Basically, it picks 3 random coords inside the defined Cloud radius range,

The problem is, clouds generated with this formula look like cubes instead of spheres.

I tried looking in google for ways to generate points in a sphere, but I could only find ways to generate points in the surface of a sphere, not in the full volume.

Any suggestions for how to change the formula to produce spheres instead of cubes?

Once I can generate spheres I can approximate a cloud shape by using many spheres.

But if there is a better method to generate a cloud shape, tell me :)

Advertisement

Two options:

(1) Write a do-while loop around your random point picking, so you keep generating random points until one inside the sphere is generated.

(2) Pick a random point on the surface of the sphere and then pick a random number between 0 and 1 which will be used to interpolate between the center of the sphere and your point at the surface. In order to get an even distribution of points, your random number should be the cubic root of a uniformly-distributed number.

Thanks, that seems a good idea, I'll try it :)

Might also make sense to switch over to a spherical coordinate system. Pick a random value from 0 - 360 for X, 0 - 360 for y, and a random value from 0 - radius for z. Then convert that to the cartesian coordinates.

http://en.wikipedia.org/wiki/Spherical_coordinate_system

It's just what I would do, you don't necessarily have to.

Might also make sense to switch over to a spherical coordinate system. Pick a random value from 0 - 360 for X, 0 - 360 for y, and a random value from 0 - radius for z. Then convert that to the cartesian coordinates.

http://en.wikipedia.org/wiki/Spherical_coordinate_system

It's just what I would do, you don't necessarily have to.

How do you pick those random values so the resulting distribution of points in the sphere is uniform? That's precisely what this thread is about. If you do the naive thing you just described, you'll end up with too many points near the center of the sphere and too many points near the poles, or something like that (not sure what "x" and "y" are in your post to know for sure).

I tested his approach now, well, it seems to work fine, the distribution might not be uniform but is enough for this case.

Also tested the do/while approach, but using spherical coordinates seems faster

Note that spherical coordinates will cause clustering at poles, which is most probably not what you want. A slightly more complicated approach is needed to properly pick points on a sphere (which, if the radius is also random, will give points inside the sphere).

On the other hand, the solution of generating points in the unit cube (or in the "radius cube", does that word exist?) and discarding points for which dot(point, point) > distance_squared is much easier and works well, too.

Here is the code that I use to generate uniform random points on a sphere (which is derived from the wolfram link posted above):


float u1 = randomRange( -1.0f, 1.0f );
float u2 = randomRange( 0.0f, 1.0f );
float r = sqrt( 1.0f - u1*u1 );
float theta = 2.0f*math::pi<float>()*u2;

return Vector3( r*cos( theta ), r*sin( theta ), u1 );

Here is the code that I use to generate uniform random points on a sphere (which is derived from the wolfram link posted above):


float u1 = randomRange( -1.0f, 1.0f );
float u2 = randomRange( 0.0f, 1.0f );
float r = sqrt( 1.0f - u1*u1 );
float theta = 2.0f*math::pi<float>()*u2;

return Vector3( r*cos( theta ), r*sin( theta ), u1 );

Thanks, it seems to work great :)

Might also make sense to switch over to a spherical coordinate system. Pick a random value from 0 - 360 for X, 0 - 360 for y, and a random value from 0 - radius for z. Then convert that to the cartesian coordinates.

http://en.wikipedia.org/wiki/Spherical_coordinate_system

It's just what I would do, you don't necessarily have to.

How do you pick those random values so the resulting distribution of points in the sphere is uniform? That's precisely what this thread is about. If you do the naive thing you just described, you'll end up with too many points near the center of the sphere and too many points near the poles, or something like that (not sure what "x" and "y" are in your post to know for sure).

Indeed, a spherical coordinate system would create clustering due to the effects of gimbal lock. The same solutions to that problem can be used to overcome this (vector algebra / quaternions)

But yes ultimately the simplest, and quite efficient solution is the rejection method where you try again if the point is outside of the radius. Do a squared-radius check of course, to avoid square roots inside the loop.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement