Managing a Laser Beam


Adding an Image to the Viewpoint

The call to gunHand( ) inside initUserControls( ) hides the creation of a TexturedPlane object just in front of and below the user's viewpoint:

     private PlatformGeometry gunHand(  )     {       PlatformGeometry pg = new PlatformGeometry(  );       // define a square of sides 0.2f, facing along the z-axis       Point3f p1 = new Point3f(-0.1f, -0.3f, -0.7f);       Point3f p2 = new Point3f(0.1f, -0.3f, -0.7f);       Point3f p3 = new Point3f(0.1f, -0.1f, -0.7f);       Point3f p4 = new Point3f(-0.1f, -0.1f, -0.7f);       TexturedPlane tp = new TexturedPlane(p1, p2, p3, p4, GUN_PIC);       pg.addChild( tp );       return pg;     } 

The Java 3D PlatformGeometry is nothing more than a detachable BranchGroup (confirmed by looking at its source code in java3d-utils-src.jar). It's added to ViewPlatform's transformGroup by a call to ViewingPlatform's setPlatformGeometry( ).

My TexturedPlane class is a Shape3D holding a four-point QuadArray; the constructor is called with the points and a transparent GIF that will be pasted onto the quad's front face. TexturedPlane is a simplification of the ImagesSeries class of the last chapter, which lays a sequence of GIFs over a quad to exhibit an animation.

The most complicated aspect of using TexturedPlane is determining its scene coordinates. I used trial and error until the image appeared at the bottom edge of the screen. It's helpful to remember that the (x, y) coordinate pair (0, 0) corresponds to the middle of the canvas, and that negative z-coordinates are farther into the scene. For example, look at the following points:

     Point3f p1 = new Point3f(-0.1f, -0.1f, -1.7f);     Point3f p2 = new Point3f(0.1f, -0.1f, -1.7f);     Point3f p3 = new Point3f(0.1f, 0.1f, -1.7f);     Point3f p4 = new Point3f(-0.1f, 0.1f, -1.7f); 

These points cause the gun-in-hand image to be located (as shown in Figure 24-5) in the center of the screen, farther into the scene.

Figure 24-5. Gun-in-hand at a new initial location


An alternative way of placing the image is to use Java 3D's Viewer and ViewerAvatar. A fragment of code illustrates the approach:

     TexturedPlane tp = new TexturedPlane(p1, p2, p3, p4, GUN_PIC);     ViewerAvatar va = new ViewerAvatar(  );     va.addChild( tp );     Viewer viewer = su.getViewer(  );     viewer.setAvatar(va); 

ViewerAvatar plays the same role as PlatformGeometry: it's a subclass of BranchGroup. setAvatar( ) connects it to the TRansformGroup above the ViewPlatform node.

The word avatar in this situation means an on-screen representation of the user (or part of the user). Section 6.3 of Java 3D Programming (Manning Publications) by Daniel Selman describes two avatar examples (PlatformTest.java and AvatarTest.java) using the techniques described here. In PlatformTest.java, the avatars are cones with text labels, and in AvatarTest.java the avatar is a large cube.


Why Use a GIF?

Since any shape can be attached to the viewpoint, why choose a QuadArray acting as a surface for a transparent GIF? One reason is efficiency because it would require a complex shape to represent a hand and a gun, together with suitable coloring and textures. By contrast, the GIF is only 5 KB.

Another advantage is that occlusion is less likely to happen. Occlusion occurs when the image at the viewpoint intersects with a shape in the scene and is partially hidden. Since the GIF is flat, the viewpoint must move right up to a shape before occlusion occurs.

3D special effects, such as the gun recoiling, can still be coded by using multiple gun-in-hand images and employing a variant of the ImagesSeries class to animate them.




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