Hi,
I am struggling to implement a fixed time step and a box2d based world in my game.
Specifically I am referring to this article on fixed time step: http://gafferongames.com/game-physics/fix-your-timestep/
I found this implementation: http://plaincode.blogspot.com/2012/05/fixed-timestep-in-cocos2d-x-with-box2d.html
I am trying to implement physics based jumping using box2d.
Trying out my world with two different fixed time steps, 0.1 and 0.05 seconds, my player’s jump height is noticeably different.
This is the simplified jump logic:
if(this->isJumping() && this->hasContactBelow()) {
float desiredVelocity = 25.0f;
float velocityChange = desiredVelocity - currentVelocity.y;
float impulseY = this->body->GetMass() * velocityChange;
this->body->ApplyLinearImpulse(b2Vec2(0, impulseY), this->body->GetWorldCenter());
}
Alternatively, I’ve tried setting the velocity directly
this->body->SetLinearVelocity(b2Vec2(currentVelocity.x, 25.0f));
In both cases, depending on which fixed time step I use, the player jumps to a different height.
I’ve tried the linked implementation above, but was seeing bad results. I am a bit confused on how to properly implement the partial render step.
In the implementation above at plaincode.blogspot, they are calling the world step AFTER the manual positioning of the world bodies and attached Node objects.
When I tried that I was seeing the b2Body shake back and forth with the Node positioning lagging behind an iteration.
When I tried moving the world step BEFORE the calls to renderFullStep(), the shaking was gone, but my issue with the variable jump height between fixed time step values was still present.
The best I have currently is simply this:
void GameLayer::update(float dt) {
_accumulator += dt;
while(_accumulator > fixedTimeStep) {
_accumulator -= fixedTimeStep;
this->_world->Step(fixedTimeStep, 8, 1);
playerNode->update(fixedTimeStep);
}
}
Any help is greatly appreciated,
Thanks