我试图让Box2D正常工作。我可以编译程序,但是当我尝试运行时出现错误。这是代码:

bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(x, y);
body = world.CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(w, h);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);


错误:

mezo: ~/cpp/Box2D_v2.2.1/Box2D/Collision/Shapes/b2PolygonShape.cpp:352: virtual void b2PolygonShape::ComputeMass(b2MassData*, float32) const: Assertion `area > 1.19209289550781250000e-7F' failed.


我想出是否注释掉fixDef.density = 1.0f;我可以编译,运行并且一切正常,除了物体快速移动2(因为可能是密度?),如果将密度设置为0,它也可以工作。

最佳答案

看起来您的形状面积非常小。检查w, h值。身体的速度与其密度无关。

关于c++ - Box2D无法设置密度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9757654/

10-10 15:30