Collision masks and its propper uses

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By brunosxs
:warning: Old Version Published before Godot 3 was released.

Hi, I’d like to better understand the concept of collisions masks and its difference between collision layer.

It got a little confusing for me as I was working with raycast.
I used a raycast trhough driect_space_state and set it to check on the collision mask 2.

var dds = get_world_2d().get_direct_space_state()
dds.intersect_ray( corner(true).bottom_left, corner(true).bottom_left + Vector2(0,raycast_size.ground_left), [self], 2)

I also made sure to set the collision mask of the object to 2 with set_collision_mask(2) before testing and I get no response.

What am I doing wrong?

:bust_in_silhouette: Reply From: Bojidar Marinov

Consider it the following way: a physics object belongs to some layers, and collides with other objects specified by its collision mask.

E.g. A collides with B if A.layers has any bits in common withB.mask, or if B.layers has bits in common with A.mask (In the code it is done with a simple bitwise-and operation).

But, raycasts aren’t actually physics objects in the same sense. They are detectors, they don’t collide with stuff (except RayCastShapes, but that’s another story). This means that they have only a mask, and no layers, so a body A would be detected by a ray R only if A.layers has bits in common with R.mask (we cannot reverse that statement, since the raycast doesn’t have layers).

So, the problem is that raycasts (by node or by code) match layer masks, not collision masks. This means that if you do set_layer_mask(2) on your object, it would be correctly matched by the raycast.

Finally, if you do think (and need it for a game of yours) that matching objects from raycasts has to be done by both collision layers and masks, feel free to open an issue on the godot repo on github.

Thanks, during yesterday, I kept messing with it and managed to make the collisions happen, through the editor although I didn’t really knew how, it felt random and I was hopping to understand it so I could truly use it. So, I changed the layer_mask of the object randomly(I still don’t understand how I managed to, with the following combination, get a collision mask of 10)

Now, except for that, everything is clear, mainly to the usage of collision masks and how together with collision layers they make collisions happen or not. The built-in documentation although gives a small glimpse that may be useful to seasoned programmers, was not enough for such a complex function that accepts so many vars.

Thanks again.

brunosxs | 2016-04-14 11:02