Table of content

Previous: 4.b: Putting the trail together

Lesson 5. Polar coordinates: going in circles and defining orbits

The next 3 lessons talk about polar, cylindrical and spherical coordinate systems, and are going to be a little more mathematical than the previous ones. You might wonder what these things are and what need we would have for them in video games.

The answer is that ordinary Cartesian coordinates, the good old x, y and z axes which we defined in lesson 1 and are drawn in your WED windows, are alright for levels such as modern cities and to compute straight trajectories and displacements. However, these only limit us to a small number of effects and geometries. Indeed, explosions, orbits around planets and water coming out of the shower do not look like cubes, squares or trapezoids: they are much closer to spheres, circles and cones. These shapes can be computed in Cartesian coordinates but they have complicated mathematical forms and are awkward to use. By contrast, they are very naturally described by polar, cylindrical and spherical coordinates. Once you understand how they work, you will be able to construct circles, cylinders, spheres out of particles or entities, make particles move on them or even generate velocity vectors using these geometrical forms. Your games will then be easier to program and will look more professional (nobody likes a 'cubic' explosion!). Let's get to it then.

a. Defining the polar coordinate system

In this lesson, we present the simplest case of these more 'exotic' coordinate systems: the 2-dimensional polar coordinate system.

Figure 5.1: Definition of the polar coordinate system (r,phi) compared to the Cartesian system of axes (x,y).

Let as look at Figure 5.1 which shows the plane with origin O, Cartesian axes x and y, and also a point M with coordinates Mx and My (see lesson 1). Now let us draw two lines: one from M to the origin O, and another from M straight toward the x axis (dashed line). What have we done? We have just drawn a right-angle triangle with points O, M and the point M' with coordinates (Mx,0). The right angle is of course the angle between the horizontal line from O to M', and the vertical line from M to M'. Let us define phi, the angle between the line linking M and O, and that joining O and M' (see Figure 5.1). The line MO (= going from M to O), which sits opposite to the right angle is called the hypotenuse of the triangle, and I will call its length r.

Now, by looking up formulas in the 'trigonometry' section of the 3DGS manual, we find that simple equations relate the angle phi, the length of the hypotenuse r and the length of lines MM' and OM':

cos(phi) = length of line OM'/length of line OM		(5.1)
sin(phi) = length of line MM'/length of line OM		(5.2)

Let's use the above definition of r as the length of the hypotenuse OM. We also take advantage of the fact that, by definition, the length of line OM' is equal to the coordinate Mx of points M and M' along the axis x. The same thing goes for the length of line MM' which is equal to My, the coordinate of point M along the y axis. After substituting these in equations (5.1) and (5.2) and some elementary algebra we find:

Mx = r*cos(phi)			(5.3)
My = r*sin(phi)			(5.4)

These show that the coordinates Mx and My of any point M in the plane can be written very simply as functions of the distance r and the angle phi.

Let us look at these equations. What do they mean? They mean that the position of point M can be defined equally well using the numbers Mx and My or using r and phi. We will say that r and phi form another coordinate system for the plane which is called the polar coordinate system. Further, polar coordinates r and phi are related to the x and y Cartesian coordinates by the following equations:

x = r*cos(phi)		(5.5)
y = r*sin(phi)		(5.6)

where r represents the distance between point (x,y) and the origin and takes its values between 0 and the largest distance in your level. Note that it can never be negative! Phi is defined as the angle between the x axis and the line segment joining the origin and point (x,y), and it goes from 0 to 360 degrees.

What have we gained here? To find out, let us look at the 'natural trajectories' in these coordinate systems. To do this, we just fix one coordinate, let the other run free and see what comes out. First, to warm us up, let us start with the Cartesian coordinate system.

Figure 5.2: Cartesian coordinate system. (left) If we set x = xc, we get a straight line. (middle) If we limit x and y in the intervals -1<x<1and -1<y<1 we get a square. (right) If we limit -1<x<1, -1<y<1 and -1<z<1, we get a cube.

Let us set x = xc and let y take any values. As you can clearly see on Figure 5.2 (left), the set of points M with coordinates (xc,anything) is a straight, vertical line parallel to the y axis and which crosses the x axis at x = xc. We would also get a vertical line if we fixed x to any other value. If, on the other hand, we fixed y and let x run free, we would get horizontal lines instead.

Now if we let x and y take any value between -1 and 1, we get a surface: a square of side 2 which is centered around the origin to be precise (see Figure 5.2 (middle)). Just to be thorough, if we did the same in 3 dimensions for the 3 axes x, y and z, we would get a cube (see Figure 5.2 (right)). That is what we meant when saying that Cartesian coordinates are natural for straight line movement and simple geometry: these are the shapes which naturally come out when we apply the simplest constraints on the variables of the coordinate system.

Now, for the polar coordinate system. What happens if we put constraints on r and phi? Let us first fix phi to some value and let r run from 0 to some large positive value (r>=0, remember).

Figure 5.3: Natural shapes in a polar coordinate system. (left) If we fix the angle phi but let r grow, we get a radial line starting from the origin and making an angle phi with the x axis. (right) If we fix r to some value and let phi go from 0 to 360 degrees, we get a circle of radius r centered around the origin.

Figure 5.3 (left) shows that we get a straight line of length r which runs from the origin. If we choose other values for phi, we get the same thing except that the inclination of the line changes accordingly. It is exactly like the big hand on a watch: it always pivots around the origin, and moves away from it. It is therefore called a radial line.

Now, let us fix r to some non-zero value and let phi run from 0 to 360 degrees. As shown on Figure 5.3 (right), we get a circle: the point (r,phi) follows a circle of radius r around the origin, in the counter-clockwise direction.

Finally, if we now let r take all values between 0 and some value rc, and phi between 0 and phic, we get a surface which is a portion of a disk (see Figure 5.4(left)). If phi = 360 degrees, the surface is a full disk (see Figure 5.4(right)).

So, now we know the equations for a radial line, a circle and a disk. What does it get us? This can be used a number of ways. For instance, by taking the equation of the circle as position for a spaceship, we can have it orbit around a planet. Also, the equation for the radial line can be used as direction to shoot projectiles from the origin.

Figure 5.4: (left) Surface 0<=r<=rc and 0<=phi<=phic. If we take phic = 360 degrees, the surface becomes a disk (right).

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