Table of content

Previous: 7.a Definition of spherical coordinates

# b. Making stationary spheres

So, how can we make spheres out of particles? Well, we can't of course but, as shown on Figure 7.5, we can spread particles in space so that they give the impression of spheres being there. They look a little like the hologram of the death star in the first Star Wars movie.

Figure 7.5: We 'create' moving and rotating spheres by judiciously placing particles in space using spherical coordinates and by moving them around circles.

How do we do that? By looking at Figure 7.3 (left), we see that if we fix the coordinate r, and we let theta and phi take any value, equations (7.1), (7.2) and (7.3) span the surface of a sphere of radius r. All we have to do then is position particles in space using these equations while fixing r at the value radius_spheres = 100. Let's try the following lines of code for the blue sphere:

    	theta = 0;
    	phi = 0;
    	while(theta<=180)
    	{
    		while(phi<=360)
    		{
    			phi += 10;
    			position.x = radius_spheres*cos(phi)*sin(theta);
    			position.y = radius_spheres*sin(phi)*sin(theta);
    			position.z = radius_spheres*cos(theta);

    			effect(blue_square,1,position,nullvector);
    			wait(1);
    		}
    		theta += 10;
    		phi = 0;
    	}

The instructions in the loop generate a vector position, at the tip of each we create a blue particle with function blue_square() attached to it. Vector position has a fixed length radius_spheres and its direction is specified by variables theta and phi. We use loops on theta and phi to sample a large number of directions on the sphere. The outer loop is on theta, with theta going from 0 (= north pole) to 180 degrees (= south pole) in steps of 10 degrees. For each value of theta, the inner loop makes the angle phi go from 0 to 360 degrees also in steps of 10 degrees. This makes a grand total of around (180/10)*(360/10) = 18*36 = 648 particles per sphere.

To be more precise, the code creates the blue sphere by successively 'drawing' horizontal circles of constant latitude on the sphere one-by-one (like the one shown on Figure 7.2 (middle)), starting at the north pole and ending at the south pole. This is because phi goes from 0 all the way to 360 degrees before theta is updated at the next iteration (see structure of the loops above).

In the case of the red sphere, just for the sake of it, we reverse the loops on theta and phi: we first choose a value for phi and then we let theta go from 0 to 180 degrees (see below):

    	theta = 0;
    	phi = 0;
    	while(phi<=360)
    	{
    		while(theta<=180)
    		{
    			theta += 10;
    			position.x = radius_spheres*cos(phi)*sin(theta);
    			position.y = radius_spheres*sin(phi)*sin(theta);
    			position.z = radius_spheres*cos(theta);

    			// create red particle with parameters (theta,phi)
    			effect(red_square,1,position,nullvector);
    			wait(1);
    		}
    		phi += 10;
    		theta = 0;
    	}

This way, we build the red sphere by tracing out in space vertical half-circles of constant longitude all around the sphere (like the one shown on Figure 7.2 (right)). Now, it is theta which goes from 0 to 180 degrees before phi is updated again.

Next: 7.c Building moving spheres