Example code

Here are some details about the example projects that accompany this chapter.

The Cube project

This is a complete example of the first spinning cube project discussed in this chapter, where we generate the model data in code and submit it to IwGx for rendering using IwGxDrawPrims. See the following screenshot:

The Cube project

The Cube2 project

This project is almost identical to the previous project, except that the model data for the cube has been exported as a GEO file from a 3D modeling package.

The Skiing project

For this chapter the Skiing game waves goodbye to its old bitmapped graphics and instead says hello to some new 3D models instead. A screenshot of the game with its new 3D skin can be seen in the following figure:

The Skiing project

The following sections describe some of the other more interesting changes to the game code for this chapter.

Migration to 3D

The first step was to change all position and velocity information from being 2D vectors to 3D vectors, which meant changing CIwVec2 instances to CIwFVec3 and making sure that the extra component in the vector was initialized.

Since we tend to think about the y axis being the height above the ground, the y component was used for height in the game too. However, since the skier and trees are stuck to the floor, this means the y component of all position vectors is always zero.

The game therefore scrolls the trees along the z axis and the camera is placed high in the air and orientated to look at the skier. This still provides the effect of the trees moving up the screen.

The second step was to get rid of all the old 2D textures and replace them with 3D models. Since the GameObject class dealt with all the rendering, all that had to be done was to change this class to use CIwModel instances instead of CIwMaterial instances. The child classes then just provided a pointer to a model instead of a pointer to the material.

The GameObject class also had a y axis rotation added to it. This is used to rotate the skier model, which leads to a far smoother motion than we had previously.

The trees also use the rotation feature. The game features only one tree model, but by rotating it at random angles it makes the game look far more interesting without having to add more game resources.

Addition of a collision detection system

The code now features a very simple collision detection system. The GameObject class now allows a collision radius to be set, which is then used to perform sphere intersection tests.

The ModeGame::Update method now steps through every game object (currently just trees, of course) in the game world and finds out how far away it is from the skier. If the distance is less than the combined collision radius of the skier and the other game object, a collision has occurred.

So, to act on these collisions, a virtual method called OnCollide has been added to the GameObject class. Child objects can override this class and then react accordingly whenever they collide with another object. The Skier class implements this method so that whenever the skier collides with a tree, the game is over.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.43.26