Rendering jBodies

I need to render the Jbox bodies, and I'm struggling to find a way to extract the needed information.
How do I loop through the bodies, and check what shape they are?
I found this method jBoxWorld.getBodyList(); and it returns Body class, and as I looked at the source code, that body class is supposed to be a list of jBox bodies, and the comments in the source code tell me that I should use bodyList.m_next to loop through the bodies, which are also Body class.

JBox2D is extremely confusing. I have no idea how to get any information out from the bodies or the JBox world.

Also, once I get the bodies, how do I check what shape they have?
And how do I get the information of custom shaped polygon's corners' positions?
This is how Box2D works in general hehe. It's a way to iterate through a list of stuff without needing an iterator or an array.

To go through the world's body list without keeping an array you manage on your own, you traverse through the array like this:

Code: Select all

for (b2Body body = world.GetBodyList(); body != null; body = body.GetNext()) {
    //do stuff
}


The usual way to keep track of bodies is to keep an array of them yourself and not to go through the world's body list. So when you create a body, just add it to your array.

To determine the type of shape/fixture it has, you either use the UserData or to use body.GetFixtureList(), which works exactly the same way as world.GetBodyList(), except with fixtures. If you only have the one fixture, then you don't have to traverse through the fixture list if there's only one fixture. Then, with the fixture, you can use fixture.GetType() to get the type of shape it is (as an integer constant), so you can downcast to the particular type of shape.

The best way for most types of games is to use userData when making the bodies or fixtures (then the GetUserData() in the body or the fixture to get the user data). userData is of type Object, so you can store it as any class you want, and to get the user data, you use GetUserData() then cast it to the class you used. You take advantage of this fact to determine not only the type of shape the body or fixture has, but also what kind of graphics.
Thank you for the fast reply!
I'll try this soon.

Original thread: https://box2d.org/forum/viewtopic.php?t=8638