Box2d: fixture:setsensor(false) causes collisions?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
togFox
Party member
Posts: 835
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Box2d: fixture:setsensor(false) causes collisions?

Post 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?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post 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?
My avatar code for the curious :D V1, V2, V3.
User avatar
togFox
Party member
Posts: 835
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
User avatar
knorke
Party member
Posts: 275
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

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

Post 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.
User avatar
togFox
Party member
Posts: 835
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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!
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests