0
votes

In Box2d, as much as i understood, objects have a default collision response, which lets them stop movement or bounce back (if you set a restitution).
Also you can say, that the object is a sensor and so you can implement your own collision response.
Now i would like to use the default collision response for some kinds of collisions, while i want to have full controll of collisions with specific objects.
For example: A player should stop movement or bounce back, if he collides with a wall, but it should loose Hp, when he collides with a bullet and the bullet should be removed.
So i would like to have something like the contact filters, but the objects, which do not match the filter should react with the default behaivor.

Is something like this possible?
How can i do that?

Thanks

2

2 Answers

1
votes

Use a CollisionListener and check the user data of your fixtures/bodies for the type. Then you can handle it however you want. If you don't want the player/bullet collision to have a response, you should disable the Contact in preSolve (via Contact.setEnabled(false)) and mark the bullet to be destroyed.

Having a powerful collision handling system is necessary in this case. I've created a gist which shows how I do it via socalled CollisionHandlers. which are responsible for each type of EntityA vs EntityB collisions. The CollisionFilter can already pre-filter the collisions in total and the CollisionListener will then determine the correct CollisionHandler and forward the callbacks to those. It uses reflection and might not be the fastest approach with a lot of bodies and collisions, but so far I didn't ran into any problems here.

You can find the gist here.

1
votes

This basics is this :

http://pastebin.com/9vzZUbTT

All your fixture's user data should have a own specific name.

This works for me!

The entity class contains the body and fixture.

Edit: Might not what you want to have, but it's something

Edit2 : http://pastebin.com/Cw0R7hmq Something like this, there's probably alot of compile error, but I hope you get what I'm trying to say.