Page 1 of 2

Sensor but with collision

Posted: Tue Jul 25, 2023 7:22 am
by Meevere
I'm using built in physics engine for physics in my game, but I found out that there's either option to have no physical collision at all but detecting them (being a sensor), or having categories and groups support but no option to turn off physical collision (besides manually setting to each contact that it's disabled)

Is there a way to solve this problem? To be more concrete - I'm making a horizontal scroller shooter, and I need to prohibit collisions with enemies (because if I collide it feels funky) but I still need to detect collisions with them so I could apply damage to the player. And there are still some obstacles, so solution via making everything a sensor wouldn't work

Re: Sensor but with collision

Posted: Thu Jul 27, 2023 10:05 am
by togFox
I would turn on sensors and then use categories to detect or ignore the events that are unimportant to you.

Re: Sensor but with collision

Posted: Thu Jul 27, 2023 10:19 am
by darkfrei
Use two rectangles in the same object: one for contact detection (larger) and one for collision solving (smaller).

Re: Sensor but with collision

Posted: Fri Jul 28, 2023 7:49 am
by dusoft
I would recommend using love.physics that allows sensors. Sensors will still trigger onCollision callback, but won't affect physics, so you are all set.

Re: Sensor but with collision

Posted: Sat Jul 29, 2023 8:07 am
by Meevere
dusoft wrote: Fri Jul 28, 2023 7:49 am I would recommend using love.physics that allows sensors. Sensors will still trigger onCollision callback, but won't affect physics, so you are all set.
Sensors do not allow for any physical collisions, but they do call onCollision callbacks, I still need the option to make entities collide with walls

Re: Sensor but with collision

Posted: Sat Jul 29, 2023 8:26 am
by Meevere
darkfrei wrote: Thu Jul 27, 2023 10:19 am Use two rectangles in the same object: one for contact detection (larger) and one for collision solving (smaller).
Wouldn't it lead to need of dynamic changing of categories/masks?

Re: Sensor but with collision

Posted: Sat Jul 29, 2023 8:31 am
by Meevere
I've found the post with the same question, although there wasn't an answer there in the end viewtopic.php?p=241751
The only viable solution seems to be making something like my own category bits in userData of fixture and then checking then at collision and disabling it. But that seems strange that to get this simple behavior you need to manage it yourself. Is there no other solution?

Re: Sensor but with collision

Posted: Sat Jul 29, 2023 8:50 am
by dusoft
Meevere wrote: Sat Jul 29, 2023 8:07 am
dusoft wrote: Fri Jul 28, 2023 7:49 am I would recommend using love.physics that allows sensors. Sensors will still trigger onCollision callback, but won't affect physics, so you are all set.
Sensors do not allow for any physical collisions, but they do call onCollision callbacks, I still need the option to make entities collide with walls
Yeah, so you can use sensors for enemies and normal colliders for walls.

Code for collisions is well explained here:
https://love2d.org/wiki/Tutorial:Physic ... nCallbacks

So you will need to create separate fixtures for your sensor-only colliders (*dynamic*, because enemies can move) and your physical types such as walls (those are *static* in addition).

Code: Select all

fixture:setSensor(true) -- for collider without physical effect that will still launch onCollision callback
E.g. I am using sensors for detecting boat moved in space around a dock (rectangle, static, sensor) and normal fixture for a wooden dock/platform (rectangle, static, not a sensor). Boat can hit the dock and bump, but it can not hit the space around the dock that is just used for detection. Once boat is detected by the sensor (onCollision) and speed is zero, boat can board passengers on/off.

Re: Sensor but with collision

Posted: Sat Jul 29, 2023 4:04 pm
by Meevere
dusoft wrote: Sat Jul 29, 2023 8:50 am
Meevere wrote: Sat Jul 29, 2023 8:07 am
dusoft wrote: Fri Jul 28, 2023 7:49 am I would recommend using love.physics that allows sensors. Sensors will still trigger onCollision callback, but won't affect physics, so you are all set.
Sensors do not allow for any physical collisions, but they do call onCollision callbacks, I still need the option to make entities collide with walls
Yeah, so you can use sensors for enemies and normal colliders for walls.

Code for collisions is well explained here:
https://love2d.org/wiki/Tutorial:Physic ... nCallbacks

So you will need to create separate fixtures for your sensor-only colliders (*dynamic*, because enemies can move) and your physical types such as walls (those are *static* in addition).

Code: Select all

fixture:setSensor(true) -- for collider without physical effect that will still launch onCollision callback
E.g. I am using sensors for detecting boat moved in space around a dock (rectangle, static, sensor) and normal fixture for a wooden dock/platform (rectangle, static, not a sensor). Boat can hit the dock and bump, but it can not hit the space around the dock that is just used for detection. Once boat is detected by the sensor (onCollision) and speed is zero, boat can board passengers on/off.
I guess I've badly explained this, but I still need, in such situation, enemies to physically collide with walls. If they are marked as sensors they would phase right through. I've marked my bullet entities as sensors and they just pass through any other entity even if it's a wall.

So, to be more precise, in simplest situation, I need to have three entities - A, B, and W (wall), A and B collide with wall physically (bounce off, do not phase through it) and A and B need to be able to pass through each other with callbacks on, i.e. no collision but being able to detect if they are overlapping

Re: Sensor but with collision

Posted: Sat Jul 29, 2023 5:44 pm
by dusoft
Meevere wrote: Sat Jul 29, 2023 4:04 pm So, to be more precise, in simplest situation, I need to have three entities - A, B, and W (wall), A and B collide with wall physically (bounce off, do not phase through it) and A and B need to be able to pass through each other with callbacks on, i.e. no collision but being able to detect if they are overlapping
Ah, I see. Maybe these functions could help? Setting same mask for both player and enemy wouldn't make them collide physically, but the onCollision callback should still work.
https://love2d.org/wiki/Fixture:setMask
or
https://love2d.org/wiki/Fixture:setFilterData