Reputation: 4523
I have a game in which the player must jump over objects floating in water. I implemented all game objects with box2D and now I need to control collisions between them. I have a WorldController class that is where a instantiate all the objects and a buoyancy controller and I have also a custom ContactListener but as I have multiple contact possibilities (player foot sensor contacting with other object, an object contacting with the water, etc.) I'm searching for a good way to organize all the code.
How do you guys control all collisions? Do you use filters? Do you use listener interfaces to notify actors?
Upvotes: 0
Views: 378
Reputation: 2560
When handling collisions you might wrap your Actors/Objects into a GameObject class or alike that has some kind of GameObjectType
. This way you can define the different objects, like the player, water, ground, floating-thingy etc...
Within your ContactListener you can then check by referring to that type. For linking your fixtures and bodies to your Actors just use the setUserData() method.
Then in the ContactListener you can check in the beginContact() method for example if the Object behind FixtureA is of type "Player" and the Object behind FixtureB is "Water"... As reaction, you can let the game end because the player is drowning or whatever you want.
Upvotes: 1