This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+7 votes

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 driectspacestate 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?

in Engine by (234 points)
edited by

1 Answer

+6 votes
Best answer

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.

by (1,608 points)
selected by

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)
enter image description here

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.