## Coordinate Spaces and Moving among Them

Graphics programming boils down to figuring out how to express how an object appears and moves around in space. Before you can begin to move an object in space, you need to orient where it is and how it moves around—you need a coordinate system. A coordinate system requires

A point of origin

A consistent unit of measurement

A convention to orient the positive and negative directions

The earliest coordinate system most of us remember is the Cartesian coordinate system. The 2D Cartesian system is represented by two perpendicular lines, one horizontal (x-axis) and one vertical (y-axis). Each line contains equally spaced ticks measured in the same unit of length (when you learned this system, you probably used graph paper, with each square representing equal length). The place where the x- and y-axes intersect is the *origin*. Each tick on the horizontal line to the right of the origin and on the vertical line above the origin is considered positive. Each tick on the horizontal line to the left of the origin and on the vertical line below the origin is considered negative. In the 3D Cartesian coordinate system, a third line, the z-axis, is added, perpendicular to the x- and y-axes (see Figure 4.1). Objects moving up and to the right are moving in a positive direction, and objects moving down and to the left are moving in a negative direction.

**Figure 4.1** Metal’s 3D coordinate space

Even though this system is familiar, it’s not the only coordinate system. The axis pointing up could be the z-axis instead of the y-axis, for example. Or up on the y-axis could be negative instead of positive. Another important aspect of coordinate systems, especially applicable later in this chapter, is to know whether you are dealing with a left- or a right-handed coordinate system. This has to do with positive and negative orientation in relation to the axis of rotation. If the coordinate system is right handed, then the positive axis of rotation moves counterclockwise. If the coordinate system is left handed, the positive axis of rotation is clockwise.

It’s important to know the rules governing your coordinate system before you begin working with it. Metal actually has two coordinate systems. One applies to the 3D world space and the other applies to the rasterized 2D projection space. In Metal’s 3D coordinate system, both the x- and y-axes run from −1 to 1, making those coordinates two units long. The z-axis is different and runs only from 0 to 1. Metal’s 2D coordinate system is the same as the one used by Core Graphics but opposite the OpenGL coordinate system. Its origin is in the upper-left corner of the projection space. The x value is the distance across the space that an object appears, and the y value is the distance down the space that the object appears. If you are not familiar with the coordinate system you will be working with, you won’t be able to accurately describe how you want your objects to appear in space.