2012-04-01 54 views
6

我正在嘗試使用Box2D調試渲染器以及我的LibGDX Sprite和Bodies。我遇到的問題是渲染器在屏幕中心繪製箱體,然後在屏幕左下角(0,0)的默認位置繪製精靈。當我移動汽車精靈時,汽車和調試盒移動,使他們不重疊。如何在Box2D調試渲染器中使用LibGDX相機

我知道問題出在相機上,因爲我現在已經用不同的相機值搞亂了。有時它們重疊,但是隨後Box2D調試主體比Car Sprite移動的更快。

有些時候Box2D體與Sprite的位置相同,但非常小。我正在使用2臺攝像機。其中一個是720×480調試相機是米所以它的,24×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(); 
} 

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; 
} 

並以米事情變得更糟。當我試圖讓主攝像頭跟着車時,它真的變得很複雜。

+0

一步關閉!我隨機只是嘗試'debugCam.unproject(battleStage.getCamera()。position);'它幾乎工作!該盒子現在只有幾個像素。 – 2012-04-01 19:39:29

回答

3

battleStage.getCamera().combined怎麼樣?結合起來對我很好。

+0

也嘗試過,但我仍然遇到類似的問題。 – 2012-04-02 01:50:44

0

您必須應用以下語句才能將主體與繪製的紋理結合起來 stage.setCamera(camera);