2
\$\begingroup\$

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

http://i.imgur.com/Q910eFs.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 )

\$\endgroup\$
0
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ @chinwong Glad I could help. \$\endgroup\$ – bornander Jan 29 '16 at 7:32
0
\$\begingroup\$

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;

//Set your position here
fixtureDef.position.set((position.x + sizeX / 2),
            (position.y + sizeY / 2));
return fixtureDef;
}
public static PolygonShape createPolygonShape(float sizeX, float sizeY){
PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(sizeX/2 , sizeY/2); 
//Don't set the position on the shape

return polygonShape;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.