Causing an Explosion


Controlling the Gun

The GunTurret class builds the scene graph branch for the cylinder and cone and has two public methods: getGunBG( ) and makeRotation( ). getGunBG( ) is used by WrapShooter3D to retrieve a reference to the gun's top-level BranchGroup, gunBG, so it can be added to the scene. makeRotation( ) is called by ShootingBehaviour to rotate the cone to point at the clicked position.

The scene graph branch built inside GunTurret is shown in Figure 23-4.

The GunTurret constructor is:

     public GunTurret(Vector3d svec)     { startVec = svec;       gunBG = new BranchGroup(  );       Appearance apStone = stoneApp(  );       placeGunBase(apStone);       placeGun(apStone);     } 

startVec contains the position of the gun cone: (0, 2, 0).

Figure 23-4. Scenegraph branch for GunTurret


There's no particular significance to this coordinate, but it works well, so I've stayed with it.


apStone is a blending of a stone texture and white material, with lighting enabled, which allows lighting effects to be seen on the gun's surfaces. The blending is done using the TextureAttribute.MODULATE setting for the texture mode.

A similar approach was used in Chapter 21 for the texture applied to the particle system of quads.


placeGunBase( ) creates the lefthand side of the subgraph shown in Figure 23-4, and placeGun( ) handles the right side.

The cylinder and cone are unpickable (you only want the gun to shoot at the tiles):

     cyl.setPickable(false);     cone.setPickable(false); 

The transformGroup for the cone (gunTG) will have its rotation details changed at run-time, so its capability bits are set accordingly:

     gunTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);     gunTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 

makeRotation( ) is called with an AxisAngle4d object that, as the name suggests, is a combination of an axis (vector) and an angle to rotate around that vector. The vector can specify any direction, not just the x-, y-, or z-axes :

     public void makeRotation(AxisAngle4d rotAxis)     // rotate the cone of the gun turret     { gunTG.getTransform( gunT3d );        // get current transform       gunT3d.get( currTrans );             // get current translation       gunT3d.setTranslation( ORIGIN );     // translate to origin           rotT3d.setRotation( rotAxis );       // apply rotation       gunT3d.mul(rotT3d);           gunT3d.setTranslation( currTrans );  // translate back       gunTG.setTransform( gunT3d );     } 

The rotation is applied to gunTG. Since the cone is located away from the origin, it's first translated to the origin, rotated, and then moved back to its original position.

Globals for Repeated Calculations

A good optimization technique for Java and Java 3D is to avoid the creation of excessive numbers of temporary objects since the JVM will have to garbage collect them often, slowing down your application in the process.

Temporary objects are usually employed to hold temporary results during a method's execution: they're created at the start of the method, and discarded at its end. If the method is called frequently, then an alternative coding strategy will be to use global variables. They're created once (usually in the classes' constructor) then reinitialized by the method each time they're needed. A multitude of short-lived objects are replaced by a few reusable long-lasting ones.

This coding strategy can be seen in makeRotation( ) in GunTurret, which uses global TRansform3D and Vector3d objects (gunT3d, rotT3d, currTrans).




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