4 min read

3D Coordinate Systems

Introduction

In this tutorial, we will look at coordinate systems in 3D. Before getting into it, we will talk a little bit about why there are so many coordinate systems, and why we care about them in games. We will then look at each of the three commonly used 3D coordinate systems, and discuss how to convert between them.

Who Cares about Coordinate Systems?

It seems like most of the time that I start to talk to beginning game programmers about game math or coordinate systems, they get a little nervous. It would seem that we would all prefer to not have to do a bunch of math to make our game work. But since games tend to model the real world in most aspects, and the real world usually behaves pretty mathematically, we need to take game math into consideration when we are making a game. In this tutorial, we will look at three different 3D coordinate systems. I’m sure some of you are saying, “Isn’t one enough? Why do we need three?!” Well, whether you believe it or not, coordinate systems are created to make life easier for people. Some coordinate systems are easier for some tasks, while other systems are easier for other tasks. It will be to your benefit to learn about these different systems so that you can choose the best one for your game.

Coordinate Systems

We will now take a look at the three most common 3D coordinate systems, which are each discussed in their own section below.

Cartesian Coordinates

Cartesian coordinates are the most common coordinate system. In fact, the rendering system in XNA only understands this system. In this system, for any particular point, there is an x-, y-, and z-coordinate, which typically correspond to how far the object is left and right, forward and back, and up and down respectively. Of course, in your XNA game, the player may be able to move around and view the scene from different directions, so the directions of the different coordinates will change as the player moves around. If you have ever had a math class that goes beyond the basics, you have probably used this coordinate system. You can think of this coordinate system like this: you are standing at a particular point, and to get to another point, you need to go a certain distance left or right (x), a certain distance forward or back (y) and a certain distance up or down (z).

Cartesian coordinate system

Cylindrical Coordinates

The cylindrical coordinate system is probably not as familiar to you. This system is used when your game has symmetry around an axis. This system has three coordinates, r, theta, and z. r is the distance to the point in the xy-plane, theta is the angle to the point in the xy-plane, and z is how far up or down the point is. You could think of this coordinate system like this: you are standing at a particular point, and to get to another point, you determine how far you would have to travel along the flat ground (the xy-plane) to get to it (the r coordinate), what direction you would have to go to get there (theta), and how far up in the air, or down in the ground the object is (z).

Cylindrical coordinate system

Like I said before, though, XNA (and virtually all graphics libraries for that matter) only understand Cartesian coordinates, so in order to allow things to be drawn, we will need to convert our cylindrical coordinates back to cartesian coordinates. This is fairly easy if you follow the following conversion functions:

#..# x = r \cos \theta #..#

#..# y = r \sin \theta #..#

#..# z = z #..#

To convert from Cartesian coordinates to cylindrical, follow these functions:

#..# r = \sqrt{x^2 + y^2} #..#

#..# \tan\theta = \frac{y}{x} #..#

#..# z = z #..#

Spherical Coordinates

The third coordinate system that we will discuss is the spherical coordinate system. This system, too, is probably not quite as familiar to you. This system is best used when a game has symmetry around a point. It, too, has three coordinates, ρ, which is the distance to a point, θ, which is the angle in the xy-plane to the point, and φ, which is the angle the point makes from the z-axis (straight up). You can think about this coordinate system like this: a point can be determined by how far away it is from you, the angle you would have to travel across the ground to get to it, as well as the angle you would have to travel up or down to get to it.

Spherical coordinate system

This system, too, can be pretty useful in the right situation, but you must convert it to Cartesian coordinates in order for XNA to be able to draw with it. This can be done with the following formulas:

#..# x = \rho \sin \phi \cos \theta #..#

#..# y = \rho \sin \phi \sin \theta #..#

#..# z = \rho \cos \phi #..#

To convert from Cartesian coordinates back to spherical, follow these functions:

#..# \rho = \sqrt{x^2 + y^2 + z^2} #..#

#..# \tan\theta = \frac{y}{x} #..#

#..# \cos \phi = \frac{z}{\sqrt{x^2 + y^2 + z^2}} #..#