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.

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:
- Each body in the world has an associated Actor sprite. I am using an Action to map the position and rotation of each body to the linked Actor. The conversion from Box2D position to pixels is by a conversion factor (the pixel width / the Box2D world width).
- Bodies loaded from the Box2D editor have their position-origin on the bottom left corner. The bodies created from the Box2D ‘primitives’ are positioned at their centre. Handling this inconsistency is a bit of a pain and makes the code a lot less elegant.
- When you close the view (select the BACK key for Android or ESC for Windows) the world is not destroyed, only the balls are marked for removal and subsequent clean-up. When you close the application it will re-claim the memory used.
- There is actually two demos in the source. Under the ‘test’ package you will find a view which uses the LibGDX Box2D debug renderer. You kind of need this to build a ‘world’ before trying to map Scene2D elements to it.
- When you click on the screen fading text is displayed with the pixel co-ordinates. This is just for fun and to show off the sort of things you can do with actions.
Here is a screenshot of the debug rendered ‘world’.

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

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.
March 7, 2012 @ 2:01 pm
Thank you for this demo, this was exactly what I was searching for. A great way to combine Box2D with Scene2D.
Very useful!
March 13, 2012 @ 8:28 am
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!
May 2, 2012 @ 12:44 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?
August 26, 2012 @ 7:31 pm
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?
August 27, 2012 @ 8:38 am
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.
February 23, 2013 @ 7:35 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.
AL.
February 23, 2013 @ 11:02 pm
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 com.netthreads.libgdx.app.AppTestDesktop
Aha! Got the desktop working by commenting out: -
in box2d-test-desktop .classpath
and
in netthreads-libgdx
It also works fine in android even with 1.7
Thanks!
February 24, 2013 @ 7:39 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
and
classpathentry kind=”var” path=”M2_REPO/junit/junit/4.5/junit-4.5.jar”
February 24, 2013 @ 7:48 am
Oh! That’s strange. I will need to investigate this.Thanks for finding this and pointing it out.
February 24, 2013 @ 11:28 am
No problem.
Nice tutorial BTW. It’s very clean code and well structured. Exactly what I was looking for. Many thanks.
February 24, 2013 @ 7:01 pm
Beautiful code – don’t see that often …
Thanks!
April 25, 2013 @ 10:11 pm