kooldart kooldart - 20 days ago 11
C++ Question

Box2D: Assertion failed and Wntdll.pdb Not Loaded

Okay so I'm making my very own simple engine, I've implemented an ECS with success, now I'm trying to implement Box2D for physics and I'm having some trouble doing it.So I have a component of type: "RigidBody2D" which does some physics calculations, it looks like this(Note I use Unity3D like components which hold Start, Awake and Update functions, I don't use systems):

'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();
};


Okay so everything looks fine, i basically copied the examples from the Box2D example: "HelloWorld.cpp" and implemented them into a class, after i run this in my console i get this message:

"Assertion failed: m_nodeCount == m_nodeCapacity, file ..\..\Box2D\Collision\b2DynamicTree.cpp, line 58"


After Some tests i found out that the line causing this problem is:

Body->CreateFixture(&fixtureDef);


when i comment this line, the program runs, then Visual Studio 2015 displays me a message that:
"Wntdll.pdb Not Loaded, wntdll.pdb contains the debug information required to find the source for the module ntdll.dll"

I'm not sure what is causing this problem or why since this is my first time working with Box2D.

EDIT:
A MVCE would have to be this, you'll need to set up Box2D with the project to be able to run this code:

#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;
}

Answer

Ok, it looks that you had not created world. b2World structure isn't initialized anywhere. The following code works for me w/o problems:

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;
}