Table of content

Previous: 6.b The Soul Reaver portal effect

# Lesson 7. Spherical coordinates: creating spheres out of particles

# a. Definition of spherical coordinates

Spherical coordinates are, as the name indicates, specially designed to describe spherical objects and trajectories on spheres. Figure 7.1 shows a point M and its coordinates both in the usual Cartesian coordinate system, (Mx, My, Mz), and in the spherical system, (r, theta, phi). r is now the distance between point M and the origin. Phi is an angle, taking its values between 0 and 360 degrees, which measures the orientation of the projection of the point M on the (xy) plane. Finally, theta is the angle between M and the z axis, and takes its values between 0 and 180 degrees.

Figure 7.1: Definition of the spherical coordinate system (r,theta,phi).

Here is the mathematical relation between the Cartesian coordinates (x,y,z) and the spherical coordinates (r,theta,phi):

    X = r*cos(phi)*sin(theta);		(7.1)
    Y = r*sin(phi)*sin(theta);		(7.2)
    Z = r*cos(theta);			(7.3)

These equations can be derived using the above figure and elementary trigonometry but that would not be of much use to us. However, we can get a feeling of what angles theta and phi do by using analogies. For the lucky readers who are good at geography, angles theta and phi are roughly similar to the angles of latitude and longitude used to specify a location on earth, respectively (only roughly, because although the directions they represent are identical, given values for them do not correspond to matching points on a sphere and the terrestrial globe. For instance, latitude 0 is the equator which corresponds to theta = 90 degrees). Also, as mentioned in lesson 2, their equivalents are used in WED to specify the position of the sun in maps.

Ok, now let's see what are the natural trajectories, surfaces and volumes in this coordinate system.

Figure 7.2: Varying a single coordinate: lines. (left) Varying r gives a radial line which can point in any direction in space. (middle) Varying phi gives a horizontal circle. (right) Varying theta gives a vertical half- circle around the origin.

Varying one coordinate - Lines:

- r varies, theta and phi constant: a straight line originating at O with a direction in 3-dimensional space specified by theta and phi (Figure 7.2 (left)).

- phi varies, r and theta constant: a horizontal circle with radius r*sin(theta) and a distance z = r*cos(theta) above (xy) (Figure 7.2 (middle)). The circles produced this way are identical to circles of constant latitude on a terrestrial globe.

- theta varies, r and phi constant: a vertical half-circle (remember, theta goes from 0 to 180 degrees, not 360!) of radius r and with horizontal orientation phi (Figure 7.2 (right)). Following the above analogy, this a circle of constant longitude.

Figure 7.3: Varying 2 coordinates: surfaces. (left) r fixed gives a hollow sphere. (middle) theta fixed gives a hollow cone. (right) phi fixed gives a vertical half-disk passing through the origin.

Varying two coordinates - Surfaces:

- theta and phi vary, r constant: empty sphere of radius r around the origin (Figure 7.3 (left))

- r and phi vary, theta constant: empty cone around z axis, with opening 2*theta (Figure 7.3 (middle)).

- r and theta vary, phi constant: vertical half-disk (again, theta goes from 0 to 180) passing through the z axis and with orientation phi in the (xy) plane (Figure 7.3 (right)).

Varying all coordinates - Volume:

- r varies from 0 to rc, phi goes from 0 to 360 and theta from 0 to thetac: a solid cone of opening 2*thetac (see Figure 7.4 (left)).

- If thetac = 180 degrees, we get a solid sphere or radius rc (see Figure 7.4 (right)).

Figure 7.4: Varying all coordinates: volumes. (left) 0<=r<=rc, 0<=phi<=360 and 0<=theta<=thetac gives a filled cone. (right) If theta goes from 0 to 180 degrees, we obtain a filled sphere.

The example for this lesson is a cute script which illustrates well the meaning of angles theta and phi. This script is however rather long and repetitive, so we will only look at the important ideas here. I refer the reader to file lesson7.wdl to really go into the details. The program creates two spheres with particles, one blue and one red, each rotating slowly on themselves, and with the red sphere also orbiting the blue one.

There is one action, a_spheres, which we attach to an arbitrary, invisible entity. It first constructs the blue sphere at the entity's position using small blue particles which carry the function blue_square(). It then produces the red sphere with red particles carrying function red_square(). When this is done, it moves the red sphere along a circle around the blue one.

Next: 7.b Making stationary spheres