Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Correct way to use box2d ContactListener for specific contacts?

$
0
0
I need to know when a bullet and a player have contacted. This is the code i am trying to use. The problem is, i get a "access violation reading location 0x000000" error for any collisions, like bullet and wall.

class MyContactListener : public b2ContactListener
{
	void BeginContact(b2Contact* contact)
	{
		//check if fixture A was the player or Bullet
		void* bodyUserDataA = contact->GetFixtureA()->GetBody()->GetUserData();
		void* bodyUserDataB = contact->GetFixtureB()->GetBody()->GetUserData();

		if( static_cast<GameObject*>(bodyUserDataA)->GetGameObjectType() == GOT_PLAYER ) // fixture A is a player
		{
			if( static_cast<GameObject*>(bodyUserDataA)->GetGameObjectType() == GOT_BULLET )// fixture B is a bullet
			{
				static_cast<Player*>(bodyUserDataA)->InflictDamage( static_cast<Bullet*>(bodyUserDataB)->damage );
			}
		}
	}
};


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>