Camera / Player Movement

As mentioned previously, the player movements are actually camera movements. The game follows the usual convention of using up and down to move forward and backward, while left and right rotate the camera. The variables locationx and locationz are used to keep track of the player's position, and angley indicates the orientation of the camera. When right and left are pressed, angley is modified, and when up and down are pressed, the location is updated. The variables stepx and stepz are used to keep note of how much to advance in each forward and backward step. This reduces the need to repeat sine/cosine calculations. At the beginning, the player is on the upper wall looking to the negative z axis. The figure below shows how movements are carried out.

When moving, it is important to avoid crossing walls. While there are many ways to do this, the picking feature of M3G is handy. Find the wall nearest to the direction in which the player is moving, calculate the distance, and if it is too small, disallow the movement. Picking is done at the center of the screen since the player is going in the direction is looking at. Picking uses the current camera's near plane as the origin of the picking ray, and when the player move backward, the camera is temporarily reversed to detect walls on the player's back.

Figure 64: Player movements

When a Fire press is detected, picking is used again to detect which wall is in front of the player. If a wall is found, its alpha factor is set to 0.8 and its appearance is set to clearWallAppearance. This makes the wall semitransparent, showing other walls behind it. When the user moves again, the wall returns to its original appearance and alpha factor.

The background is made of a star field image. Since the background is set in screen coordinates, when the player rotates, the background stays in the same place, giving a nonrealistic impression. To solve this problem, whenever the player turns right or left the background is shifted using the cropping mechanism. This gives the illusion of a star field that is in synchrony with the player's movements.