Many guides used userData. But with the source of Box2d I have, I don't see the SetUserData
functions in the code as I see them in the guides.
After understanding it, I realized that these manuals were outdated, and now there is no such function and userData should be set in a different way. After digging through the Box2d files for three days, I realized that I could not cope alone.
I found out that it is possible to edit the b2FixtureDef structure to add my new data there. But I don't understand how to do this correctly, because just adding a line to the structure of the program crashes.
This is what I want to do:
class MyContactListener : public b2ContactListener
{
void BeginContact(b2Contact* contact)
{
//check if fixture A was the foot sensor
std::string fixtureUserData = contact->GetFixtureA()->getmy();
if ((int)fixtureUserData == 3)
fixturesUnderfoot.insert(contact->GetFixtureB());//A is foot so B is ground
//check if fixture B was the foot sensor
fixtureUserData = contact->GetFixtureB()->GetUserData();
if ((int)fixtureUserData == 3)
fixturesUnderfoot.insert(contact->GetFixtureA());//B is foot so A is ground
}
void EndContact(b2Contact* contact)
{
//check if fixture A was the foot sensor
void* fixtureUserData = contact->GetFixtureA()->GetUserData();
if ((int)fixtureUserData == 3)
fixturesUnderfoot.erase(contact->GetFixtureB());//A is foot so B is ground
//check if fixture B was the foot sensor
fixtureUserData = contact->GetFixtureB()->GetUserData();
if ((int)fixtureUserData == 3)
fixturesUnderfoot.erase(contact->GetFixtureA());//B is foot so A is ground
}
};
previously, it was possible, as I understood, to create a body and write:
body.setUserData = smth;
body.getUserData();
but this is not possible because the method returns a structure:
b2BodyUserData& b2Body::GetUserData()
{
return m_userData;
}
struct b2BodyUserData
{
b2BodyUserData()
{
pointer = 0;
}
/// For legacy compatibility
uintptr_t pointer;
};
next to the structure definition, it is written "You can define this to inject whatever data you want in b2Fixture".
When I did something like this, the program is crashing:
struct B2_API b2FixtureUserData
{
b2FixtureUserData()
{
pointer = 0;
objectType = "";
}
std::string objectType;
/// For legacy compatibility
uintptr_t pointer;
};
b2Fixture
does not have agetmy
function, shouldn't it beGetUserData
? Also, how do you set this user data? \$\endgroup\$