Hello, my name is Jhonny, I am from Brazil. I don’t write or speak english well, but I sure that you will understand me. I am developing a game and I met Libgdx searching in the web. I want learn about physics in libgdx. Can you help me with a simple example about this? Thanks and sorry for my english, I am trying learn this also. :).
Hi,
We have written some tutorials which can be helpful for you. For physics best option is to use box2d library which comes with libgdx. You can start with the post “Using Box2d in Libgdx Game Part I” (http://rotatingcanvas.com/using-box2d-in-libgdx-game-part-i/) . And also we have posted a basic example in here (http://rotatingcanvas.com/box2dlibgdx-example-code/ ) which you can download and setup in eclipse and see how physics works in it. It is a simple bouncing ball program.
With this code only the physics body/shape is being created. To show it on screen we need to attach a texture to it. By attaching a texture what i mean is just setting position of texture to the body position in world coordinates and updating it in every frame to match the body position.
We added a widget from where you can subscribe the blog my email.
Regarding tutorials we write new ones whenever we think we had used something new which we did not put in tutorials before. We intend to add new tutorials in future whenever we learn something new about libgdx or game development in general.
Here is Jhonny again. I would like to know if can you post something about the dimensions in the box2d. Show us some technique with details to transform pixels for world of box2d. Thanks! 🙂
Sorry, my english really isn’t good, and the google translate don’t help rs. I would like that you showed some standard for transform pixels of a texture in meters in the box2d world. About this code, with more details:
static final float WORLD_TO_BOX=0.01f;
static final float BOX_WORLD_TO=100f;
float ConvertToBox(float x){
return x*WORLD_TO_BOX;
}
Okay. According to box2d documentation it is suggested that we dont make physics object same size as the number of pixels of image associated with it but rather quite smaller. For example if we have a image of box of 100×100 px then it would be better to convert the box to smaller size like for example 1×1. Now if you are transforming the size, the position also have to changed to handle that. Lets suppose another 100×100 box is 100 pixels away in horizontal direction and if the world is shrinked by ratio of 100 then there would be two 1×1 boxes 1 unit apart in horizontal direction. And while drawing we just scale them back up by 100 so on screen they look the normal size and are 100 pixels apart.
If you want, you can set the above ratios to 1 but then sometimes physics wont work properly.
Hello, my name is Jhonny, I am from Brazil. I don’t write or speak english well, but I sure that you will understand me. I am developing a game and I met Libgdx searching in the web. I want learn about physics in libgdx. Can you help me with a simple example about this? Thanks and sorry for my english, I am trying learn this also. :).
Hi,
We have written some tutorials which can be helpful for you. For physics best option is to use box2d library which comes with libgdx. You can start with the post “Using Box2d in Libgdx Game Part I” (http://rotatingcanvas.com/using-box2d-in-libgdx-game-part-i/) . And also we have posted a basic example in here (http://rotatingcanvas.com/box2dlibgdx-example-code/ ) which you can download and setup in eclipse and see how physics works in it. It is a simple bouncing ball program.
Good luck with your game.
How can I draw the body object in the screen?
With the code below already is possible show in the screen? Thanks (y)
world= new World(new Vector2(0,-20), true);
BodyDef bodyDef = new BodyDef();
bodyDef.type=BodyDef.BodyType.StaticBody;
bodyDef.position.set(new Vector2(0,0));
bodyDef.position.rotate(0);
Body body=world.createBody(bodyDef);
PolygonShape bodyShape = new PolygonShape();
bodyShape.setAsBox(100 ,100);
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.density=1f;
fixtureDef.restitution=1f;
fixtureDef.shape=bodyShape;
body.createFixture(fixtureDef);
bodyShape.dispose();
With this code only the physics body/shape is being created. To show it on screen we need to attach a texture to it. By attaching a texture what i mean is just setting position of texture to the body position in world coordinates and updating it in every frame to match the body position.
Hi I’m Taylor Ringo do you update tutorials on libgdx periodically? and am I able to subscribe by email?
Hi Taylor,
We added a widget from where you can subscribe the blog my email.
Regarding tutorials we write new ones whenever we think we had used something new which we did not put in tutorials before. We intend to add new tutorials in future whenever we learn something new about libgdx or game development in general.
Thanks
Here is Jhonny again. I would like to know if can you post something about the dimensions in the box2d. Show us some technique with details to transform pixels for world of box2d. Thanks! 🙂
Hi Jhonny,
Sorry i did not understand your comment. Can you please explain again?
Thanks
Sorry, my english really isn’t good, and the google translate don’t help rs. I would like that you showed some standard for transform pixels of a texture in meters in the box2d world. About this code, with more details:
static final float WORLD_TO_BOX=0.01f;
static final float BOX_WORLD_TO=100f;
float ConvertToBox(float x){
return x*WORLD_TO_BOX;
}
Okay. According to box2d documentation it is suggested that we dont make physics object same size as the number of pixels of image associated with it but rather quite smaller. For example if we have a image of box of 100×100 px then it would be better to convert the box to smaller size like for example 1×1. Now if you are transforming the size, the position also have to changed to handle that. Lets suppose another 100×100 box is 100 pixels away in horizontal direction and if the world is shrinked by ratio of 100 then there would be two 1×1 boxes 1 unit apart in horizontal direction. And while drawing we just scale them back up by 100 so on screen they look the normal size and are 100 pixels apart.
If you want, you can set the above ratios to 1 but then sometimes physics wont work properly.
HI! I’d like to integrate openkit into libgdx. Could you make a tutorial, please?
Hi,
I dont have any experience with openkit. I will see if i can get it working with libgdx and then maybe post a tutorial about it.
Thanks