Search code examples
javalibgdxbox2d

box2d on different positions of the sprites on libgdx


box2d libgdx test I create a class for the DynamicBody balls and other for the walls, every class are according to tutorials and they work, the balls fall, but the problem is that the geometric shape use by box2d are not at the same place as the sprites, for what I search on google has something to do with the world of box2d not been using the camera or the camera vewport

other point is that when I create the world I use this

world = new World(new Vector2(0, -9.8f), true);

I was expecting that aggravation behaves like in the real world but appears much slower.


Solution

  • When creating box2d objects the origin is in the centre so a 2 wide object will have 1 unit left of the position set and one right. Textures are usually drawn with the lower left corner at the position so a 2 unit wide texture would have both units to the right of the position.

    To fix this you just need to draw the image 1/2 the width to the left and 1/2 the hight to the bottom. e.g

    draw(texture, position.x-(width/2),position.y-(height/2));