I’m developing a little Android game in Java, using AndEngine for graphics and Box2D for physics – specifically, collision handling. I have some different types of objects with constructors in classes, like so:
MainActivity.java
Enemy.java
Npc.java
Door.java
I have a static PhysicsWorld in the main class, and I was setting up a ContactListener from the Enemy class, to define what happens when one of the enemies hits something. However, I tried to set up another ContactListener for the Door class, when I discovered that each PhysicsWorld has only one ContactListener.
Essentially, my question is this: what is the best way to get around this?
I’m aware I’ve probably explained this rather badly, so my apologies.
You can use your single
ContactListenerto manage the entire world;Contact.getFixtureA/B()will return the fixtures involved in the contact. You can utilizeFixture.getBody()to get the associatedBodywith each collision fixture; if, for example, yourDoorandEnemyobjects are associated with theBodys as user data, then you can useBody.getUserData()to retrieve that.