Box2D and Scene2D Camera issue

Discuss issues specific the Java port of Box2D
kilosi
Posts: 2
Joined: Sun Mar 15, 2015 11:37 am

Box2D and Scene2D Camera issue

Postby kilosi » Sun Mar 15, 2015 1:59 pm

Edit: I've managed to find the solution to my problem.

Here were the changes I needed to do:
1. Change the constructor call on the stage to this:
super(new ScalingViewport(Scaling.stretch, SCREEN_WIDTH, SCREEN_HEIGHT,
new OrthographicCamera(SCREEN_WIDTH*WorldConstants.PIXELS_PER_METER, SCREEN_HEIGHT*WorldConstants.PIXELS_PER_METER)));

2. Input a scaling to the TMX Renderer:
tmr = new OrthogonalTiledMapRenderer(tileMap, 1f/WorldConstants.PIXELS_PER_METER);
Note: Make sure the 1 is a '1f' in order to get the proper precision on the divde.

Now, everything works just fine. May others who have issues with my described problem find this helpful.


-------------------------------------------------------------------------------

Hello

I am making a scrolling game using LibGDX's Scene2D, Box2D and TileMap. I am having an interesting issue with the cameras. I suspect I made some error causing the confusing.
(Code snippets will be provided below)

What I want to have happen is:
when the player moves, the camera updates its position so they are approx in the center of the screen.
I have a camera specifically for Box2D.
The camera for the stage is the one created by the constructor.

The issue is as follows:
If I am only using the Box2D Debug Render everything I want to do works. I see the debug shapes and I can move my character right or left and the camera updates properly.
If I am not using the Box2D Debug Render, I now see my tiles from TileMap and my Actors that have graphics on them and everything lines up properly with Box2D's collisions, etc. The issue is that, the camera does not properly move itself. The behavior is that it very slowly jitters and inches along extremely slowly - so slow the character can run off of the screen/camera's view port.

In the Stages render method, here are the code snippets:

Code: Select all

@Override
   public void draw ()
   {
                //WORKS fine.
      /*this.box2DCamera.position.set(
      actor.getX() / WorldConstants.PIXELS_PER_METER,
      box2DCamera.position.y, 0);

      this.box2DCamera.update();*/
      
                //Update the scenes camera so it focuses on the player
      this.getCamera().position.set(
            (actor.getX() / WorldConstants.PIXELS_PER_METER), //DOES NOT WORK
            getCamera().position.y, 0);
      
      this.getCamera().update();
      
      super.draw();
      
                //Render tile map
      tmr.setView((OrthographicCamera)(this.getCamera()));
      tmr.render();
      
      //debugRenderer.render(world, box2DCamera.combined);
      
      spriteBatch.begin();
      font.draw(spriteBatch, "FPS:" + Gdx.graphics.getFramesPerSecond(), 12, SCREEN_HEIGHT - 30);
      spriteBatch.end();
      
   }



On a somewhat related note - for some reason I cannot use the debug renderer along with the Scene2D's drawing functionality. If I try to use both, it breaks and and the Tile Map is drawn huge - as if it is scaled up to epic proportions. I wonder if I did something that is causing this and ultimately my problem mentioned above. I suppose if I could make it so the Tile Map is not drawn so huge I could use the Box2D Camera - since that works.

Here is the code for the Stage constructor and the camera for Box2D:

Code: Select all

public Box2DStage(MyScene scene, InputListener inputListener)
   {
      super(new ScalingViewport(Scaling.stretch, SCREEN_WIDTH, SCREEN_HEIGHT,
            new OrthographicCamera(SCREEN_WIDTH, SCREEN_HEIGHT)));
      
      this.scene = scene;
      this.inputListener = inputListener;
      
      initializeBox2DTiledWorld("maps\\test2.tmx");
      
      initializeBox2DActors();
      
                //DEBUG camera
      box2DCamera.setToOrtho(false, Box2DUtil.screenToWorld(SCREEN_WIDTH), Box2DUtil.screenToWorld(SCREEN_HEIGHT));
      
      //debugRenderer.setDrawContacts(true);
      
   }


Box2DUtil.screenToWorld is simply dividing the input value by 100.

Thanks for any help in fixing my issue. I realize this is a bit complicated so feel free to ask for any additional information.

Return to “Java”



Who is online

Users browsing this forum: Google [Bot] and 2 guests