好的,我正在制作我自己的简单引擎,我已经成功实现了ECS,现在我正在尝试用物理实现Box2D而且我在做某些事情上遇到了麻烦所以我有一个类型的组件:" RigidBody2D"它做了一些物理计算,它看起来像这样(注意我使用Unity3D就像保存Start,Awake和Update功能的组件一样,我不使用系统):
'RigidBody.h'
#pragma once
#include "Component.h"
class RigidBody2D : public Component
{
private:
float Density = 1.f;
float Friction = 1.f;
b2BodyDef bodyDef;
b2FixtureDef fixtureDef;
b2PolygonShape dynamicBox;
public:
Transfrm* Trans;
b2Body* Body;
RigidBody2D() : Component("RigidBody2D") {
Trans = new Transfrm(b2Vec2(0.f, 0.f), 0.f, b2Vec2(1.f, 1.f));
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(Trans->Position.x, Trans->Position.y);
Body = CurrentPhysicsWorld->CreateBody(&bodyDef);
dynamicBox.SetAsBox(1.0f, 1.0f);
fixtureDef.shape = &dynamicBox;
fixtureDef.density = GetDensity();
fixtureDef.friction = GetFriction();
Body->CreateFixture(&fixtureDef); // The Line That Causes trouble
};
~RigidBody2D();
};
好吧所以一切都很好,我基本上复制了Box2D示例中的例子:" HelloWorld.cpp"并将它们实现到一个类中,在我的控制台中运行后,我得到了这样的消息:
"Assertion failed: m_nodeCount == m_nodeCapacity, file ..\..\Box2D\Collision\b2DynamicTree.cpp, line 58"
经过一些测试后我发现造成这个问题的一行是:
Body->CreateFixture(&fixtureDef);
当我评论此行时,程序运行,然后Visual Studio 2015向我显示一条消息:"Wntdll.pdb Not Loaded, wntdll.pdb contains the debug information required to find the source for the module ntdll.dll"
我不确定导致此问题的原因或原因,因为这是我第一次使用Box2D。
编辑: MVCE必须是这个,你需要在项目中设置Box2D才能运行这段代码:
#include <Box2D.h>
b2World CurrentWorld;
class World {
public:
b2World physicsWorld;
World() { CurrentWorld = this->physicsWorld; };
~World() {};
};
class RigidBody2D
{
public:
b2Body* Body;
b2BodyDef bodyDef;
b2FixtureDef fixtureDef;
b2PolygonShape dynamicBox;
RigidBody2D() {
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(Trans->Position.x, Trans->Position.y);
Body = CurrentWorld->CreateBody(&bodyDef);
dynamicBox.SetAsBox(1.0f, 1.0f);
fixtureDef.shape = &dynamicBox;
Body->CreateFixture(&fixtureDef);
}
~RigidBody2D() {};
};
int main() {
World wrld = World();
RigidBody2D body = RigidBody2D();
return 0;
}
答案 0 :(得分:1)
好吧,看起来你没有创造世界。 b2World
结构未在任何地方初始化。以下代码适用于我没有问题:
static b2World *CurrentWorld = nullptr;
class RigidBody2D
{
public:
b2Body* Body;
b2BodyDef bodyDef;
b2FixtureDef fixtureDef;
b2PolygonShape dynamicBox;
RigidBody2D() {
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(1.0, 1.0);
Body = CurrentWorld->CreateBody(&bodyDef);
dynamicBox.SetAsBox(1.0f, 1.0f);
fixtureDef.shape = &dynamicBox;
Body->CreateFixture(&fixtureDef);
}
~RigidBody2D() {}
};
int main(int argc, char** argv)
{
b2Vec2 gravity(0.0f, -10.0f);
CurrentWorld = new b2World(gravity); // <- you missed that
RigidBody2D *body = new RigidBody2D();
float32 timeStep = 1.0f / 60.0f;
int32 velocityIterations = 6;
int32 positionIterations = 2;
for (int32 i = 0; i < 60; ++i)
{
CurrentWorld->Step(timeStep, velocityIterations, positionIterations);
b2Vec2 position = body->Body->GetPosition();
float32 angle = body->Body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
return 0;
}