当我运行使用Box2D
的程序时,我遇到了这个奇怪的错误,这真是意料之外的,因为它在一个与我的项目无关的文件中(未建立的文件夹没有链接到我的项目)。我为Box2D
获取了VS12
的副本,并为我的项目构建了该副本,并按照以下教程设置项目:https://www.youtube.com/watch?v=kEClRFKyGkw&list=PLSPw4ASQYyymu3PfG9gxywSPghnSMiOAW&index=53
这是我的代码:
void Box::init(b2World* world, const glm::vec2& position, const glm::vec2& dimensions)
{
m_dimensions = dimensions;
b2BodyDef boxDef;
boxDef.type = b2_dynamicBody;
boxDef.position.Set(position.x, position.y);
m_body = world->CreateBody(&boxDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(position.x / 2.0f, position.y / 2.0f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &boxShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
m_fixture = m_body->CreateFixture(&fixtureDef);
}
我称之为init()
:
b2Vec2 gravity(0.0f, -9.8f);
m_world = std::make_unique<b2World>(gravity);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);
b2Body* groundBody = m_world->CreateBody(&groundBodyDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(50.0f, 10.0f);
groundBody->CreateFixture(&groundShape, 0.0f);
Box newBox;
newBox.init(m_world.get(), glm::vec2(0.0f, 14.0f), glm::vec2(15.0f, 15.0f));
m_boxes.push_back(newBox);
我的控制台上印有错误:
断言失败:区域&gt; 1.19209 2896e-07F, path_to_unbuild_box2d \ box2d_v2.3.0 \ Box2D的\ Box2D的\碰撞\形状\ B2 polygonshape.cpp,第422行
以下是发生错误的位置(当我点击Retry
时)line 336 b2Fixture.h
:
inline void b2Fixture::GetMassData(b2MassData* massData) const
{
m_shape->ComputeMass(massData, m_density);
}
问题1已修复
boxShape.SetAsBox(dimensions.x / 2.0f, dimensions.y / 2.0f); ///Instead of position.
答案 0 :(得分:3)
The line of code that's failing is(如&#34; Assertion失败&#34;消息中所述):
b2Assert(area > b2_epsilon);
这意味着您的多边形具有微不足道的大小(查看断言,该区域小于1.192092896e-07F
,这是值0.0000001192092896
,这很小。)
可能boxShape
的大小无效。在行上设置断点:
boxShape.SetAsBox(position.x / 2.0f, position.y / 2.0f);
并检查position
的值。确保您将框的尺寸设置为合理的值。我敢打赌,你不是。