How to use Collision Layer/Mask in Godot4? (or is it bugged?)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GrandNecro

I’ve been following tutorials on how to use bit layer-mask, but none of it seems to work… Here’s my setup:

layer:  bit3=1
mask:   bit4=1


#enemy (not working)
layer: bit4=1
mask:  none
#enemy (not working)
layer: bit3=1, bit4=1
mask:  none
#enemy (not working)
layer: bit3=1, bit4=1
mask:  bit3=1

#enemy (not working)
layer: bit3=1, bit4=1
mask:  bit4=1

It’s supposed to print out a statement when the area collide. The statement never printed for any of these configurations.

The only setup that works is when I turn on both bit 3 and 4 of the enemy’s mask:

#enemy (working)
layer: none
mask:  bit3=1, bit4=1    #BOTH bits must be high. why?

and yes, both bit 3 AND 4 must be active for the collision to be detected. if either bits is pulled down to 0 then the collision will not be detected and no print statement will occur.

Did Godot4 change how layer and bitmasking work or is this a bug? Where do I go to see known bugs? I’d like to report it if it’s actually a bug.

Here’s the code for the enemy if it helps btw:

func _on_hurtbox_area_entered(area):
  for body in $hurtbox.get_overlapping_bodies():
:bust_in_silhouette: Reply From: aXu_AP

IIRC Godot 4.0 does change logic behind collision layers. While in 3.x you can use layers and masks interchangeably, in 4.0 layer decides can the body be collided with (ie. world should always be on layer) and mask decides if the body can collide with objects in that layer (ie. player and enemy colliders should have mask set, and layer only if they should collide with each other). This system is more flexible, and for me at least, more intuitive.

Same logic goes with areas. Since you want player to be noticed, it should be in layer (in this case, it is on layer 3). Enemy hurtbox should have corresponding mask active. Looking at your examples, enemy 3 should also work, so I’m not sure what’s going on there. I recreated scene as such and it worked. Try investigating your setup a bit more or recreating experiment with smaller scale. Maybe enemy has collider which doesn’t allow it’s hurtbox to overlap with player?

Ahhh. I finally found the problem. It was the line get_overlapping_bodies().

The interacting nodes were areas, not bodies so I had to use get_overlapping_areas() instead. Thanks for the help

GrandNecro | 2022-09-10 22:22