2
votes

I just made my character for the game which is a box2d dynamic body:

   public Body createPlayer(){
        Body body;
        BodyDef def = new BodyDef();
        def.type = BodyDef.BodyType.DynamicBody;
        def.fixedRotation = true;
        def.position.set(position.x, position.y);
        body = world.createBody(def);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(1, 1);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 0.1f;
        body.createFixture(fixtureDef).setUserData(this);
        body.setLinearVelocity(20, 0);
        shape.dispose();

        return(body);
    }

Is it possible to add texture or sprite to a body? or I'll just set the sprite position the same as the position of my body? so that it will cover the shape of the body and move like the actual box2d body.

1

1 Answers

3
votes

The Box2D physics library is completely graphics API agnostic - It doesn't understand sprites at all. It is a none visual (just data) simulation of a physics world.

As you mentioned, you will have to create a Sprite and move/rotate it to keep in sync with the Box2D simulation.

Here is a good beginners guide to linking a sprite to a Box2D simulation