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'm new in libgdx and i have a little problem. I created a body in box2d and i want to assign it a sprite; for that i need to get the position of the body,then insert into the main loop the updated position of the sprite, right ? So..i made so but the sprite doesn't align right on the body, but in a corner. What should I write in body.setPosition() and in sprite's position ?

P.S: sorry but i can't post a screenshot :(

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You have to consider different scenario for box2d and scene. Both the thing have different anchor point. Box2d assign position based on the centre of the object while scene assign position based on the corner of the object.

Either you can change the anchor point of the object to centre to match it with box2d or you have to calculate position of the sprite or object.

If you want position of the object to (100,100) in scene coordinate then same assignment for box2d object would be (100/PTM_RATIO,100/PTM_RATIO) where PTM_RATIO value is 32.0f.

Now using above calculation both sprite and object get same position. Also consider position and size of the object must be divided by PTM_RATIO in box2d otherwise it show abnormal behaviour.

share|improve this answer
    
Sorry but I think you didn't understand my problem. I've tried your example but it didn't work. The sprite is not aligned, it's far away form the physic body :( Could you give me a better example ? –  Ion Farima Feb 17 '13 at 16:17
    
I've forgotten to set the solution your answer. The problem was due to different projections. For the physic world I set the spritebatch but for the graphics I didn't set it. –  Ion Farima Mar 10 '13 at 18:50
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.