Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a better fixed time step:</p> <pre><code>//FIX TIME STEP ///////---&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; const float32 FIXED_TIMESTEP = 1.0f / 60.0f; // Minimum remaining time to avoid box2d unstability caused by very small delta times // if remaining time to simulate is smaller than this, the rest of time will be added to the last step, // instead of performing one more single step with only the small delta time. const float32 MINIMUM_TIMESTEP = 1.0f / 600.0f; const int32 VELOCITY_ITERATIONS = 8; const int32 POSITION_ITERATIONS = 8; // maximum number of steps per tick to avoid spiral of death const int32 MAXIMUM_NUMBER_OF_STEPS = 25; ///////&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;------------------- //FIX TIME STEP-----------&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; -(void)afterStep { // process collisions and result from callbacks called by the step } -(void)step:(ccTime)dt { float32 frameTime = dt; int stepsPerformed = 0; while ( (frameTime &gt; 0.0) &amp;&amp; (stepsPerformed &lt; MAXIMUM_NUMBER_OF_STEPS) ){ float32 deltaTime = std::min( frameTime, FIXED_TIMESTEP ); frameTime -= deltaTime; if (frameTime &lt; MINIMUM_TIMESTEP) { deltaTime += frameTime; frameTime = 0.0f; } world-&gt;Step(deltaTime,VELOCITY_ITERATIONS,POSITION_ITERATIONS); stepsPerformed++; [self afterStep]; // process collisions and result from callbacks called by the step } world-&gt;ClearForces (); } //FIX TIME STEP&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;------------------ -(void) tick: (ccTime) dt { //It is recommended that a fixed time step is used with Box2D for stability //of the simulation, however, we are using a variable time step here. //You need to make an informed choice, the following URL is useful //http://gafferongames.com/game-physics/fix-your-timestep/ // Instruct the world to perform a single step of simulation. It is // generally best to keep the time step and iterations fixed. // world-&gt;Step(dt, velocityIterations, positionIterations); [self step:dt]; //Iterate over the bodies in the physics world for (b2Body* b = world-&gt;GetBodyList(); b; b = b-&gt;GetNext()) { if (b-&gt;GetUserData() != NULL) { CCSprite *myActor; myActor = (CCSprite*)b-&gt;GetUserData(); myActor.position = CGPointMake( b-&gt;GetPosition().x * PTM_RATIO, b-&gt;GetPosition().y * PTM_RATIO); myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b-&gt;GetAngle()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload