The Game Universe: Scrolling in Space
I already covered how to implement scrolling, so I'm not going to cover it again. However, there are a couple of interesting details about the scrolling in Outpost. First, the player is always at the center of the screen. This makes things a bit easier, but more importantly it gives the player the biggest game area possible to react in. If you allowed the player's character to get near the edges of the screen in a scrolling game, it's possible that an enemy could pop up and hit the player before he even had a few milliseconds to react. By keeping the player in the center of the screen, you give him good visibility of what's around him at all times.
As for the size of the game universe, I chose 16000x16000, as shown here:
// size of universe #define UNIVERSE_MIN_X (-8000) #define UNIVERSE_MAX_X (8000) #define UNIVERSE_MIN_Y (-8000) #define UNIVERSE_MAX_Y (8000)
Universe size is always a hard thing to decide, but my standard technique is the following: Estimate how many frames per second the game is going to run at, estimate the max speed the player can move, and then, based on that, decide how long you want the player to take to get from one end of the universe to the other:
universe_size = player_velocity*fps*desired_time
Hence, if the player moves at a max of 32 pixel/frames per second, and you want to run at 30 fps with a time of 10 seconds from one end to another, you have a universe size of
universe_size = 32 pixels/frame * 30 fps * 10 = 9600 units
The other issue to consider when you're making a scrolling space game is the sparseness. You may think that a space of 10x10 seconds or 16000x16000 pixels isn't that big, but to populate it, you will need hundreds if not thousands of asteroids. Otherwise, the player will be flying around forever looking for stuff to shoot! My friend Jarrod Davis learned this when writing Astro3D, which you can find on the CD.
As for the scrolling algorithm, there's not much to it. The player's position is used as the center of a window to render from, as shown in Figure 15.3.
The algorithm works by taking the player's position, and then translating the player to the origin (the screen center) and translating the objects to the player. The objects that are within the window are rendered, and the others are not. The only objects that aren't drawn this way are the stars, which are basically pixels that wrap around as they move off the edges of the screen. Hence, if you look carefully, you can see the same stars in the same positions if you move slowly in the x- or y-axis.