O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Basic 3D Sprite

Sprite3D is the base class for creating 3D sprites. The TourSprite subclass is used to create the user's robot sprite, and AlienSprite is a subclass of TourSprite for the alien hand.

TourSprite is controlled by TouristControls, which monitors user key presses and can adjust the sprite's position or the user's viewpoint as needed. AlienSprite is periodically updated by the TimeBehavior class to make the alien hand chase the user's sprite.

Figure 18-6 shows the public methods of the Sprite3D and Behavior classes and the relationships between them.

Sprite3D and Behavior classes

Figure 18-6. Sprite3D and Behavior classes

Sprite3D represents a model able to move about the XZ plane, rotate around the y-axis, and detect obstacles and floor boundaries. The sprite can be made inactive, which will cause it to disappear from the scene.

The constructor for Sprite3D utilizes PropManager to load the model representing the sprite. It then adds a Switch node and TransformGroup above the model's graph. The result can be seen in the branches for the robot (Coolrobo.3ds) and alien hand (hand1.obj) in Figure 18-4. Here is the relevant code:

 PropManager propMan = new PropManager(fnm, true); visSwitch = new Switch(); // for sprite visibility visSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); visSwitch.addChild( propMan.getTG() ); // add obj to switch visSwitch.setWhichChild( Switch.CHILD_ALL ); // make visible ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required