Setting Up the Target


Chapter 24. A First-Person Shooter

This is the second chapter on 3D shooting. Chapter 23 was about third-person shooting, or when an object in the scene fires at something. This chapter is about first-person shooting (FPS) and puts the gun in the user's hand. There are several new issues to deal with:


Faking the 3D

There's no need to build 3D models of the user's arm, hand, and gun. The objects always stay in the same spot in front of the user, so a 2D image showing a static view of the gun hand is quite sufficient. In general, a 3D model is only needed if the user is going to move around it or through it. To keep the gun hand image fixed relative to the user's viewpoint, I'll attach the image to the ViewingPlatform component of the scene graph.


Keyboard-based movement

Up to now, I've been using the Java 3D OrbitBehavior class to change the viewpoint with the mouse, but I'm going to replace it with my own KeyBehavior class (so called because key presses drive the movement). The viewer can move forward, backward, left or right, rotate left or right, and float up or down.


Rapid-fire action

The gun in Chapter 23 can fire only a single laser beam at a time. Another beam cannot be shot until the current one has disappeared or has been replaced by an explosion. In this chapter, I'll explain how to have multiple laser beams and explosions in the scene at the same time. This is important when the user wants to indulge in some rapid firing.


Hitting a target

Chapter 23 employs Java 3D picking to guide a beam to its target. This chapter utilizes a simpler approach, which detects a hit by comparing the beam's current location with the target's coordinates. A laser beam that misses the target vanishes after traveling a certain distance.


Creating beams and explosions

This chapter reuses (or slightly changes) the techniques of Chapter 23 for creating laser beams (thin red cylinders) and animated explosions, so it's advisable to read that chapter before reading this one.

Figure 24-1 contains two screenshots for the FPShooter3D application: the first has the user strafing a robot with only laser beams that come close enough to the robot explode. The second screenshot shows a similar scene after the user has crept behind and up close to the robot.

Figure 24-1. FPS diplomacy


A laser beam that misses the target vanishes after traveling a certain distance. To simplify the coding, no sounds are played. The explosions do no harm to the robot, which keeps standing there.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net