Game Development Reference
In-Depth Information
12 - Cartesian Coordinates
Grids and Cartesian Coordinates
A problem in many games is how to talk
about exact points on the board. A common
way of solving this is by marking each
individual row and column on a board with a
letter and a number. Figure 12-1 is a chess
board that has each row and each column
marked.
In chess, the knight piece looks like a
horse head. The white knight is located at the
point e, 6 and the black knight is located at
point a, 4. We can also see that every space
on row 7 and every space in column c is
empty.
A grid with labeled rows and columns like
the chess board is a Cartesian coordinate
system. By using a row label and column
label, we can give a coordinate that is for one
and only one space on the board. This can really help us describe to a computer the exact
location we want. If you have learned about Cartesian coordinate systems in math class,
you may know that usually we have numbers for both the rows and columns. This is handy,
because otherwise after the 26th column we would run out of letters. That board would
look like Figure 12-2.
Figure 12-1: A sample chessboard with a
black knight at a, 4 and a white knight at e, 6.
The numbers going left and right that describe the columns are part of the X-axis . The
numbers going up and down that describe the rows are part of the Y-axis . When we
describe coordinates, we always say the X-coordinate first, followed by the Y-coordinate.
That means the white knight in the above picture is located at the coordinate 5, 6 (and not
6, 5). The black knight is located at the coordinate 1, 4 (not to be confused with 4, 1).
Notice that for the black knight to move to the white knight's position, the black knight
must move up two spaces, and then to the right by four spaces. (Or move right four spaces
and then move up two spaces.) But we don't need to look at the board to figure this out. If
we know the white knight is located at 5, 6 and the black knight is located at 1, 4, then we
can just use subtraction to figure out this information.
Subtract the black knight's X-coordinate and white knight's X-coordinate: 5 - 1 = 4. That
means the black knight has to move along the X-axis by four spaces.
Subtract the black knight's Y-coordinate and white knight's Y-coordinate: 6 - 4 = 2. That
means the black knight has to move along the Y-axis by two spaces.
Search WWH ::




Custom Search