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 ...

Get Killer Game Programming in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.