Related Approaches to Scene Generation


Moving the Viewpoint

This chapter's KeyBehavior class is similar to the one described in Chapter 24, since both control the user's viewpoint. They both extend ViewPlatformBehavior, so they can access the ViewPlatform's TRansformGroup. They both use the same key presses to trigger movement and rotation.

Movement can be forward, backward, left, right, up, or down, by one unit step, excluding jaunts through the walls or floor. Rotations are carried out in 90-degree turns left or right, which simplifies the implementation of the BirdsEye class. BirdsEye's main task is to calculate the position and orientation of an arrow drawn on top of the overview image when the viewpoint moves or rotates.

KeyBehavior is complicated by being involved in the manipulation of two cameras and the BirdsEye view. The movement/rotation of the cameras are done inside KeyBehavior by affecting their transformGroups, but the BirdsEye object carries out its own changes. These additional responsibilities can be seen in standardMove( ) that calls the moveBy( ) and doRotateY( ) methods with extra arguments for the extra views:

     private void standardMove(int keycode)     {       if(keycode == forwardKey)         moveBy(VFWD, FORWARD, VBACK);       else if(keycode == backKey)         moveBy(VBACK, BACK, VFWD);       else if(keycode == leftKey)         doRotateY(ROT_AMT, LEFT);       else if(keycode == rightKey)         doRotateY(-ROT_AMT, RIGHT);     } 

The arguments of moveBy( ) are constants related to the main camera, the bird's-eye view, and the back facing camera. When the main camera moves in a given direction (e.g., VFWD, meaning forward), then the back facing camera must move in the opposite direction (VBACK, meaning backward). However, both cameras always rotate in the same direction, so no distinction is made between them when doRotateY( ) is called.

moveBy( ) implements collision detection by first calculating the expected position after carrying out the requested move. If that position is occupied by an obstacle, then the move isn't executed and a warning is reported:

     private void moveBy(Vector3d theMove, int dir, Vector3d theMoveC2)     {       Point3d nextLoc = possibleMove(theMove);       if (mm.canMoveTo(nextLoc.x, nextLoc.z)) {   // no obstacle there?         targetTG.setTransform(t3d);  // nasty!           doMoveC2(theMoveC2);         be.setMove(dir);       }       else  // there is an obstacle         be.bangAlert(  );  // tell BirdsEye, so a warning can be shown     } 

This "try-it-and-see" approach is also employed in the Tour3D application in Chapter 18.


possibleMove( ) retrieves the current transform (into the global t3d) and makes the move but doesn't update the transformGroup because it's only checking if the move is possible:

     private Point3d possibleMove(Vector3d theMove)     {       targetTG.getTransform(t3d);   // targetTG is ViewPlatform's TG       toMove.setTranslation(theMove);       t3d.mul(toMove);       t3d.get(trans);       return new Point3d( trans.x, trans.y, trans.z);     } 

The new location is returned to moveBy( ), which checks it by calling canMoveTo( ) in MazeManager. If everything's okay, then targetTG will be updated with t3d. The change is achieved with a global that was set in a different method, so the offending line is commented with the word "nasty!".

moveBy( ) has to change the back facing camera and the bird's-eye view. doMoveC2( ) deals with the camera by applying a move to its transformGroup:

     private void doMoveC2(Vector3d theMoveC2)     {       camera2TG.getTransform(t3d);       toMove.setTranslation(theMoveC2);       t3d.mul(toMove);       camera2TG.setTransform(t3d);     } 

Rotations of the cameras and bird's-eye view can be carried out immediately without any collision testing, inside doRotateY( ):

     private void doRotateY(double radians, int dir)     {       targetTG.getTransform(t3d);   // rotate main camera       toRot.rotY(radians);       t3d.mul(toRot);       targetTG.setTransform(t3d);           camera2TG.getTransform(t3d);  // rotate back-facing camera       t3d.mul(toRot);  // reuse toRot value       camera2TG.setTransform(t3d);           be.setRotation(dir);  // rotate bird's eye view     } 



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