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 :(


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.

  • \$\begingroup\$ 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 ? \$\endgroup\$ – Ion Farima Feb 17 '13 at 16:17
  • \$\begingroup\$ 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. \$\endgroup\$ – Ion Farima Mar 10 '13 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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