Things to know about LibGDX

Hey everyone,

I just started porting over Crate Crash to libgdx to get it working on Android and through this process I’ve learned a few things.

  1. The coordinate system in LibGDX is in the bottom left, instead of the top left. This means that most things will be flipped if you port them over for a 2D project. Luckily, you can change the camera around to display things so you can provide values in a typical top-left scenario.
    1. camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      // setToOrtho (bool yIsDown, width, height)
      camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  2. Due to flipping the coordinate system, the images then need to be flipped to accommodate the newly flipped camera.
    1. If using Sprites:
      mySprite.flip(false, true); // flip(bool x, bool y)
  3. Within LibGDX they have a physics engine. The physics engine that is being used is Box2D and unlike the normal distributions of Box2D, the pixel to meter ratio is 1:1 instead of the usual 30:1. Switching over to this is not that difficult and requires small changes if the swap is made early into the project
  4. By default, the version of LibGDX that I got uses OpenGL 1.x. This is only problematic if you want to use textures that are not in a power of two dimension. If you are using Android 1.6 or older (Because many devices that have 1.6 are very old), then you may not be able to use OpenGL 2. However, this is a minor limitation because 99% of devices used today are OpenGL 2 compatible.
  5. Handling multiple resolutions is really easy once you know the basic idea behind it. There are a few options for cross platform stability and display and one of them is to force the same aspect ratio on each device. By doing this you will effectively force the game to show black bars around the perimeter of the game screen. There is however one problem that comes from that: Mouse touch positions don’t line up. I wrote a function that returns the mouse position for the active game area, and ensures that the position is properly scaled. To achieve the black bar (gutter) effect, try the following in your main resize event:
    	    // Game Helper screen width/height holds the resolution we want the game to always be displayed in
    	    Vector2 size = Scaling.fit.apply(GameHelper.ScreenWidth, GameHelper.ScreenHeight, width, height);
    	    int viewportX = (int)(width - size.x) / 2;
    	    int viewportY = (int)(height - size.y) / 2;
    	    int viewportWidth = (int)size.x;
    	    int viewportHeight = (int)size.y;
    	    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);

    This will force your view to always have the proper aspect ratio no matter what. I have also learned that while doing this, and using the Stage object (for menus and UI position), you need to update the active size of the that as well, otherwise it won’t scale the right way and the stage could be off the screen.

While LibGDX does abstract a lot of the native code, preventing you from working primarily in pure Android, it does provide a very solid building block to start from. Starting with LibGDX is really easy and really straight forward. It’s also cross system compatible for iOS, Desktop, and  HTML5. I don’t know how the compilation schemes work, but I’m guessing that it’s fairly optimized.

One thought on “Things to know about LibGDX”

Leave a Reply

Your email address will not be published. Required fields are marked *