Stepping into the world

Once the world has been created, the simulation must actually be run. You can do this by calling the Step function of the b2World class, usually in the update function of your game, as follows:

world_->Step(dt, 8, 3);

The three parameters passed to the Step function are the time-step, the number of velocity iterations, and the number of position iterations, respectively. The variable dt passed into the Step function is passed to us from the update function of our GameWorld class, and the value held by dt is that of a variable time-step. We use the term variable to describe this value because the amount of time the Cocos2d-x engine will take to finish one single tick might vary continuously.

One drawback of using a variable time-step is that it makes the entire simulation hardware dependent. Thus the simulation might run faster on a machine having a powerful configuration, while it might run significantly slower on a machine having a poor configuration. Considering that this game is not too heavy graphically, we can assume that we will get a steady 60 FPS and thus we shall stick with the variable time-step.

The second argument, that is, velocity iterations, basically describes the number of times the simulation will process velocity for all moving bodies within a single time-step. The same goes for the third argument, that is, position iterations, which is the number of times the simulation will process position values within a single time-step. The values for velocity and position iterations suggested in the Box2D manual have been used for our game but you can tweak these values. It should suffice to keep in mind that there is a trade-off between speed and accuracy involved. Larger values for iterations assure a more accurate simulation but incur a performance cut.

Now that we have understood how to create and run the simulation, let's create and add some of our game's bodies and see some action.

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

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