# Box2D LibGDX Body.getPosition() and Body.getWorldCenter() problems

I have a problem with my game in Java with Box2D and LibGDX

I found out that when I use

body.getPosition() it is only half of that of body.getWorldCenter().

The problem here is for me that I use Joints also, which forces me into using body.getPosition().

But now my body gets drawn on the position of body.getWorldCenter().

which makes it so it looks like that

https://i.sstatic.net/xsTNl.png

Here is the source code where I create the Car Body

}

public static Body createCarBody(Vector2 position, float sizeX, float sizeY, float density, float friction, float restitution, float linearDamping, float angularDamping)
{

System.out.println("position createCarBody " + position);
Body body = Physics.createBody(createBodyDef(position, linearDamping, angularDamping));
body.createFixture(createFixtureDefCar(createPolygonShape(sizeX, sizeY,position), density, friction, restitution));

return body;
}

public static FixtureDef createFixtureDefCar(PolygonShape shape, float density, float friction, float restitution)
{
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;
return fixtureDef;

}

public static PolygonShape createPolygonShape(float sizeX, float sizeY, Vector2 position)
{

PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(sizeY/2 , sizeX/2, position,0);

System.out.println("position polygonshape " + position);

return polygonShape;
}


I use the createCarBody method in my car class where it is given a Position Vector of (30,30) ( first car ) and (40,40) second car

If I print body.getWorldCenter() i get (60,60) and (80,80 )

Instead of applying the position to the Shape if the FixtureDef, apply the position to the Body.

Something like this might work for you;

public static Body createCarBody(Vector2 position, float sizeX, float sizeY, float density, float friction, float restitution, float linearDamping, float angularDamping) {
System.out.println("position createCarBody " + position);
Body body = Physics.createBody(createBodyDef(position, linearDamping, angularDamping));
body.createFixture(createFixtureDefCar(createPolygonShape(sizeX, sizeY), density, friction, restitution));
body.setTransform(position, 0.0f); // Set the position of your body
return body;
}

public static FixtureDef createFixtureDefCar(PolygonShape shape, float density, float friction, float restitution) {
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;
return fixtureDef;
}

public static PolygonShape createPolygonShape(float sizeX, float sizeY) {
PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(sizeY/2 , sizeX/2); // Don't set a position here
return polygonShape;
}

• @chinwong Glad I could help. Commented Jan 29, 2016 at 7:32

Try setting the position on the FixtureDef.

I'll assume that you already converted the units to Box2d:

public static Body createCarBody(Vector2 position, float sizeX, float sizeY, float density, float friction, float restitution, float linearDamping, float angularDamping){

System.out.println("position createCarBody " + position);
Body body = Physics.createBody(createBodyDef(position, linearDamping, angularDamping));
body.createFixture(createFixtureDefCar(createPolygonShape(sizeX, sizeY), density, friction, restitution, position, sizeX, sizeY));

return body;
}

public static FixtureDef createFixtureDefCar(PolygonShape shape, float density, float friction, float restitution, Vector2 position, float sizeX, float sizeY){
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = density;
fixtureDef.friction = friction;
fixtureDef.restitution = restitution;