2012-04-01 13 views
6

Sto tentando di utilizzare un Debug Renderer Box2D insieme ai miei Sprites e Corpi LibGDX. Il problema che sto avendo è che il Renderer disegna il corpo della scatola al centro dello schermo e quindi lo Sprite è disegnato nella sua posizione predefinita di (0,0) nella parte inferiore sinistra dello schermo. Quando sposto il Car Sprite, la mossa Car e Debug Box non si sovrappongono.Come utilizzare le fotocamere LibGDX con Renderer di debug Box2D

Conosco il problema con la macchina fotografica, perché da un paio di giorni sono in giro con valori diversi della videocamera. Alcune volte si sovrappongono, ma poi il corpo di debug di Box2D si muove più velocemente di Car Sprite.

Alcune volte il corpo di Box2D si trova nella stessa posizione dello Sprite ma estremamente piccolo. Sto usando 2 telecamere. Uno che è 720 x 480. La fotocamera è in Debug metri per cui il suo, 24 x 16.

Ecco po 'di codice in cui il problema potrebbe risiedere (sto usando Fasi e Attori):

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: (anche attore)

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

E per m fare le cose peggio Diventa davvero complicato quando provo a far seguire la macchina dalla macchina fotografica principale.

+0

ONE STEP CLOSER! Ho provato a caso 'debugCam.unproject (battleStage.getCamera(). Position);' e ha quasi funzionato! La scatola è ora spenta di pochi pixel. –

risposta

3

Che dire di battleStage.getCamera().combined? Combinato ha funzionato bene per me.

+0

ha provato anche questo, ma ho ancora un problema simile. –

0

si deve applicare seguente dichiarazione di coniugare il corpo con la texture disegnato stage.setCamera (macchina fotografica);

Problemi correlati