Collision without force

Hello everyone,

in my game I often want to if the player collides with an enemy that he loses an lives and becomes temporary invulnerable. Because of this invulnerability I want him to be able to walk through the enemies for short time and the collision should not have any physical effect on the player.

I have some ideas how I can do it, but I think that there are probably better / more straightforward solutions.

My approach would be to have an duplicate version of every fixture.
The first instance of the fixture has a collisioncategory/maskbits, so that it will only collide with things like walls (not enemies!).
The second fixture instance is a sensor, which collides with enemies only.

When the second fixture has a collision (which then must be with an enemy), I will change the collisioncategory/maskbits so that this fixture instance won't collide with anything until the player is vulnerable again.

This approach might work, but it has some serious problems:
- It's complex
- I need to duplicate every fixture
- The type of reaction to a collision (if force should be applied or not) must be defined, before the actual collision happens, so it will be difficult to to change this behaviour based on volatile information

Is there a better way to do this?
Thanks for your help in advance!
Just to clarify, you mean touching the enemies should have no physical response whatsoever, even after the invulnerability expires?
If so you could probably just disable all contacts between them in PreSolve so that the player can always walk through them. Also in the PreSolve you would need to check if the invulnerability timeout had expired, so you can knock off another life if it has.
irresistible force wrote:
Just to clarify, you mean touching the enemies should have no physical response whatsoever, even after the invulnerability expires?


Yes, most times it is true, but the behaviour is not always the same.

So what's the right way to use Presolve?
(I'm using the farseer port for C#, but it claims to be almost the same as plain box2d)

Would I just do
contact.Enabled = false;
and then change the collisioncategories to prevent the contact from showing up again?
And can I then use PreSolve as a replacement for BeginContact to react to collisions?
You don't need to change the collision categories, just disable the contact in PreSolve every time.

BeginContact happens once each time when two things begin to touch, and PreSolve is called every frame during the time they are touching. So if the player is standing in the same place as a monster, you will get many PreSolve calls.

Whatever you are already doing with BeginContact, I think you can just keep it as is, this should not affect it.

Original thread: https://box2d.org/forum/viewtopic.php?t=7969