b2Assert(m_I > 0.0f) in SetMassFromShapes()

Hello

I have a problem with a b2Assert(m_I > 0.0f) assertion in SetMassFromShapes(). The problem seems to occur when I have small x values (lower than about -15). This minimal code causes the assertion:

Code:
b2BodyDef bodyDef;
b2Body* body = world->CreateBody(&bodyDef);
b2PolygonDef shapeDef;
shapeDef.density = 1;
shapeDef.vertexCount = 4;
shapeDef.vertices[0].x = -36.989616f;
shapeDef.vertices[1].x = -36.897999f;
shapeDef.vertices[2].x = -36.693401f;
shapeDef.vertices[3].x = -36.785019f;

shapeDef.vertices[0].y = 18.751558f;
shapeDef.vertices[1].y = 18.698116f;
shapeDef.vertices[2].y = 19.048872f;
shapeDef.vertices[3].y = 19.102314f;

body->CreateShape(&shapeDef);
body->SetMassFromShapes();


As you can see, it's a CCW rectangle with width around 0.1 and height around 0.3. It's used as a segment in a chain, and those kinds of segments work all over the map except when the x values get small (as I said). I can design around this problem in my game, but it's still annoying and confusing. Anybody knows something about this?
First of all, thanks for providing sample code. This makes it much easier to provide support for Box2D.

The rotational inertia is computed about the origin and then shifted to the center of mass. Since the shape is small and far from the origin you get a lot of rounding error that leads to a negative inertia.

I reduced the round-off error by turning on the section of code at line 385 in b2PolygonShape.cpp. However you may still run into trouble for other shapes.

Is it possible to keep these shapes closer to the origin of the body?
Cool, thanks for the explanation!

I was actually thinking about this when I stood in the shower. The reason why I'm using body center (0, 0) and place the shape using the polygon vertices is because I use the same data to render my objects, so it was easier for me. Hoooowever... I realized that I can just as well create the polygon right on top of (0, 0), and set the body center instead, because I use the xForm in the render stage anyways.

Hopefully this will help other people with similar problems when they search these forums 🙂
I was having a similar problem with that assert firing in ResetMassData.

If it helps anyone else I got around this issue by making my dynamic b2Bodies static initially:
Code:
bodyDef.type = b2_staticBody;

Then after I attached all the fixtures I set the body type to dynamic.
Code:
this_body->SetType(b2_dynamicBody);


Not 100% sure why that helped, but I noticed the assert in ResetMassData is never reached for static bodies, which makes sense 'cause static bodies have no mass iirc. Anyway something about switching the type after attaching the fixtures made things happy.
I committed a fix that makes the polygon mass computation more robust. So hopefully no tricks are necessary.
I took out my little hack and I'm still having the problem as of r174. 🙁

It only started happening with the releases from the last couple weeks...

I actually started having problems with my hack, so I instead created the body with fixed rotation, then turned it off after the body was created and that seemed to solve it. Weird! I can try to isolate the body data I'm using that's causing the assert if it would help.
fixty, can you provide the polygon data that is causing the crash?
I'm having the same problem. I just updated to the latest version of box2d (2.2.1), and I even tried the trick of adding the fixtures initially as a static body, and then changing to dynamic. That seemed to help, but sometimes I still get the dreaded assertion failure. (Not surprising, I guess, since ResetMassData is going to get called either way.)

I have it dumping the polygon points to the console right where it creates the b2PolygonShape fixture, so I could collect some examples. Here's a simple one:

2.360000, 0.210000
2.210000, 1.560000
1.370000, 0.190000
1.320000, 1.270000

Where the failure happens (at b2Body.cpp:319), localCenter is (3.15895, 1.03749), m_mass is 0.067325, and m_I is -0.111618.

Any ideas on how to avoid this problem?

Thanks,
- Joe
Self-intersecting and concave polygons are not supported - that polygon is self-intersecting.
Aha -- my own triangulation code is supposed to fix that, but clearly it failed to do so in this case. I should have checked for that before posting! Thanks for pointing out what should have been obvious.

Best,
- Joe

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