0

I making a map with Tiled and load all box2d bodies with Box2DMapObjectParser to my world and everything just work fine except one thing. I'm using a newer version of Tiled where implemented body rotation. I'm trying to implement it in Box2DMapObjectParser by myself and this is what I get from it:

Editor: enter image description here

And the game: enter image description here

As you can see the right rectangle has a property named Rotation so I can easily get from properties, but I don't know what is wrong with it.

I set the rotation like this

fixture.getBody().setTransform(fixture.getBody().getPosition(), degree);

where degree is value from editor. Anybody have a clues about what am I doing wrong? I also tried doing something like

fixture.getBody().setTransform(fixture.getBody().getPosition(), degree*MathUtils.degreesToRadians);

But itsalso sets a wrong rotation.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • use -degree*MathUtills.degreetoradian – Singhak Jul 02 '15 at 04:36
  • I tried it and it did not help. Looks like the editor rotates body from it's center and box2d `setTransform` rotates it from bottom left corner. is there a way to rotate it like editor does? – BeforeBigBang Jul 02 '15 at 14:02
  • @BeforeBigBang Can you please share your solution? I'm having the same issue and I can't figure out what is suggested in the accepted answer. – Lior Iluz Apr 26 '16 at 18:23
  • 1
    @LiorIluz Sorry mate, it's been so long since i did this, i don't have src anymore. – BeforeBigBang May 10 '16 at 15:14

1 Answers1

1

Tiled rotates rectangle objects by their top-left corner. Box2D rotates around the position of the body, so the easiest solution is probably to create your box fixture such that the position of the body is in the top-left (if you're using b2PolygonShape::SetAsBox this means passing a center at half the width and half the negative height (since the Y axis is inverted)).

Thorbjørn Lindeijer
  • 2,022
  • 1
  • 17
  • 22