Sensor but with collision

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.
Meevere
Prole
Posts: 5
Joined: Sun Mar 05, 2023 7:37 pm

Sensor but with collision

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

Re: Sensor but with collision

Post by togFox »

I would turn on sensors and then use categories to detect or ignore the events that are unimportant to you.
Last edited by togFox on Fri Jul 28, 2023 9:04 am, edited 2 times in total.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Sensor but with collision

Post by darkfrei »

Use two rectangles in the same object: one for contact detection (larger) and one for collision solving (smaller).
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Sensor but with collision

Post 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.
Meevere
Prole
Posts: 5
Joined: Sun Mar 05, 2023 7:37 pm

Re: Sensor but with collision

Post 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
Meevere
Prole
Posts: 5
Joined: Sun Mar 05, 2023 7:37 pm

Re: Sensor but with collision

Post 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?
Meevere
Prole
Posts: 5
Joined: Sun Mar 05, 2023 7:37 pm

Re: Sensor but with collision

Post 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?
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Sensor but with collision

Post 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.
Meevere
Prole
Posts: 5
Joined: Sun Mar 05, 2023 7:37 pm

Re: Sensor but with collision

Post 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
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Sensor but with collision

Post 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
Last edited by dusoft on Sun Jul 30, 2023 9:29 am, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 0 guests