Game Development Reference
In-Depth Information
When we start the player for the first time on a level, we will use very similar logic. The only
problem we face is when the player is positioned near an edge when it is started. Let's look at
that example now:
1. The player's starting position in world is in row 2, column 3, or 64x and 96y .
2. The game logic will first try to position the camera so the player is in the center of the
screen. The problem in this example is that this would offset the camera outside to
viewable screen area.
camera.x = player.worldX - (.5 * camera.width) = 64 - 192 = -128
camera.y = player.worldY - (.5 * camera.height) or 96 - 192 = -86
3. The camera cannot be position here, so we need to re-orient it for it sits at 0,0:
camera.x = 0;
The player now must be offset from this new starting position for the camera:
player.x = player.worldX + camera.x = 64 + 0 = 64
The same goes for the y axis:
camera.y = 0
player.y = playerWorldY + camera.y = 96 + 0 = 96
Figure 10-8 illustrates this example.
Figure 10-8. Player start position example
Search WWH ::




Custom Search