behdad / box2d

Automatically exported from code.google.com/p/box2d
2 stars 12 forks source link

Assertion in b2Body::ResetMassData() #174

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I found a case where adding a small polygon fixture to a body at a large 
distance away from the body's center will cause an assert to hit in 
b2Body::ResetMassData().
The problem occurs in the section commented "Center the inertia about the 
center of mass" and seems to be related to floating point precision.

    //replication case
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        b2Body* body = m_b2world->CreateBody(&bodyDef);

        b2PolygonShape polyShape;       
        b2Vec2 verts[3];
        verts[0].Set( 17.63, 36.31 );
        verts[1].Set( 17.52, 36.69 );
        verts[2].Set( 17.19, 36.36 );
        polyShape.Set(verts, 3);

        b2FixtureDef polyFixtureDef;
        polyFixtureDef.shape = &polyShape;
        polyFixtureDef.density = 1;

        body->CreateFixture(&polyFixtureDef);   //assertion hits inside here
    }

In the code above, if you change the triangle location so it is the same size 
and shape but closer to the 'origin', such as:
        verts[0].Set( 7.63, 6.31 );
        verts[1].Set( 7.52, 6.69 );
        verts[2].Set( 7.19, 6.36 );
there is no problem.

I seem to recall some restrictions on sizes of polygon fixtures mentioned 
somewhere, but I can't recall where it was... could this be the reason why?

Original issue reported on code.google.com by iforc...@gmail.com on 28 Jan 2011 at 1:55

GoogleCodeExporter commented 9 years ago
The mass is computation is now less sensitive to round-off errors.

Original comment by erinca...@gmail.com on 28 Mar 2011 at 6:34