LibGDX: Scene2D with Box2D

Here is another LibGDX demo. This one uses Box2D to build a simple model of a box with a gear cog spinning in the centre. If you click on the screen a ball is released which will drop into the view and bounce off the gear and walls. I am no expert in Box2D but I think you may find this useful if you are trying to combine it and LibGDX Scene2D elements.

Game Test

This demo is heavily indebted to the ideas from the demo provided for the Box2D editor which I used to create the polygon model for the central cog.

Few things of note to point out:

Here is a screenshot of the debug rendered ‘world’.

Game Test


The demo code is licensed under the same license as LibGDX itself with Apache 2.0.

Demo code here.


16 Responses to “LibGDX: Scene2D with Box2D”

  1. LeVulcain on March 7th, 2012 2:01 pm

    Thank you for this example: it’s a real programming lesson !
    The scene2d package is very convenient to organize the code !
    I’ll be able to organize my code from now.

  2. Julian on March 13th, 2012 8:28 am

    Thank you for this demo, this was exactly what I was searching for. A great way to combine Box2D with Scene2D.
    Very useful!

  3. junkdog on May 2nd, 2012 12:44 pm

    This was some the clearest, easiest to understand and well structured code I’ve seen. Too many “tutorial” projects tend to be hampered by spaghetti code.

    So, thanks for the great demos/tutorials!

  4. Aveblein on August 26th, 2012 7:31 pm

    any instructions on how to configure the project to properly run it?
    I have imported the 3 projects into Eclipse, cleaned them project so the ADT plugin generates the gen folder again, set Java Compiler to 1.6 but it still refuses to run, maybe I am missing references?

  5. admin on August 27th, 2012 8:38 am

    It should definitely run okay on both the desktop and android configurations. You should be able to right click on the main app and run as ‘java’ application. Are you getting any error messages?

  6. Miresnare on February 23rd, 2013 7:35 pm

    I’m having trouble getting the project imported too, is there any way you could check it out (if you have the time) as this demo looks to be pretty much exactly what I’m looking to do in an upcoming project?

    Many thanks if you could.

  7. admin on February 23rd, 2013 11:02 pm

    Miresnare, I am getting errors for the Android project but it might be because I am trying to compile against Java 7. Can you tell me

    – Is it only the Android version you are having problems with?
    – What platform (windows, osc etc)?
    – What JDK you are using?

    We’ll get this working I promise.


  8. Miresnare on February 24th, 2013 7:39 am

    Hi again.

    Window 7 Home premium
    I extract the zip file to desktop.
    Open eclipse (juno)
    File -> Import -> Existing projects
    Select root -> Browse
    Navigate to desktop, select box2d-test
    Import all three projects
    Projects -> Clean
    (Ignoring android atm, java 6 may be the fix for that)
    right click box2d-test-desktop, run as java application
    Select AppTestDesktop
    (errors exist, proceed)
    Error: Could not find or load main class

    Aha! Got the desktop working by commenting out: –
    in box2d-test-desktop .classpath

    in netthreads-libgdx

    It also works fine in android even with 1.7


  9. Miresnare on February 24th, 2013 7:48 am

    Sorry, your sanitisation stripped the tags

    classpathentry kind=”var” path=”M2_REPO/junit/junit/4.5/junit-4.5.jar”
    classpathentry kind=”var” path=”M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16.jar


    classpathentry kind=”var” path=”M2_REPO/junit/junit/4.5/junit-4.5.jar”

  10. admin on February 24th, 2013 11:28 am

    Oh! That’s strange. I will need to investigate this.Thanks for finding this and pointing it out.

  11. Miresnare on February 24th, 2013 7:01 pm

    No problem.

    Nice tutorial BTW. It’s very clean code and well structured. Exactly what I was looking for. Many thanks.

  12. milkotv on April 25th, 2013 10:11 pm

    Beautiful code – don’t see that often …

  13. arv on September 10th, 2013 11:16 am

    The code structure is nice….it is what exactly i am looking for.
    But i am unable to import it.Please help me.
    File -> Import -> Existing projects
    Select root -> Browse ->select box2d-test then…..
    No Project are found (inside box2d-test) is coming.

  14. admin on September 10th, 2013 3:29 pm

    Hi Arv, The project uses Maven to define its dependencies so ensure you have Maven installed and on your command path.

    There is more here.

    Basically if you run the maven command to generate an Eclipse project you will be able to import it. The basic command is ‘mvn eclipse:eclipse’ (no quotes). Once imported you may have to define the M2_REPO Eclipse variable to ensure your Eclipse can see the M2 repository files.

  15. Fayçal on November 23rd, 2016 5:55 pm

    Hi, no gradle for the project ? I have some issues 🙁

  16. admin on November 23rd, 2016 7:32 pm

    Yeah I am working on it. Hopefully I will have converted all the projects over pretty soon.