2015-10-19 61 views
0

嘿,我的問題是,在PhysicsHandler的onUpdate中,pSecondsElapsed經常從0.016 ...跳到0.038,這使得玩家在如此大的步驟中移動,看起來像玩家會落後。 這裏從的onUpdate的重要提示代碼:Andengine PhysicsHandler讓玩家看起來遲緩

@Override 
protected void onUpdate(final float pSecondsElapsed, final IEntity pEntity) { 
    if(this.mEnabled) { 
     /* Apply linear acceleration. */ 
     final float accelerationX = this.mAccelerationX; 
     final float accelerationY = this.mAccelerationY; 
     if(accelerationX != 0 || accelerationY != 0) { 
      this.mVelocityX += accelerationX * pSecondsElapsed; 
      this.mVelocityY += accelerationY * pSecondsElapsed; 
     } 

     /* Apply angular velocity. */ 
     final float angularVelocity = this.mAngularVelocity; 
     if(angularVelocity != 0) { 
      pEntity.setRotation(pEntity.getRotation() + angularVelocity * pSecondsElapsed); 
     } 

     /* Apply linear velocity. */ 
     final float velocityX = this.mVelocityX; 
     final float velocityY = this.mVelocityY; 
     if(velocityX != 0 || velocityY != 0) { 
      pEntity.setPosition(pEntity.getX() + velocityX * pSecondsElapsed, pEntity.getY() + velocityY * pSecondsElapsed); 
     } 
    } 
} 

BTW我只使用線速度。有沒有人有這個解決方案?謝謝你的幫助!

+0

你不能忽略* pSecondsElapsed *並把它乘以一些常量嗎? –

+0

@ m.antkowicz如果我會這樣做遊戲速度取決於fps – Hansfritzi

回答

0

我繼續前進,查看了這個引擎的存儲庫,發現一個可能對您有幫助的類。基礎引擎類有一個名爲Fixed Step Engine的擴展,它應該允許你控制每幀的增量時間,這可能值得嘗試(如果你還沒有)來獲得更流暢的物理。

+0

謝謝!這實際上有助於獲得持續的pSecondsElapsed,但是現在整個遊戲都顯着滯後,甚至連玩家都沒有順利移動 – Hansfritzi

0

與FixedStepPhysicsWorld組合,你會得到最好的結果,但:有一個小 「錯誤」 的FixedStepPhysicsWorld類中:

變化的onUpdate如下:

... 
while(this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) { 
    world.step(stepLength, velocityIterations, positionIterations); 
    this.mSecondsElapsedAccumulator -= stepLength; 
    stepsAllowed--; 
} 

this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed); 

... 
while(this.mSecondsElapsedAccumulator >= stepLength && stepsAllowed > 0) { 
    world.step(stepLength, velocityIterations, positionIterations); 
    this.mSecondsElapsedAccumulator -= stepLength; 
    stepsAllowed--; 
    this.mPhysicsConnectorManager.onUpdate(stepLength); 
} 

原始代碼中的問題是PhysicsWorld在多個步驟(大多數情況下)後更新PhysicsConnector。我的代碼修復了這個問題。每當UiThread繪製連接的實體時,它們總是處於其連接體的位置。他們不會跳過物理步驟。

+0

嘿,謝謝你的回答!但問題是我沒有使用Physics box 2D擴展。對不起,我沒提過那個。 – Hansfritzi