如何将LibGDX相机与Box2D Debug Renderers一起使用

Fre*_*cer 6 java graphics android box2d libgdx

我正在尝试使用Box2D Debug Renderer以及我的LibGDX Sprites和Bodies.我遇到的问题是渲染器在屏幕中央绘制Box Body,然后Sprite在屏幕左下角的默认位置(0,0)绘制.当我移动Car Sprite时,Car和Debug Box都移动,使它们不重叠.

我知道问题出在相机上,因为我现在已经搞乱了不同的相机值几天了.有时它们会重叠,但Box2D Debug Body的移动速度比Car Sprite快.

有时Box2D机身与Sprite处于同一位置,但非常小.我正在使用2台相机.一个720 x 480.调试摄像头以米为单位,因此它是24 x 16.

这里有一些问题所在的代码(我正在使用阶段和演员):

BattleScreen.java:

public void show() {
    battleStage = new Stage( 720, 480, false );
    // The Box2D Debug Renderer will handle rendering all physics objects for debugging
    debugRenderer = new Box2DDebugRenderer( true, true, true, true );
    debugCam = new OrthographicCamera( 24, 16 );
}
public void render() {

    // Set the Camera matrices
    battleStage.getCamera().update();       

    // Update the Physics World, use 1/45 for something around 45 Frames/Second for mobile devices
    physicsWorld.step( 1/45.0f, 8, 3 );     // 1/45 for devices 

    // Again update the Camera matrices and call the debug renderer
    //debugCam.update();
    debugRenderer.render( physicsWorld, debugCam.combined );

    // Update all Game Objects then Draw them
    battleStage.act(delta);
    battleStage.draw();
}
Run Code Online (Sandbox Code Playgroud)

Car.java :(也是演员)

public Car(Texture texture ) {
    super( "Car" ); 

    mSprite = new Sprite( texture );
    mSprite.setSize( 54, 105 );

    mSprite.setOrigin( mSprite.getWidth()/2, mSprite.getHeight()/2);    // set the origin to be at the center of the body

    FixtureDef carFixtureDef = new FixtureDef();
    mBody = Physics.createBoxBody( BodyType.DynamicBody, carFixtureDef, mSprite );
}

public static Body createBoxBody( final BodyType pBodyType, final FixtureDef pFixtureDef, Sprite pSprite ) {

    final BodyDef boxBodyDef = new BodyDef();
    boxBodyDef.type = pBodyType;

    // Temporary Box shape of the Body
    final PolygonShape boxPoly = new PolygonShape();
    final float halfWidth = pSprite.getWidth() * 0.5f / Consts.PIXEL_METER_RATIO;
    final float halfHeight = pSprite.getHeight() * 0.5f / Consts.PIXEL_METER_RATIO;
    boxPoly.setAsBox( halfWidth, halfHeight );  // set the anchor point to be the center of the sprite
    pFixtureDef.shape = boxPoly;

    final Body boxBody = BattleScreen.getPhysicsWorld().createBody(boxBodyDef);
    boxBody.createFixture(pFixtureDef);
    boxPoly.dispose();
    return boxBody;
}
Run Code Online (Sandbox Code Playgroud)

并使事情变得更糟.当我试图让主摄像头跟随汽车时,它真的变得复杂了.

小智 3

关于什么battleStage.getCamera().combined?结合起来对我来说效果很好。