0

I am following the manual at http://box2d.org/manual.pdf to try and better understand how Box2D works but I have run into a problem. When I call m_body->CreateFixture it throws an exception when it tries to access the fixtureDef->shape because fixtureDef->shape is 0xCDCDCDCD. I am very confused because I am still learning Box2D and would love it if anyone could help explain.

Here are some important pieces of code to know from PhysicsEngine.h:

class PhysicsGround{
private:
    b2Body* m_body;
    b2Fixture* m_fixture;
    b2PolygonShape m_shape;

public:
    void init(glm::vec2 pos, glm::vec2 size, b2World* world);

    b2Body* getBody();
    void setBody(b2Body* body);

    b2Fixture* getFixture();
};

class PhysicsEngine
{
private:
    ICMEM::StackAllocator m_stackAlloc;

    std::vector<PhysicsSprite*> m_physObjs;

    b2World* m_world;
    b2Vec2 m_gravity;

public:
    PhysicsEngine() {}
    void init(float gravX, float gravY);
    void shutDown();

    void addPhysicsObject(PhysicsSprite* sprite);
    void addGround(glm::vec2 pos, glm::vec2 size);
    void setGravity(float gravX, float gravY);
    void update(double dt);

    b2World* getWorld() { return m_world; }
};

Here is some important info from PhysicsEngine.cpp:

void PhysicsEngine::init(float gravX, float gravY) {
    m_stackAlloc.init(200000000);
    m_world = (b2World*)m_stackAlloc.allocAligned(sizeof(b2World), alignof(b2World));
    m_world = new(m_world)b2World(b2Vec2(gravX, gravY));
}

void PhysicsEngine::addGround(glm::vec2 pos, glm::vec2 size) {
    PhysicsGround* physicsGround = (PhysicsGround*)m_stackAlloc.allocAligned(sizeof(PhysicsGround), alignof(PhysicsSprite));
    physicsGround->init(pos, size, m_world);
}

void PhysicsGround::init(glm::vec2 pos, glm::vec2 size, b2World* world) {
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(pos.x, pos.y);
    m_body = world->CreateBody(&groundBodyDef);

    m_shape.SetAsBox(size.x / 2.0f, size.y / 2.0f);
    m_fixture = m_body->CreateFixture(&m_shape, 0.0f); // Exception thrown here
}
Artur S.
  • 25
  • 7
  • Uninitialized value somewhere (there is no b2Shape here). Your constructor doesn't initialize your values, put something like `b2World* m_world{nullptr};` **everywhere** if you don't provide a constructor. – Matthieu Brucher Dec 18 '18 at 19:39
  • @MatthieuBrucher I thought placement new initialized b2World, no? I'll try that tho. Thank you – Artur S. Dec 18 '18 at 20:13
  • You don't have a placement new for the pointers. But yes, you have numerous arbitrary values and your init may need to be the constructor. – Matthieu Brucher Dec 18 '18 at 20:14
  • @MatthieuBrucher Thank you, I fixed the original problem by adding placement new to the physicsGround object. I appreciate your help. Now I have a different problem which I placed in the question. Thanks for your help though, I really appreciate it. – Artur S. Dec 18 '18 at 20:30
  • If there is another question, please raise a new one. Easier, as this one is now solved and people have already looked and discarded it. – Matthieu Brucher Dec 18 '18 at 20:35
  • @MatthieuBrucher Ok, thank you. Can you mark a comment as an answer? – Artur S. Dec 18 '18 at 20:38
  • `0xCDCDCDCD` means unitialized heap memory https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – drescherjm Dec 18 '18 at 20:57

1 Answers1

1

You have uninitialized values in your objects. This creates in non debug build random values, in this case the pattern 0xCDCDCDCD is a typical Visual Studio debug value for these uninitialized values.

You may want to use C++11 initialization capabilities as you don't provide a custom constructor. For instance:

class PhysicsGround{
private:
    b2Body* m_body{nullptr};
    b2Fixture* m_fixture{nullptr};
    b2PolygonShape m_shape;
//
};
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62