Quantcast
Channel: cocos2d-x - Cocos Forums
Viewing all articles
Browse latest Browse all 2748

Do not understand box2d Step() and update interval when using cocos2d-x?

$
0
0

@nichlaspro132 wrote:

Hi!

When you simulate the physics in box2d, you are using Step().

But in the user manual it is used in a for loop like this and not as something that will be running all the time:

float32 timeStep = 1.0f / 60.0f;
int32 velocityIterations = 6;
int32 positionIterations = 2;
///////////////////////////////////////
for (int32 i = 0; i < 60; ++i)
{
world.Step(timeStep, velocityIterations, positionIterations);
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
Here, the float32 timeStep is always the same.

But when you are doing it in a scheduled updater function i see some do it like this:

void Level::update(float dt)
{
  // get current time double
  currentTime = getCurrentTimeInSeconds();
  if (!lastTickTime)
    lastTickTime = currentTime;
  // determine the amount of time elapsed since our last update
  double frameTime = currentTime - lastTickTime;
  accumulator += frameTime;
  // update the world with the same seconds per update
  while (accumulator > kSecondsPerUpdate)
  {
    accumulator -= kSecondsPerUpdate;
    // perform a single step of the physics simulation
    world->Step(kSecondsPerUpdate, 8, 1);
  }
  lastTickTime = currentTime;
}

And this

void HelloWorld::update(float dt){
   int positionIterations = 10;  
   int velocityIterations = 10;
   deltaTime = dt;
   world->Step(dt, velocityIterations, positionIterations);  
    world->ClearForces();
    world->DrawDebugData();        
}

In both examples they are running the Step function with an interval that is updating/changing all the time and isn't dt getting bigger and bigger all the time or at least changing.

So my question is - Why are they not using a constant value in the last two examples when the one from the user manual is and why? When do i have to do what??

Really hope you get me. smile

Thank you!

Posts: 5

Participants: 4

Read full topic


Viewing all articles
Browse latest Browse all 2748

Trending Articles