Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I found about this using debug renderer. When game starts, everything is in order. But when some collision happens, sprite's rotation is way larger than its body. When sprite and body match is the time its body is horizontally aligned.

enter image description here

It seems like center of sprite's rotation is far distant. I am using following code.

        Sprite sprite = data.sprite;
        position=body.getPosition();
        sprite.setPosition(position.x-sprite.getWidth()/2,position.y-sprite.getHeight()/2);
        sprite.setOrigin(position.x, position.y);
        sprite.setRotation( MathUtils.radiansToDegrees*body.getAngle());

As you can see, I am even trying to set center of its rotation with setOrigin without success. does anybody have any advice or clue?

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Most APIs represent the Sprite's origin in local space, not in world space. This is supported by libgdx's documentation which states:

A Sprite also has an origin around which rotations and scaling are performed (that is, the origin is not modified by rotation and scaling). The origin is given relative to the bottom left corner of the Sprite, its position.

So I think that if you want rotations to happen around the center of the sprite, you should be using the following origin instead:

sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
share|improve this answer
    
You are really fast. You are right about setOrigin. That was the trick. Thanks a lot! BTW, setPosition statement should be from mine. You need to set corner point for sprite position. –  Paul Jan 18 '12 at 5:42
    
@Paul I noticed the problem and edited my post but you had already commented. :) In some APIs the origin also affects translation (which is why I initially wrote a different setPosition) but then I checked the documentation and found out that's not the case with libgdx so your setPosition was correct. –  David Gouveia Jan 18 '12 at 5:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.