Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Gokudomatic2 | |
Old Version | Published before Godot 3 was released. |
In PhysicsBody and PhysicsBody2D, I see there since a few months a new attribute called Mask (PhysicsBody / Collision / Mask). I noticed that a body collides with another body if they have a layer in common or a mask in common. But then, if there’s already a collision layer, what’s the purpose of the mask? Alas the documentation is yet incomplete about this subject.
Related: https://forum.godotengine.org/3020/collision-masks-and-its-propper-uses
Bojidar Marinov | 2016-05-14 10:40
I still haven’t seen a good explanation of the difference.
Diet Estus | 2018-06-11 02:17
Here’s a really good way to think of it:
- The Layer tells us “I exist on” the following layer(s)."
- The Mask tells us “I will collide with items that exist on the following layers.”
So if we have
Player: Layer 1, Mask 2
Enemy: Layer 2, Mask 1,3
and
Bullet: Layer 3, no mask
then the Player will collide with an Enemy only, Enemies will collide with Bullets and the Player but not each other, and Bullets will only collide with what is set to collide with them (which in this case is Enemies, since the Bullet’s (“I exist on”) Layer (i.e., 3) is the same as one of Enemy’s (“I collide with items that exist on”) Masks (i.e., 1 and 3).
(A collision between Object 1 and Object 2 happens when either Object 1’s Layer == Object 2’s Mask, or Object 1’s Mask == Object 2’s Layer, or both.)
Speak2Animals | 2021-05-24 19:13