2013-12-16 24 views
1

我正在玩libgdx製作另一個物理遊戲:)我發現了一些奇怪的東西。即,我使用SpriteBatch與Box2DDebugRenderer同時渲染圖像進行調試。Box2DDebugRenderer和SpriteBatch錯位

但是,當物理行爲,他們似乎是錯位。我寫道:

public class Canon implements ApplicationListener { 
    private OrthographicCamera camera; 
private Box2DDebugRenderer debugRenderer; 

    /... 
public void create() {    
    camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT); 
    world = new World(new Vector2(0f, -9.8f), true); 
     camera.position.set(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, 0f); 
     camera.update();   
    debugRenderer = new Box2DDebugRenderer(); 
    spriteBatch = new SpriteBatch(); 
     //Create a canon. A rectangle :) 
     bd = new BodyDef(); 
    fd = new FixtureDef(); fd.density = 1; 
    PolygonShape ps = new PolygonShape(); 

    // Cannon 
    bd.type = BodyDef.BodyType.StaticBody; 
    bd.position.set(new Vector2(8, 5)); 
    ps.setAsBox(5f, 1f); 
    cannonBody = world.createBody(bd); 
    fd.shape = ps; 
    cannonBody.createFixture(fd); 
    } 

@Override 
public void render() {  
    Gdx.gl.glClearColor(0, 0, 0, 1); 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

    debugRenderer.render(world, camera.combined); 
    world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS); 
     spriteBatch.begin(); 
     Sprite s = (Sprite)targetBody1.getUserData(); 
     spriteBatch.draw(s.getTexture(), 
      (targetBody1.getPosition().x - bodyWidth/2)*ppuX,   (targetBody1.getPosition().y - bodyheight/2)*ppuY, 
      0f, 0f, bodyWidth*ppuX, bodyheight*ppuY, 1f, 1f, radToGrad*targetBody1.getAngle(), 0, 0, s.getTexture().getWidth(), s.getTexture().getHeight(), false, false); 
    spriteBatch.end(); 

} 
} 

enter image description here

而且這裏是它的後enter image description here

看起來如何任何想法?

謝謝!

+1

您是否更新過SpriteBatch,以使用與DebugRenderer一起使用的相機? –

+0

你好,Rob,thans你的回答。我已經完成了這個debugRenderer.render(world,camera.combined);但是,正如你指出的那樣,SpriteBatch沒有相機的知識(矩陣)。 –

回答

1

我找到了。這是因爲OpenGL的旋轉是在左下角附近完成的,而Box2D的旋轉是在質心的身體周圍完成的。 在質心體周圍旋轉紋理會產生正確的物理/紋理行爲。

+2

如果這是您的答案,請接受它從未答覆的問題列表中刪除此問題, – Sinkingpoint

+0

確定如何完成此操作?一旦我說出答案,它不是自動的? –

+2

點擊答案旁邊的勾號。 – Sinkingpoint