Box2DDebugRenderer和SpriteBatch放错地方了

时间:2013-12-16 06:47:10

标签: java libgdx box2d

我正在玩libgdx制作另一个物理游戏:)我发现了一些奇怪的东西。即我使用SpriteBatch与Box2DDebugRenderer同时渲染图像进行调试。

但是当物理行为时,它们似乎是错位的。我写道:

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 个答案:

答案 0 :(得分:1)

我找到了。这是因为OpenGL中的旋转是围绕左下角完成的,而Box2D中的旋转是围绕质心的身体进行的。 在质心中心体周围旋转纹理可以产生正确的物理/纹理行为。

相关问题