1
votes

The image below exemplifies what I am trying to do.The polygon body should rotate keeping its initial position and the red point, which could also be another body, stays in the center.

enter image description here

Here is what I have tried, however it moves something like around the red point:

        Vector2 position = body2.getWorldCenter();
        body2.setTransform(position, body2.getAngle() + 0.01f);

Any thoughts?

UPDATE 1

Here is how a create the Polygon body:

private Body createPolygonBody(Vector2 pos) {
    // 0. Create a loader for the file saved from the editor.
    BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("shot/wall"));

    // 1. Create a BodyDef, as usual.
    BodyDef bd = new BodyDef();
    bd.position.set(pos);
    bd.type = BodyType.StaticBody;

    // 2. Create a FixtureDef, as usual.
    FixtureDef fd = new FixtureDef();
    fd.density = 1;
    fd.friction = 0.5f;
    fd.restitution = 0.3f;
    fd.filter.categoryBits = CATEGORY_SCENERY;
    fd.filter.maskBits = MASK_SCENERY;

    // 3. Create a Body, as usual.
    Body body = world.createBody(bd); 


    // 4. Create the body fixture automatically by using the loader.
    loader.attachFixture(body, "one_door",fd, 3f);

    return body;
}
1
For that to work you'd have to define the polygon shapes' points in relation to the red dot's center. Could you post the code that creates the body?Dennis Korbar
Thanks @DennisKorbar for your attention on that. I have updated the post. I use "Physics Body Editor" by Aurilien to generate the vertices.Devester
Unfortunately I don't know about that editor. Does that file include coordinates for the polygon? What you basically have to do is to make sure that the coordinates are relative to the point (0,0). And then position the body exactly on the red circle.Dennis Korbar
@DennisKorbar Yes, the editor generate the coordinates and it is possible to generate them relative to a point. Thanks for pointing out this possibility, it is working now as expected. If you want to drop a answer and I can accept it...Devester
Great! I have formulated the information from the comments as an answer.Dennis Korbar

1 Answers

1
votes

You have to generate the polygons relative to the point that you want to rotate around.

Here is what you have to do:

  • Create the polygons in relation to point (0,0)
  • Place the body exactly at the center of the red point

Rotation should work as you want it then.