Box2D polygonal bodies do not rotate

I am trying to create a game with "libGDX" and "Box2D". I have multiple shapes in the game, so I created a class BodyFactory

that creates my bodies usingPolygonShape

The problem is that when I create the body using the method Shape.setAsBox()

, everything works fine, but when I create the bodies with PolygonShape.set(vertices)

, the position of the bodies changes as I wish, but they won't rotate at all.

This is what I get (after stability) when I drop 3 bodies from the sky:

enter image description here The square rotates and remains on the ground, the bot will not have other shapes.

Also note that I tried to add

body.setFixedRotation(false);

      

into my code, but nothing has changed.

Also friction, mass and density of forms are in reasonable amount.

This is the part of my code that creates a "PolygonShape" from a file:

...
Body body = world.createBody(bodyDef);
...
for (int i = 0; i < bodyConf.meshData.length; i++) {
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.set(bodyConf.meshData[i]);
    fixtureDef.shape = polygonShape;
    body.createFixture(fixtureDef);
    polygonShape.dispose();
}

      

+3


source to share


1 answer


I think the problem is that you are only creating one Body

with three Fixture

attached to it.

What you really want is three Body

s, with one Fixture

attached to each of them. Thus, each body can rotate independently of the others.



for (int i = 0; i < bodyConf.meshData.length; i++) {
    BodyDef bodyDef = ...;
    Body body = world.createBody(bodyDef);
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.set(bodyConf.meshData[i]);
    fixtureDef.shape = polygonShape;
    body.createFixture(fixtureDef);
    polygonShape.dispose();
}

      

+2


source







All Articles