0
votes

Im trying to rotate a body around its center in box2d. The rotation of the sprite works, but what the body does is rotate around its top left point.

Code for rotating the sprite:

    public void drawBitMap(Bitmap bit, float x, float y, float width, float height, float rotation){
    Sprite sprt = null;
    if (!sprts.containsKey(bit)) {
         sprt = new Sprite(bit.texture);
        sprts.put(bit, sprt);
    }else{
        sprt = sprts.get(bit);
    }
    sprt.setOrigin(width/2,height/2);
    sprt.setRotation(rotation);
    sprt.setPosition(x,y);

    sprt.setSize(width,height);
    sprt.draw(this);
}

Code for rotating body:

 body.setTransform(x, y, rotation * MathUtils.degreesToRadians);

Images of what is happening:

Here, rotation is around (-/+)90 degrees

Here rotation is broken for every wolf

1

1 Answers

2
votes

It probably has something to do with the definition of the origin of the body or fixture. Can you show me the code you used to create the body and fixture?

EDITED

Ok, I've checked your JSON and it's exactly what I thought. In those JSONs the fixture was defined with the center of the body at its bottom left corner.

The point (0,0) is the center of the body, and the fixture you are defining has that point at its bottom left corner, that's why it rotates around its bottom left corner.

This might sound confusing so I'm going to give you a simple example: a square. A square of size 1 that has this same problem would have its vertices defined at (0,1), (1,1), (1,0) and (0,0).

As stated above, the point(0,0) is the center of the body so, in order to alineate the centers of the fixture and body, the vertices should be defined AROUND the point that is the center of the body(0,0).

The correct square would have the following vertices: (-0.5,0.5), (0.5,0.5), (0.5,-0.5), (-0.5,-0.5).