Advertisement
dermetfan

Box2D Sprite drawing possible mistake

Oct 3rd, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. // sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
  2.  
  3. // you have to draw sprites like this for bodies which shape are positioned like this:
  4. // shape.setPosition(new Vector2(0, 1));
  5. // obviously the sprite is in the correct place but the shape is not then
  6. // the shape was a CircleShape with a radius of .5f
  7. // so the image was drawn right under it, rotating around it
  8. // however, I'm keeping this formula I figured out to be working in this case.
  9. // I doubt it works with different shape positions/sizes though
  10. sprite.setPosition(body.getPosition().x - sprite.getWidth() + sprite.getWidth() / 2 * (1 - MathUtils.sin(body.getAngle()) * 2), body.getPosition().y - sprite.getHeight() / 2 * (1 - MathUtils.cos(body.getAngle()) * 2));
  11.  
  12. sprite.setRotation(body.getAngle() * MathUtils.radDeg);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement