Monday, January 4, 2010

Chapter 23.  Shooting a Gun









Chapter 23. Shooting a Gun


It's an unfortunate fact that shooting (and being shot) is a major part of many types of games. Exact percentages are hard to come by, but I've seen figures which indicate that perhaps 40 percent of the top-selling console games involve shooting, rising to 50 percent for PC games. My own feeling is that the percentages are much higher.


I'm writing this as Christmas 2004 approaches, and the top action games for the PC include Half-Life 2, Doom 3, Medal of Honor: Pacific Assault, The Lord of the Rings: Battle For Middle Earth, Warhammer 40,000: Dawn of War, World of Warcraft, and so on. Where's the peace and harmony gone? Santa better have some serious armor on his sleigh this Yuletide.


Action games can be categorized into third person and first person. Third-person games frequently utilize a viewpoint slightly above and behind the main character as in Splinter Cell and Hitman. First-person games put the gun in your hand, with a viewpoint linked to the gun, as in Doom and Half-Life.


This chapter looks at how to get an object in the scene to shoot. This is a prerequisite for third person games and, of course, is needed in first person games when the enemy fires back. Chapter 24 is about first person shooting, where the shot comes from your own gun.


The Shooter3D application contains a gun (a cone mounted on a cylinder) which fires a laser beam at a point on the checkered floor clicked on by the user. The flight of the laser beam (a red cylinder) is accompanied by a suitable sound and followed by an explosion (an animated series of images and another sound).


Figure 23-1 shows three screenshots of Shooter3D. The first one has the laser beam in mid-flight, the second captures the explosion, and the third is another explosion after the user has clicked on a different part of the floor, from a different viewpoint.



Figure 23-1. The deadly Shooter3D



The cone head rotates to aim at the target point, and the animated explosion always faces the user.


Here are some Java 3D and Java features illustrated by this example:



Picking


The user's clicking on the floor is dealt with by Java 3D picking. Picking works by creating a pick shape (often a line or cone) extending from the user's viewpoint, through the mouse location and into the scene. Information can be gathered about the objects in the scene (e.g., the floor) which intersect with the pick shape.


3D sounds


The laser beam and explosion sounds are Java 3D PointSound objects. A PointSound is a sound source at a given position in the scene.


More complex rotations


The rotations of the cone and the laser beam are handled by axis angles defined using Java 3D's AxisAngle4d class. An axis angle specifies a rotation in terms of an angle turned around a vector. The vector can be any direction, not just along one of the axes.


Animation


The explosion visual is created with my ImagesSeries class, which simplifies the loading and displaying of a sequence of transparent GIFs as an animation.


Global variables for repeated calculations


Many operations, such as the method to rotate the gun, require Java 3D objects (e.g., transform3D and Vector3d). A bad way of coding these methods is to create new, temporary objects each time they're called. Object creation, and subsequent garbage collection, will slow the application down. Instead, I employ global objects, created once at startup and reused throughout the execution.


User and Java 3D threads


The delivery of the laser beam and subsequent explosion are managed by a FireBeam thread, showing how Java threads and the built-in threading of Java 3D can coexist.


The benefits of OOD


The overall complexity of this application is greatly reduced by using object-oriented design (OOD) principles; each of the main entities (the gun, the laser beam, the explosion) are represented by its own class.









    No comments: