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.
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. 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.
If you have any issues please post the problem in comments and we would try to solve it. Thank you for the patience
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
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?
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.
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
Thanks for letting us know. It may help some other people also who may have similar problems.
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.
Hi,
For java you can refer to the book “Java the complete reference”. It is a really good book to understand java.
And regarding the tutorial are you asking for a post on changing ball’s velocity when screen is touched in the previous example? Actually we had posted a tutorial on how to do this but it was not in box2d but rather a general tutorial here -> http://decisiontreegames.wordpress.com/2012/03/04/game-development-in-android-using-libgdx-part-viii/
because we had a similar functionality in our game polar .
See if it helps.
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.
Currently we dont have an example for adding debug renderer.
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
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 🙁
Sorry. I’ve worked this out. I didn’t see black border around objects. 🙂
But is it possible to change border color?
I am not sure if it is possible to change border color. If you check the source code (https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/physics/box2d/Box2DDebugRenderer.java), you can see the colors defined are private final and function getColorByBody() is also private.