I've been trying to implement Box2D into this code, and I've been extremely stuck on this. I've searched high and low for an answer but could not find anything that would fix this problem. I looked at guides and tried fixes that helped other users such as doing b2World->SetAllowSleeping(false).
I start by creating the world and setting the contact listener. The World class contains the b2World, therefore I get the world and set the contact listener. To add, both pointers are stored within a scene class.
m_World = new World();
m_ContactListener = new ContactListener(m_World);
m_World->GetB2World()->SetContactListener(m_ContactListener);
I return the pointer and store it in my scene just in case it will be necessary later on.
My Contact Listener class is basically just the basics that every tutorial has said. I put a few lines of code into the BeginContact() to put a break point and try to see if it's working but so far it has not been working.
Here is the ContactListener.h:
#pragma once
class ContactListener : public b2ContactListener
{
private:
World* m_World;
public:
ContactListener(World* aWorld);
~ContactListener();`
virtual void BeginContact(b2Contact* aContact);
virtual void EndContact(b2Contact* aContact);
void SetWorld(World* aWorld) { m_World = aWorld; }
World* GetWorld() { return m_World; }
};
And here is the ContactListener.cpp file: #include "pch.h"
ContactListener::ContactListener(World* aWorld)
{
m_World = aWorld;
}
ContactListener::~ContactListener()
{
}
void ContactListener::BeginContact(b2Contact* aContact)
{
b2Fixture* fixtureA = aContact->GetFixtureA();
b2Fixture* fixtureB = aContact->GetFixtureB();
void* userDataA = fixtureA->GetUserData();
std::string nameA = ((GameObject*)userDataA)->GetName();
void* userDataB = fixtureB->GetUserData();
std::string nameB = ((GameObject*)userDataB)->GetName();
}
void ContactListener::EndContact(b2Contact* aContact)
{
}
Like I said previously I added lines of code just to try and put a break point. The guides I read said to simply override the functions and everything should work. Any help with this issue would be appreciated and if you need anymore info I will gladly add any.
For the bodies, I have created two. One static and one dynamic.
Here's the function I use to create the bodies:
b2Body* World::CreateBody(b2Vec2 aPosition, float aRotation, BodyType aType, GameObject* aOwner)
{
b2BodyDef bodyDef;
bodyDef.position = aPosition;
bodyDef.angle = aRotation;
if (aType == KinematicBody)
{
bodyDef.type = b2_kinematicBody;
}
else if (aType == DynamicBody)
{
bodyDef.type = b2_dynamicBody;
}
else
{
bodyDef.type = b2_staticBody;
}
bodyDef.userData = aOwner;
return m_b2World->CreateBody(&bodyDef);
}
Then after I create the two bodies I was previously talking about:
((PlayerObject*)m_pGameObjects["Floor"])->AddBody(m_World->CreateBody(aPosBlock, aRotationBlock, World::BodyType::StaticBody, m_pGameObjects["Floor"]));
((PlayerObject*)m_pGameObjects["Player"])->AddBody(m_World->CreateBody(aPos, aRotation, World::BodyType::DynamicBody, m_pGameObjects["Player"]));
tempgoing out of scope. Please correct that and retry. If still the same results, then please edit your question to show your corrected code that you believe no longer has data being used out of scope. If it works however, please explain this fix in the answer section so this question won't show up as unanswered anymore. Thanks! - Louis Langholtz