Hello,
I'm having a hard time trying to fix the timestep using both of the main physics engines. I'm following the famous http://gafferongames.com/game-physics/fix-your-timestep/ and some topics I found around here.
Given Chipmunk template that comes with cocos, I changed the step to the following:
-(void) stepFixed: (ccTime) delta
{
float fixedTimeStep = 1.0/60.0f;
timeAccumulated += delta;
while(timeAccumulated >= fixedTimeStep) {
CGFloat dt = fixedTimeStep/(CGFloat)2;
for(int i=0; i<2; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
timeAccumulated -= fixedTimeStep;
}
}
And this version:
-(void) step: (ccTime) delta
{
float fixedTimeStep = 1.0/60.0f;
float timeToRun = delta + timeAccumulated;
while(timeToRun >= fixedTimeStep) {
CGFloat dt = fixedTimeStep/(CGFloat)2;
for(int i=0; i<2; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
timeToRun -= fixedTimeStep;
}
timeAccumulated = timeToRun;
}
Box 2D:
-(void) tick: (ccTime) dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
float fixedTimeStep = 1.0/60.0f;
float timeToRun = dt + timeAccumulated;
while(timeToRun >= fixedTimeStep) {
world->Step(fixedTimeStep, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
timeToRun -= fixedTimeStep;
}
timeAccumulated = timeToRun;
}
- Everything starts and runs ok. I add up to 14 sprites and the framerate keeps stable. Until I add 15+ sprites, then the frame rate drops to 1.0 ~ 5.0.
- Testing on an iPod Touch with iOS4.
- Wierd thing is: it runs smoothly with the non-fixed-step version (template's default code), even when I add up to 40-50 sprites.
Thanks in advance.