Random Points on the surface of a Sphere (Star Effect)


I have a little program which is supposed to look like Elite. I have managed to create a space station and a couple of the space ships. What I am now trying to do is create a space backdrop. One way I thought of as a quick solution was to generate random coordinates which lie on the surface of a sphere. I tinkered around with some formulas but could not figure out exactly what formulas I could use supplying random numbes and then getting x, y and z coordinates which lie on the surface of the sphere. My brother in law who is a maths teacher tried to help me but could not create formulas in the form of x = ? y = ? z = ? which I would need to write a method to generate coordinates. Can anyone help?

float x = (float)(random());
float y = (float)(random());
float z = (float)(random());
float r = sqrt(x*x + y*y + z*z);

x /= r;
y /= r;
z /= r;

creates random x/y/z values and scales them down so that they lie on a sphere with radius of 1.0

Look into spherical coordinates for the math:
for example: http://en.wikipedia.org/wiki/Spherical_coordinates

Once you implement a spherical-to-cartesian method (and cartesian-to-spherical for kicks), then the following psuedocode gives pretty good random point without too much preference for the polar regions (note the acos).

Vector randomVector(float length)
 float theta = TWO_PI * random();//random range is 0.0 to 1.0
 float phi = acos(2.0f * random() - 1.0f);
 float rho = length;
 return spherical_to_cartesianCoordinates(rho, theta, phi);

Hope that helps.

That’s a good one.

Here’s a list of a bunch of methods:

Another good one is 3rd from the bottom (Marsaglia).


As for RigidBody’s solution, it would be good to throw away all points that have r > some_value. Generating random points in cube makes this cube’s corners more ‘densly populated’ with stars when viewn from center.

Statistical uniformity is certainly nice in some scenarios, although it might not be necessary or even desirable in the case of real stars. Take the distribution of stars in the Milky Way, for instance, which from our point of view (seen edge on) exhibits a greater density in a narrow band stretching across the sky. So I guess one could say that it really depends on the galaxy in question, and even the observer’s perspective, how large a sample is taken, the viewing medium, the desired effect, etc., etc., etc.

Then again, points on a sphere are pretty cool :slight_smile:

By the way, I’m not familiar with the game “Elite”, so I’m not exactly sure what we’re shooting for here. Anyone seen this? Perhaps the game takes place in a cubic galaxy…


Elite is an old space trading game. It was released somewhere around 386 processors. We’re talking about normal star system.

As for star distribution - alternatively to placing points on sphere, one could just use background exture (cubemap or two sphere maps). Such texture would be manually created. It costs some fill rate, but with such texture you don’t need to clear color and depth buffers. It also gives free antialiasing due to texture filtering. Such texture should be as big as possible, but should not extend the size that will produce 1:1 mapping to screen.

Originally posted by k_szczech:
Elite is an old space trading game. It was released somewhere around 386 processors.
actually it came in 1984, 2 years before the 386. it was very popular on commodore c64. its 3d wireframe graphics (with hidden line removal) were cutting edge at that time :slight_smile:

That’s what I thought - the sequel called “Frontier” was released later.
I’m a former C64 demo coder - I remember “Moonfall” which was one of my favourites, but nothing could beat “Revs” (Formula 3 simulator).
As for Elite - this genre is still alive - from lightweight “Darkstar one”, through enjoyable “Freelancer” to such heavyweight games like “X3 Reunion”. There’s also “Eve online”, but my favourite space sim was Freespace 2.

As for using textures for background starfield - it’s also a good idea to have one small texture with blurred circle in it. Each star would be rendered as small quad with this texture - good antialiasing.