Box2d/Libgdx Example Code

Hi, We are uploading source code of a simple box2d-libgdx example. Here is the File In the attachment you would find three projects :-

  • BoxLibgdxExample
  • BoxLibgdxExample-desktop
  • BoxLibgdxExample-android

UPDATE :- The example now contains box2d debug renderer also which can be used to render box2d objects.

Copy them in your eclipse workspace and import the projects in the following way:

  • File->Import->General->Existing Projects into Workspace
  • Then set root directory by browsing to the directory where the above three folders were copied. Then the above three projects would be seen listed.
  • Then click Finish.
Example1
Example2

Finally you would have to a link source of android project to desktop project so that both projects use same source of assets. Right click on  BoxLibgdxExample-desktop in Package Explorer in Eclipse and in Build Path go to Link Source. Browse to the assets folder of BoxLibgdxExample-android and then build all the projects. Example3 Run the desktop project as Java Application and select DesktopGame.java. That should run the example.

For Android Code just check if the build path matches the below screenshot.

Build Path

If you have any issues please post the problem in comments and we would try to solve it. Thank you for the patience

Tagged with: , , , , ,
14 comments on “Box2d/Libgdx Example Code
  1. JG says:

    thanks for this example, been able to play around with the desktop example but the android one gives the error build path incomplete and says “cannot find class file for java.lang.object. Fix the build path then try building this project.” also it gives the project properties error but I know how to fix that one
    sorry if this obvious I am still trying to learn

    • DTAdmin says:

      Hi,

      Can you check the build path screen matches the screenshot we just posted. If after that also you get the error can you post the stack trace here in the comment?

      • JG says:

        i think this may be a problem with my setup of eclipse because I have all of the same build path except on the android 2.2 and android dependencies it says “unable to get system library project” for android 2.2 and for android dependencies its says “com..android.android.ide.eclipse.adt.LIBRARIES”. I dont think I can give you a stack trace because I cant actually run it because of the error in the program means that when I click run as android app it says fix errors first. what I might try is to create a new project and then copy all of the code from the android example and then link it up with the rest of them and sort out the build path and see if that works, because I will able to set up the android 2.2 and dependencies. i WILL post if it works thanks for quick reply.

      • JG says:

        yeah managed to get it to work by creating a new android project and then setting it up. for some reason when I imported it into my workspace the android 2.2 libraries werent coming with it, I have this problem when looking at different examples, easiest way to fix is to create a new project and then copy in the code. thanks for offering to help

  2. JG says:

    Another question I have is how you learnt java and in general making games and what books you may have read because I am really interested but am really struggling. for example when I got your example to work i don’t really understand where to actually put the code to make something happen.
    E.G. taking the example of the ball bouncing and then adding code so that when an area on the screen is touched the ball’s velocity is changed and it moves in a different direction.

    I know its a lot to ask but if you could show me how this done either through a tutorial or a download i would be grateful. however if you don’t have time I would just be happy to know how you learnt.

  3. Wing says:

    Do you have an example how the debug renderer? If I put the box2ddebugrenderer in your code, the debugrenderer are misplaced and tiny. It didn’t scaled along with the ratio.

    • Rahul Srivastava says:

      Currently we dont have an example for adding debug renderer.

    • Rahul Srivastava says:

      We have updated the older example which now contains the debug renderer.
      The reason for the objects coming mispaced and tiny is because the objects are scaled down with respect to world coordinates. So if you can scale the matrix used by debug render by a factor 100 (as the box objects in the example are scaled down by 100) then the objects should look fine.
      Thanks

  4. How to exactly enable debug render? What lines should be commented? When I do nothing, then game is being rendered with textures. If I comment texture rendering in render method, then I see just black screen 🙁

  5. But is it possible to change border color?

2 Pings/Trackbacks for "Box2d/Libgdx Example Code"
  1. […] setting the example in eclipse please look for instructions in this […]

  2. […] This is an update to the older example (“using Box2d in Libgdx”) we put in the post here. […]

Leave a Reply