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
}