Video Game Math: Polar and Spherical Notation
What is Polar and Spherical Notation?
These systems are alternate ways of describing a position in space. This allows us a different way of calculating positions.
Polar and Spherical Coordinates
In a “normal” Cartesian coordinate system, you have coordinates in an (x, y) pattern (or (x, y, z) for 3D) – along with an arbitrary point known as the “Origin”. Each xyz coordinate then describes a position relative to that coordinate, with the X value usually being horizontal displacement, Y being vertical displacement and Z being depth displacement. Note that xyz doesn’t always map to those values, in particular Y and Z are often flipped for specific applications, but it’s a good general rule of thumb.
Polar coordinates are used in 2D to describe a different kind of displacement. Instead of xyz, they use (r, θ) (r, theta), where θ is the angle from the origin (measured from the far right being 0°) and r is the distance from the center. In 3D, we use the Spherical coordinate system, which is similar to Polar but uses (ρ, θ, φ) (rho, theta, phi) where ρ is the distance, θ is the vertical angle (y/x displacement) and φ is the horizontal angle (x/z displacement). Although the remainder of this article will be talking about Polar specifically, 3D equivalents exist using Spherical coordinates/equations/systems.
The Polar system sounds overly clunky – especially when you’re used to the typical Cartesian system we all learned in school, but it provides some definitive advantages in certain cases. For instance, the equation for a circle in Cartesian coordinates is the complicated (X – Cx)2 + (Y – Cy)2 = Cr2 – which also involves the quadratic equation if you need to isolate a variable. In polar equations, a circle can be done as easily as r = Cr. The inverse is also true – while a line equation is relatively easy in Cartesian, y = mx + b, in Polars this becomes much more complicated.
Polar equations also aren't very friendly to a computer. Although storing data in Cartesian coordinates is fairly trivial, and displaying objects at those coordinates is as well, most systems don’t naturally handle Polar coordinates without some conversion.
So why use Polar equations at all?
One of the things Polars do, by virtue of the fact that everything they do is based on curvature, is handle curves (circles, ellipses, spirals, etc.). Most organic things don’t follow linear patterns, so having access to an entire library of equations that make for convenient curves gives you the ability to do movement patterns, interesting shot patterns in a “bullet hell” type game, handle splash, and a ton of other options easier than their Cartesian counterparts. For instance, a “flower” pattern is a simple equation (r = cos(2θ)) whereas the same equation in Cartesian systems would be insane.
Additionally, one of the core principles of animation is the use of arcs – which Polar equations handle more simply than Cartesian ones do – making Polar calculations incredibly useful in areas like animating living creatures and handling motion capture algorithms.
Although the use of Polars is somewhat an “edge case”, and there’s certainly workarounds to accomplish the same things in Cartesian systems, understanding how they work gives you a useful tool in the programmer’s toolbox.