box2d.Fixture contact

In box2d, there is a class b2ContactListener, consisting of virtual functions and kind of allowing you to override what will be executed when collisions are detected. I have redefined the functions as follows :

class MyContactListener : public b2ContactListener {     void BeginContact(b2Contact* contact)     {         if (((reinterpret_cast<FixtureUserData*> (contact->GetFixtureA()->GetUserData().pointer))->objectType) == "player")         {             if ((reinterpret_cast<FixtureUserData*> (contact->GetFixtureB()->GetUserData().pointer))->objectType == "box")                 isBoxContactPlayer = 1;         }     }       void EndContact(b2Contact* contact)     {         if (((reinterpret_cast<FixtureUserData*> (contact->GetFixtureA()->GetUserData().pointer))->objectType) == "player")         {             if ((reinterpret_cast<FixtureUserData*> (contact->GetFixtureB()->GetUserData().pointer))->objectType == "box")                 isBoxContactPlayer = 0;         }     } }; 

However, the value of the variable remains unchanged.

full code:

#include "test.h" #include <iostream> #include <vector>    bool isBoxContactPlayer = 0;  /*FixtureUserData* objectData = reinterpret_cast<FixtureUserData*> (boxes[1]->GetFixtureList()->GetUserData().pointer);         std::string stringData = objectData->objectType;*/ struct FixtureUserData {     std::string objectType; };  class AAA: public Test { public:     b2Body* player;     b2Body* sword;     b2Joint* joint;     std::vector<b2Body*> swordsVec;     b2Body* boxes[31];     AAA()     {         m_world->SetGravity(b2Vec2(0.0f, -30.f));         {             b2PolygonShape grnd;             grnd.SetAsBox(30, 1);             b2BodyDef groundDef;             groundDef.position.Set(0, 5);             b2Body* ground = m_world->CreateBody(&groundDef);             ground->CreateFixture(&grnd, 5); }         {             int check = 0;//             b2BodyDef boxDef;//             boxDef.type = b2_dynamicBody;//             b2PolygonShape boxShape;//             boxShape.SetAsBox(0.5, 0.5);//              //fixture definition             b2FixtureDef boxFDef;//             FixtureUserData* MyDataBox = new FixtureUserData;             (*MyDataBox).objectType = "box";             boxFDef.shape = &boxShape;             boxFDef.density = 3;//             boxFDef.userData.pointer = reinterpret_cast<uintptr_t>(MyDataBox);                          for (int i = 0; i < 31; i++)             {                 boxDef.position.Set(0, 10+i);//                 boxes[i] = m_world->CreateBody(&boxDef);                 boxes[i]->CreateFixture(&boxFDef);             }                      }         {             b2PolygonShape playerShape;//             playerShape.SetAsBox(1, 1);//             b2BodyDef playerDef;//             playerDef.type = b2_dynamicBody;//             playerDef.fixedRotation = true;//             playerDef.position.Set(0, 7);//             b2FixtureDef playerFDef;//             playerFDef.density = 5;//             playerFDef.shape = &playerShape;             FixtureUserData* MyDataPlayer = new FixtureUserData;             (*MyDataPlayer).objectType = "player";             playerFDef.userData.pointer = reinterpret_cast<uintptr_t>(MyDataPlayer);              player = m_world->CreateBody(&playerDef);             player->CreateFixture(&playerFDef);         }              }      void Step(Settings& settings) override     {         std::cout << isBoxContactPlayer << '\n';          if (glfwGetKey(g_mainWindow, GLFW_KEY_A) == GLFW_PRESS)         {             if (player->GetLinearVelocity().x > -20)                 player->ApplyLinearImpulseToCenter(b2Vec2(-100, 0), true);         }                      if (glfwGetKey(g_mainWindow, GLFW_KEY_D) == GLFW_PRESS)         {             if (player->GetLinearVelocity().x < 20)                 player->ApplyLinearImpulseToCenter(b2Vec2(100, 0), true);         }          if (glfwGetKey(g_mainWindow, GLFW_KEY_C) == GLFW_PRESS)         {                      }         if (glfwGetKey(g_mainWindow, GLFW_KEY_K) == GLFW_PRESS && (swordsVec.size()==0))         {             //Создается меч             b2PolygonShape swordShape;             swordShape.SetAsBox(2, 0.2f, b2Vec2(-1, 0), 0);             b2BodyDef swordDef;             swordDef.fixedRotation = true;             swordDef.type = b2_dynamicBody;             swordDef.angle = 1.4f;             b2FixtureDef fdPlayerSword;             FixtureUserData* MyDataSword = new FixtureUserData;             (*MyDataSword).objectType = "sword";             fdPlayerSword.shape = &swordShape;             fdPlayerSword.isSensor = true;             fdPlayerSword.density = 0.00001f;             sword = m_world->CreateBody(&swordDef);             sword->CreateFixture(&fdPlayerSword);             sword->SetAngularVelocity(1);             //Присоединяется             b2RevoluteJointDef rJoint;             rJoint.bodyA = player;             rJoint.bodyB = sword;             joint = m_world->CreateJoint(&rJoint);             //Записывается факт его существования :3             swordsVec.push_back(sword);                       }          if (glfwGetKey(g_mainWindow, GLFW_KEY_I) == GLFW_PRESS)         {             std::cout << sword->GetAngle() << std::endl;         }         Test::Step(settings);          //уголы взмаха меча. От 1.4 до 4.8 радиан         if (swordsVec.size() != 0)         {             if (swordsVec[0]->GetAngle() > 4.8)             {                 m_world->DestroyBody(sword);                 swordsVec.erase(swordsVec.begin());             }         }     }       static Test* Create()     {         return new AAA;     } }; //void* bodyUserData = contact->GetFixtureA()->GetBody()->GetUserData(); class MyContactListener : public b2ContactListener { public:     void BeginContact(b2Contact* contact) override     {         if (((reinterpret_cast<FixtureUserData*> (contact->GetFixtureA()->GetUserData().pointer))->objectType) == "player")         {             if ((reinterpret_cast<FixtureUserData*> (contact->GetFixtureB()->GetUserData().pointer))->objectType == "box")                 isBoxContactPlayer = 1;         }     }      void EndContact(b2Contact* contact) override     {         if (((reinterpret_cast<FixtureUserData*> (contact->GetFixtureA()->GetUserData().pointer))->objectType) == "player")         {             if ((reinterpret_cast<FixtureUserData*> (contact->GetFixtureB()->GetUserData().pointer))->objectType == "box")                 isBoxContactPlayer = 0;         }     } };  static int testIndex = RegisterTest("Benchmark", "AAA", AAA::Create);