Page 1 of 1

Box2d: fixture:setsensor(false) causes collisions?

Posted: Tue Feb 21, 2023 9:34 am
by togFox
My Box2D code has a bunch of objects moving about on the screen and for reasons I can't explain, beginContact is invoked regardless of sensor enabled or disabled.

In other words, fixture.setSensor(false) triggers the beginContact and that is counter-intuitive. Infact, with further debugging, I do this:

Code: Select all

local function beginContact(a, b, coll)

        local aindex = a:getUserData()    -- this is simply a handle/index to the global array
        local bindex = b:getUserData()

	print(PHYS_PLAYERS[aindex].fixture:isSensor(), PHYS_PLAYERS[bindex].fixture:isSensor())

        ...
The majority of the time (not not every time), isSensor = false on a and b. What am I not understanding? Why is a collision created when sensor = false?

Re: Box2d: fixture:setsensor(false) causes collisions?

Posted: Tue Feb 21, 2023 12:09 pm
by Bigfoot71
If I understood the problem correctly, the difference between a sensor and a non-sensor fixture is that sensors generate collision callbacks but they don't generate a physical collision response. From what I understand in your case you expect beginContact to be called only when the fixtures are sensors so if you need to differentiate between sensor and non-sensor fixtures in your beginContact callback you can just use the fixture:isSensor() method to check if each device is a sensor or not.

Is that right?

Re: Box2d: fixture:setsensor(false) causes collisions?

Posted: Tue Feb 21, 2023 12:47 pm
by togFox
Correct.

So, you can see my print statement - I need to test that to understand if the callback was invoked by an active sensor?

Code: Select all

function beginContact
    Gets called when two fixtures begin to overlap.

Is a sensor on a fixture even needed to trigger beginContact?

I'll do more testing to get my head around this as I was expecting setsensor(false) to not trigger the callback.

Re: Box2d: fixture:setsensor(false) causes collisions?

Posted: Tue Feb 21, 2023 3:59 pm
by knorke
from https://love2d.org/wiki/Fixture:setSensor
Sensors do not cause collision responses (they pass through other objects instead of bouncing off), but the beginContact and endContact World callbacks will still be called for this fixture.
Is a sensor on a fixture even needed to trigger beginContact?
No.
Maybe it is the word "sensor" that confuses you?
It means the body will not physically collide with other bodies but you will still get the callbacks if they overlap.

Example usage, a photoelectric barrier: If player walks through this area then open a door.
The player should not bounce of a photoelectric barrier or be blocked from moving through a radar screen or similar. But the collision callback functions will still be called so that the game can trigger some response.

Re: Box2d: fixture:setsensor(false) causes collisions?

Posted: Tue Feb 21, 2023 9:01 pm
by togFox
So setsensor(false) won't bounce but callback is still invoked?

If I need special logic in the callback for true and false sensors then I need to test that in the callback and act appropriately?

If so, then I'll review my code.

Thanks!