Search code examples
c++mathgeometryparticle-system

Calculate points around a sphere


How can I calculate points around a sphere? I need this for my particles explosion. I don't want the particle's points to be all random. I need them in a spherical pattern. For 2d circle explosion I was using this:

float n=many;
float rad = 1;
for (int i = 0; i < n; i++)
{
        float fi = 2*PI*i/n;
        float x1 = rad*sin(fi + PI)+x ;
        float y1 = rad*cos(fi + PI)+y ;
        addparticlesmart(x,y,(x1-x),(y1-y), 0.01f),r,g,b,a,0.02f);
}

Solution

  • You have a few options.

    Lat / Lon - Loop over latitude from -π/2 to + π/2 and longitude from 0 to 2π at whatever interval you like. Then convert from spherical to cartesian coordinates. While this is easy to code, it has the disadvantage that the points tend to cluster at the poles.

    Tessellation - You can pick a regular polyhedron, preferably with triangular faces, (an icosahedron is my favorite for this purpose) and recursively find the bisector of each edge of each face. You then divide that face into four triangular faces, normalizing the bisector points so they lie on the surface of the sphere. Though the points are not quite uniformly distributed over the sphere (which can be seen if you don't use an icosahedron as the base polyhedron) it appears to be much more evenly distributed than the lat / lon approach. It has as a disadvantage, being somewhat more difficult to code. A more detailed description is available here.

    Random points - I know you said you didn't like the idea of picking random points, but I'll include it here for completeness. There is a good treatment of it at Wolfram's site.