Local Sprites


Scene Creation on the Client

createSceneGraph( ) starts the main tasks of WrapNetTour3D: the 3D scene is created, contact is made with the server, and a local sprite is initialized:

     void createSceneGraph(String userName, String tourFnm,                                            double xPosn, double zPosn)     { sceneBG = new BranchGroup( );       bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE);       // allow clients to be added/removed from the world at run time       sceneBG.setCapability(Group.ALLOW_CHILDREN_READ);       sceneBG.setCapability(Group.ALLOW_CHILDREN_WRITE);       sceneBG.setCapability(Group.ALLOW_CHILDREN_EXTEND);       lightScene( );         // add the lights       addBackground( );      // add the sky       sceneBG.addChild( new CheckerFloor( ).getBG( ) );  // add the floor       makeScenery(tourFnm);      // add scenery and obstacles       makeContact( ); // contact server (after Obstacles object created)       addTourist(userName, xPosn, zPosn);                      // add the user-controlled 3D sprite       sceneBG.compile( );   // fix the scene     }

Capability bits are set to allow distributed sprites to be added to and removed from the scene at runtime.

makeContact( ) sets up an input and output stream to the server and passes the input stream to TourWatcher to monitor. TourWatcher creates distributed sprites when requested by the server, and so must know about the obstacles present in the world:

     private void makeContact( )     { try {         sock = new Socket(HOST, PORT);         in  = new BufferedReader( new InputStreamReader( sock.getInputStream( ) ));         out = new PrintWriter( sock.getOutputStream( ), true);         new TourWatcher(this, in, obs).start( );   // watch server msgs       }       catch(Exception e)       {  System.out.println("No contact with server");          System.exit(0);       }     }

addTourist( ) creates a TourSprite object and connects a TouristControls object to it so key presses can make it move and rotate:

     private void addTourist(String userName,double xPosn,double zPosn)     {       bob = new TourSprite(userName, "Coolrobo.3ds", obs, xPosn, zPosn, out);       sceneBG.addChild( bob.getBG( ) ); // local sprite       ViewingPlatform vp = su.getViewingPlatform( );       TransformGroup viewerTG = vp.getViewPlatformTransform( );       TouristControls tcs = new TouristControls(bob, viewerTG);       tcs.setSchedulingBounds( bounds ); // sprite's controls       sceneBG.addChild( tcs );     }

The TourSprite object is passed a reference to the output stream going to the server. The object can then notify the server of its creation, and when it moves or rotates. The server will tell the other clients, which can affect the distributed sprite representing the user.



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