Table of content

Previous: 5.b Circles, planets, orbits and spaceships

Lesson 6. Cylindrical coordinates: The "Soul Reaver portal" effect

Now we start the more serious stuff. 2-dimensional coordinate systems are fine for 2- dimensional video games, but we need more to help us in the 3 dimensions of everyday games. We therefore generalize the polar coordinate system by adding a third dimension.

Figure 6.1: Definition of cylindrical coordinates (r,phi,z) in relation to Cartesian coordinates (x,y,z).

a. The cylindrical coordinate system

The easiest and most straightforward way is to just use the z axis of the Cartesian coordinate system, like so (see Figure 6.1):

x = r*cos(phi);			(6.1)		
y = r*sin(phi);			(6.2)		
z = z;				(6.3)			

where z takes all values between the ceiling and the floor of your level.

As shown on Figure 6.1, what we mean here is that any point (x,y,z) in space can be uniquely specified by the numbers (r,phi,z) where r is now the distance of the point from the z axis, phi is the angle this distance makes with the x axis, and z is its height over the (xy) plane. r, phi and z are the coordinates of that point in the cylindrical coordinate system. Notice now that r has changed meaning: it is no longer the distance from M to the origin, but rather the distance between the projection of M on the plan (xy) and the origin in the (xy) plane. The cylindrical coordinate system is a straightforward generalization of the polar coordinate system, since we just added the Cartesian axis z to it.

Now what are natural trajectories in this system? Figure 6.2, Figure 6.3 and Figure 6.4 show the possibilities offered by cylindrical coordinates.

Figure 6.2: Varying one coordinate in the cylindrical system. (left) Varying only r gives a horizontal radial line. (middle) Varying only phi gives a horizontal circle. (right) Varying only z gives a vertical line.

First, let us only allow one coordinate to vary. We get:

- r varies, phi and z constant: straight horizontal line starting at the origin, with height z and orientation phi (Figure 6.2 (left)).

- phi is free, r and z fixed: horizontal circle with radius r and height z (Figure 6.2 (middle)).

- z is free, phi and r fixed: straight vertical line which passes through the (xy) plane at the point specified by r and phi (Figure 6.2 (right)).

Figure 6.3: Varying 2 coordinates gives the following surfaces. (left) Freezing coordinate phi gives a half plan. (middle) Freezing coordinate r gives a hollow tube and (right) freezing z gives a disk.

Now, if we let two coordinates vary, we get the following three surfaces (see Figure 6.3):

- z and r vary, phi constant: we get a half plane similar to a door with its hinges on the z axis and orientation phi (Figure 6.3 (left)).

- phi and z vary, r is frozen: now we get a vertical hollow tube of radius r and centered around the z axis (Figure 6.3 (middle)).

- r and phi vary, z is frozen: we get a disk of radius r and at height z (Figure 6.3 (right)).

Finally, since we are in three dimensions now, we can let all three coordinates r, phi and z vary to get a volume: we get a full cylinder of radius r centered around the z axis (see Figure 6.4).

Figure 6.4: Taking 0<=r<=r0, 0<=phi<=360 and 0<=z<=z0 gives a full cylinder.

Alright, that was fun ;-) but when and what should cylindrical coordinates be used for? They could greatly simplify programming anytime the geometry of the game or the trajectories of entities look (even roughly) like a cylinder, a spiral or anything which looks like what is shown on Figure 6.2, Figure 6.3 or Figure 6.4. For instance, if you want to create a tornado, cylindrical coordinates would probably be more practical than Cartesians coordinates.

Next: 6.b The Soul Reaver portal effect