To store the position of an entity in a 2D game, you use a coordinate system with two components, x and y, that correspond to the physical pixels on the screen. For a 3D game you need to use a coordinate system with an additional component called z (see Figure 8-1). This extra component is used to measure the distance into, or out from, the screen. Of course, Pygame can't actually draw anything using 3D coordinates because the screen is flat. So 3D coordinates will eventually have to be converted to 2D before they are used to render anything to the screen. We will cover how to do this later in the chapter, but first we need to know how to store a 3D coordinate with Python.
In a 3D coordinate system, x points right and y points up. This is different from the coordinate system we have been using to create 2D graphics, where y points down the screen. In 3D, if you increase the y component, the coordinate will move up the screen and not down.
The z axis in a 3D coordinate system can point in one of two ways depending on the graphics technology used; either it points into the screen (away from the viewer), or it points out of the screen (toward the viewer). In this book, we will be using a coordinate system with a positive z that points out of the screen. Figure 8-2 shows a 3D coordinate system with a spy droid— represented by a circle—at coordinate (7, 5, 10). Since this isn't a pop-up book, the extra axis is represented as a diagonal line.
The units for a 3D coordinate could represent anything depending on the scale of the game. If you are writing a first-person shooter, the units may be meters or even centimeters, but for a space game the units may represent a much larger scale (maybe light-years)! Assuming that the droid's coordinates are in meters and soldier alpha (the player character) is standing at coordinate (0, 0, 0) facing along the negative z axis, the droid would be hovering 10 meters in the air, behind and to the right of the player.
Was this article helpful?
Post a comment