0
votes

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.

1
Just process all the different collisions you need to in that single ContactListener for your world.obataku
But then how can I refer to the individual object that was in the collision? Say an enemy hits a wall, how do I tell that SPECIFIC enemy to change direction? I can't refer in a static way.Joel Auterson
Contact.getFixtureA/B() will retrieve the two fixtures in contact. Fixture.getBody() will retrieve the Body object associated with the fixture, and, if you associate the Enemy or Door as a user data with a Body, use Body.getUserData() to retrieve it.obataku
Is there any way to differentiate between A and B? And will this refer in a non-static way?Joel Auterson
I will do, thank you. If you'd like to add all this as an answer, I'll mark this solved? :)Joel Auterson

1 Answers

3
votes

You can use your single ContactListener to manage the entire world; Contact.getFixtureA/B() will return the fixtures involved in the contact. You can utilize Fixture.getBody() to get the associated Body with each collision fixture; if, for example, your Door and Enemy objects are associated with the Bodys as user data, then you can use Body.getUserData() to retrieve that.