2012-04-01 8 views
6

Estoy intentando usar un Renderizador de depuración Box2D junto con mis Sprites y cuerpos LibGDX. El problema que tengo es que el renderizador dibuja el cuerpo de la caja en el centro de la pantalla y luego el sprite se dibuja en su ubicación predeterminada (0,0) en la parte inferior izquierda de la pantalla. Cuando muevo el Sprite del coche, tanto el carro como la caja de depuración se mueven, lo que hace que no se superpongan.Cómo usar cámaras LibGDX con Renderizadores de depuración Box2D

Sé que el problema es con la cámara porque he estado jugando con diferentes valores de la cámara durante un par de días. Algunas veces se superponen pero luego el cuerpo de depuración de Box2D se mueve más rápido que el Sprite de coche.

Algunas veces el cuerpo de Box2D se encuentra en la misma posición que el Sprite pero es extremadamente pequeño. Estoy usando 2 cámaras. Uno que es de 720 x 480. La cámara de depuración es en metros por lo que su, 24 x 16.

Aquí hay algo de código en la que el problema podría residir (estoy usando Etapas y Actores):

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: (también actor)

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

Y para m empeorar las cosas Realmente se complica cuando trato de hacer que la cámara principal siga al automóvil.

+0

ONE STEP CLOSER! Intenté aleatoriamente 'debugCam.unproject (battleStage.getCamera(). Position);' ¡y casi funcionó! La caja solo tiene unos pocos píxeles ahora. –

Respuesta

3

¿Qué hay de battleStage.getCamera().combined? Combinado funcionó bien para mí.

+0

lo intenté también, pero sigo teniendo un problema similar. –

0

Debe aplicar la siguiente declaración para combinar el cuerpo con la textura dibujada stage.setCamera (cámara);