Reputation: 862
My problem is, that my Box2D Body has another position than the LibGDX Sprite I want to render for that body. In my render-loop, for each body, I'm setting the position of it's sprite to the one of the body, and then rendering it. When creating a Box2D Shape other than a circle, Box2D does not move it from it's origin, and neither does the LibGDX-sprite. If I now move or set the position of the body, my sprite will always follow it. But, unfortunately, this is not possible with CircleShapes: Since LibGDX's Sprite#setPosition does not take into account the origin of the sprite (Which is only used for scaling and rotating), the sprite is set by it's lowerleft corner. So here is the problem: The Box2D CircleShape is moved by taking into account the origin! So my sprite always starts in the origin of the shape. Does anyone know how to fix that? And, ultimately, I'd want to always move both while taking into account the origin. How do I do that?
Upvotes: 1
Views: 1506
Reputation: 21
Box2d body origin is never changed. The Circle and Box shape's origin is middle and the polygon shape's origin is the bottom left corner.
The only way to fix it, you change the sprite origin to middle that is Sprite.setOriginCenter();
.
If body is a circle or box shape, the sprite position set like as
sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
.
If body is a polygon shape the code should be like as sprite.setPosition(body.getPosition().x, body.getPosition().y);
.
Upvotes: 2