2011-08-08 9 views
1

80個のBox2dオブジェクトを持つiOS cocos2dアプリケーションで40 FPSで動作しています。私は固定タイムステップを使用しています:cocos2d FPS(Fixed Fimed TIMESTEP)を使用して60 FPSで動作しない

-(void) update: (ccTime) dt 
{ 
if(gameState_ != kGameStatePaused) { 
world_->Step(dt, 6, 1); 

.... 
} 

どうしてですか?ありがとう。

+0

あなたはBOX2Dまたはcocos2d固定タイムステップを使用していますか? – Carter

+0

毎フレームで16ms以上のCPU時間を使用していますか? –

答えて

1

これは、より良い固定時間ステップである:

//FIX TIME STEP 
///////--->>>>>>>>>>>>> 
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; 
///////<<<<<<<<<<<<<<<<<<------------------- 

//FIX TIME STEP----------->>>>>>>>>>>>>>>> 
-(void)afterStep { 

    // process collisions and result from callbacks called by the step 
} 


-(void)step:(ccTime)dt { 
    float32 frameTime = dt; 
    int stepsPerformed = 0; 
    while ((frameTime > 0.0) && (stepsPerformed < MAXIMUM_NUMBER_OF_STEPS)){ 
     float32 deltaTime = std::min(frameTime, FIXED_TIMESTEP); 
     frameTime -= deltaTime; 
     if (frameTime < MINIMUM_TIMESTEP) { 
      deltaTime += frameTime; 
      frameTime = 0.0f; 
     } 
     world->Step(deltaTime,VELOCITY_ITERATIONS,POSITION_ITERATIONS); 
     stepsPerformed++; 
     [self afterStep]; // process collisions and result from callbacks called by the step 
    } 
    world->ClearForces(); 
} 
//FIX TIME STEP<<<<<<<<<<<<<<<<<------------------ 


-(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->Step(dt, velocityIterations, positionIterations); 
    [self step:dt]; 

    //Iterate over the bodies in the physics world 
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
    { 
     if (b->GetUserData() != NULL) { 

      CCSprite *myActor; 
      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()); 

     } 
    } 
} 
1

現在の受け入れ答えはいくつかの問題を持っている...私はそれを試してみましたが、カメラがそれに追従し、私の高速スクロールのゲームで私は、メインキャラクターのためのジッタを見たとき。私はこのコードの詳細(なぜそれが間違っている)、ここでhttp://box2d.org/forum/viewtopic.php?t=5619

を見つけた、私は、補間して、より強力なソリューションを使用することをお勧めだと思う:http://plaincode.blogspot.com/2012/05/fixed-timestep-in-cocos2d-x-with-box2d.html

関連する問題