## 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 commonly use 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*).

### 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*).

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 to do, if you follow the following conversion functions:

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

(4)### 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, $\rho$, which is the distance to a point, $\theta$, which is the angle in the xy-plane to the point, and $\phi$, which is the angle the point makes from the z-axis (strait 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.

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:

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

(10)## What's Next?

This covers the basics of 3D coordinate systems. Of course, there is a lot more to game math that coordinate systems, but this is something that I commonly put to use in the games I make, so I figured it would be worth sharing.

Having problems with this tutorial? Try the troubleshooting page! |