Time for action – setting up the world step loop
We will make the world time advance by carrying out the following steps:
- In order to advance the world step, we have to call the
step
function in the world instance periodically. We usedsetTimeout
to keep calling thestep
function. Put the following function in our JavaScript logic file:function updateWorld() { // Move the physics world 1 step forward. carGame.world.Step(1/60, 10, 10); // display the build-in debug drawing. if (shouldDrawDebug) { carGame.world.DrawDebugData(); } }
- Next, we will set up an interval in the
initGame
method:setInterval(updateWorld, 1/60);
- We will again simulate the world in a browser. The box is created at the initialized position and falls on the ground correctly. The following screenshot shows the sequence of a box dropping on the ground:
What just happened?
We have advanced the time of the world. Now, the physics library simulates the world in a frequency of 60 times per second. In the...