Box2d cpp 给出:box2d 表达式区域 > 1.1 异常

Box2d cpp gives : box2d expression area > 1.1 exception

我有一个奇怪的错误:

#define PTM_RATIO 32
// shape
            b2PolygonShape axleShape;
            axleShape.SetAsBox(20/PTM_RATIO,20/PTM_RATIO);

            // fixture
            b2FixtureDef axleFixture;
            axleFixture.density=0.5;
            axleFixture.friction=3;
            axleFixture.restitution=0.3;
            axleFixture.shape=&axleShape;
            axleFixture.filter.groupIndex=-1;
            // body definition
            b2BodyDef axleBodyDef;
            axleBodyDef.type=b2_dynamicBody;
            // the rear axle itself
            axleBodyDef.position.Set(m_cart->GetWorldCenter().x-(60/PTM_RATIO),m_cart->GetWorldCenter().y+(65/PTM_RATIO));
            b2Body* rearAxle =m_world->CreateBody(&axleBodyDef);
            rearAxle->CreateFixture(&axleFixture);
            // the front axle itself
            axleBodyDef.position.Set(m_cart->GetWorldCenter().x+(75/PTM_RATIO),m_cart->GetWorldCenter().y+(65/PTM_RATIO));
            b2Body* frontAxle=m_world->CreateBody(&axleBodyDef);
            frontAxle->CreateFixture(&axleFixture);

在最后一行中,我收到异常消息:

当我将其更改为:

axleShape.SetAsBox(40/PTM_RATIO,40/PTM_RATIO);

它可以正常工作,但尺寸不符合我的需要。 我怎样才能制作小盒子?

这是一个整数除法的例子。当您将 20/PTM_RATIO 传递给 SetAsBox 方法时,这与 20/32 相同,其计算结果为 0

您可以通过将第一行更改为 #define PTM_RATIO 32.0 来解决此问题。末尾的零点应使所有除法表达式的计算结果为小数。

断言 "area > 1.192092896E-07" 确保形状的面积大于该数字。如果断言失败,程序将不会编译。目前,您传入的半宽和半高均为 0,这意味着矩形的面积将为 0,这会导致断言失败。