2013-04-24 33 views
2

我正在開發一個libgdx和box2d的Android遊戲。 我的問題是box2d中的物體插值效果不好......身體有點臃腫。如果沒有內插,身體就會「不起眼」。 以下是我的部分代碼:libgdx box2d插值效果不好

public void gameRunning() 
{ 
    mAccumulator += Gdx.graphics.getDeltaTime(); 

    if(mAccumulator > 1f) 
    { 
     mAccumulator = 1f; 
    } 

    while(mAccumulator >= BOX_STEP) 
    { 
     resetSmooth(); 
     mWorld.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS); 
     mAccumulator -= BOX_STEP; 
    } 

    mWorld.clearForces(); 
    smooth(); 
} 

public void smooth() 
{ 
    float ratio = mAccumulator/BOX_STEP; 
    float oneMinusRatio = 1.f-ratio; 

    mSmoothedX = ratio*mBowl.getPosition().x+oneMinusRatio*mPreviousX; 
    mSmoothedY = ratio*mBowl.getPosition().y+oneMinusRatio*mPreviousY; 

    mBowl.setTransform(mSmoothedX, mSmoothedY, 0f); 
} 

public void resetSmooth() 
{ 
    mSmoothedX = mPreviousX; 
    mSmoothedY = mPreviousY; 

    mPreviousX = mBowl.getPosition().x; 
    mPreviousY = mBowl.getPosition().y; 
} 

問題在哪裏? 對不起,我英語不好,並提前感謝... :)

+0

爲什麼'如果(mAccumulator> 1F)'塊? – 2015-02-17 14:54:28

回答

1

你不應該移動box2d這樣的身體,而是應用力量/衝動。否則,它們在物理模擬中將無法正確運行。

此外,你的插值實現對我來說似乎很奇怪,你爲什麼不使用Interpolation class

實施例:

mSmoothedX = Interpolation.linear.apply(startx, endx, <time>);