So I have Body body that is a rectangle. How do I get its half width and half height?(I could not find an answer anywhere else)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unfortunately, it’s not perfectly straight forward as Box2D (and thus JBox2D) does not have any concept of rectangles per se. The rectangle is a
PolygonShape, whose shape was probably specified usingsetAsBox(halfWidth, halfHeight).To get that
halfWidthandhalfHeightafter creating aFixture, consider the following (please refactor it as needed):To get this information from a
Bodyuse:And a few tests:
Prints:
Hope that helps.
On a similar note, here’s a terse way to get the dimensions for a
PolygonShape:Dividing the returned
Vec2‘s dimensions in half will also retrieve the desired value.