0
votes

I need that my body rotate around specific point like shown in picture. enter image description here I already done that by this line of code (Please let me know if there is a better way)

PolygonShape shape = new PolygonShape();
shape.setAsBox(1.8f / PPM, 0.2f / PPM, new Vector2(2,2), 0); // vector2 is

now I want to position sprite accordingly to my rotating body. Here what I did

sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
sprite.setPosition(body.getPosition().x * 32 - sprite.getWidth() / 2, body.getPosition().y * 32 - sprite.getHeight() / 2);

and here how I draw everything

public void draw(SpriteBatch sb){
        System.out.println(body.getWorldCenter());
        sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
        sprite.draw(sb);
    }

Strange things happen when I run my code. I seee that body rotating correctly, but sprite rotates around its origin (middle of a sprite) because of this line sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2); however I need that sprite position would be always the same as my body position. How can I cheve this?

1
Check whether this helps: tutorialsface.com/2015/12/…Laxman Marothiya

1 Answers

0
votes

The origin of all box2d bodies is their location.

Important: The box2d bodies of type box/circle get centered around their location, while the other types of bodies are NOT.

If you want to sync your rotation with the sprite, you should position the sprite origin over the body location and then rotate.

Example: if you have body at location 0,0 and sprite with origin 1,1 you should position that sprite on location -1,-1 in order for the two to rotate synchroniously!