Controlling the GunThe 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
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.
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.
|