hi, would someone explain how to use it? I have 2 nodes of enemy and player both kinematicBody2D. does collision take place in collision/layer or collision/mask? and how do I set it up? I want to have both nodes detect collision when they touch each other. any help is welcomed.

Its a little confusing and I often have to remind myself how it works when I need it, I believe that collision layers are the layers that the object is on. In contrast, collision masks are the layers that the object will react to, but it does not necessarily have to be a part of.

For example, if Node_A is on layers 1 and 2, and is on masks 3 and 4, then it will react to any collision shapes on 3 and 4. Node_B, if it has a mask of 1 and/or 2, then it will be able to react to Node_A. If Node_B's layer is 5, it will not be noticed by Node_A, since it's mask doesn't include mask 5.

Hopefully that helps a bit. I always find the layer/mask system confusing and have to tinker around to remind myself how it works.

Edit: To answer your question of how to have both nodes detect each other, they need to share a layer and mask. For example, if both nodes have layer 3 and mask 3, then they will be able to detect each other.

this explanation kinda explain but not what i was looking for, but based on your example

		collision layer		collision mask

node_a 1,2 3,4 <-- in open world, node_a will collide with other node that have collision mask either layer 3 or 4 checked node_b 1 1,2 node_b 1 5

node_a will not be able to detect collision with node_b at all

my scattered info from the net tell me that a physic body will exist on the collision_layer (why is there so many layer i don't know) but the collision mask is the layer that the physic body will look for collision if checked

i'm not sure if the enemy or player exist in the same collision_layer will detect collision. i can't verify that yet since i don't know the command to play around in the script yet but it something like this.

if node_a.collision_layer == something do something

anyone want to share any info please chip in

   
nod
e_a is in collision layer 1,2 and mask 3,4 <-- in open world, node_a will collide with other node that have collision mask either layer 3 or 4 checked node_b collision layer 1 and mask 1,2 node_b collision layer 1 and mask 5

is this the way to detect collision? it say the identify player isn't declared in the scope if player.collision_mask == enemy.collision_mask: print('i collide with the enemy')

oh i forget the $ for player and enemy

As long as both nodes share the same collision layer and mask, they will be able to collider with each other.

To detect collision, I would recommend using an Area2D node or a function like RigidBody2D's get_colliding_bodies function. If you want to know what thing you have collided with, you'll want to add something to the collision body that can help indicate it. For example, you could add const IS_ENEMY = true to the enemy script and then in the code do something like if "IS_ENEMY" in node_collided_with:, which will return true only if the IS_ENEMY variable exists in the node (node_collided_with in the example).

2 years later