How to use LibGDX cameras with Box2D Debug Renderers

I am trying to use Box2D Debug Renderer with my lyrics and LibGDX bodies. The problem I am facing is that Renderer draws the body of the box in the center of the screen, and then Sprite draws by default the default (0,0) in the lower left corner of the screen. When I move the Car Sprite, the movement of the Car and Debug Box makes them not overlapping.

I know that the problem is with the camera, because for several days I was messing around with different camera values. In some cases, they overlap, but then the Box2D Debug Body moves faster than the Car Sprite.

Several times, the Box2D is in the same position as the Sprite, but very small. I use 2 cameras. One of them is 720 x 480. The debugging camera is in meters, so its 24 x 16.

Here is some code where the problem might occur (I use Stages and Actors):

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: (also 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; } 

And so that everything worsens. It really gets complicated when I try to get the main camera to follow the car.

+6
source share
2 answers

What about battleStage.getCamera().combined ? The combo worked great for me.

+3
source

You must apply the following instruction to combine the body with the texture drawn by stage.setCamera (camera);

0
source

Source: https://habr.com/ru/post/912186/


All Articles