2D Collision - ignore certain objects

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

Hi,

I have two kinematic bodies (kbody1, kbody2), and both have a collision shape/polygon.
In the world there are other object too (static/rigid bodies), and they also have their own collision polygons.

I want the 2 kinematic bodies to collide with all objects in the world, except each other.

So the main goal is that kbody1 never collide with kbody2.

Is there any way to do this in Godot?

Thank you!

:bust_in_silhouette: Reply From: guppy42

You could use layers

put kbody1 in layer 1
put kbody2 in layer 2
put everything else in layer 1 and in layer 2

should solve the problem

Incase you don’t know where to set then layer this is where;
This is where you find the setting

I will try layers, thanks!

But I don’t understand your third instruction:

put everything else in layer 1 and in layer 2

How can be an object on two different layers at the same time?

bruteforce | 2017-02-25 14:35

Ahh, I got it!
Looks good! I will try it, and come back!

Thanks!

bruteforce | 2017-02-25 14:47

Problem solved with the layer settings! Thanks again!

bruteforce | 2017-02-25 16:07

Thanks! This worked for me as well. However can you explain what the checkboxes for Layers and Masks mean? I only set the layers in both of my bodies like in the picture.

jjphung | 2017-04-10 06:32

:bust_in_silhouette: Reply From: Warlaan

While I would always recommend using layers as much as possible (since they can improve performance and help avoid bugs) in this case it may be the simpler solution to use the add_collision_exception_with()-method that allows you to specify a physics body that a physics body is supposed to not collide with.

As soon as you have multiple exceptions I would recommend layers, but since this seems to be about one and only one exception this may be easier.

I have multiple exceptions. I just tried to simplify the issue with the 2 bodies.
But good to know that there is another solution, thank you!

bruteforce | 2017-02-25 19:59

:bust_in_silhouette: Reply From: eons

You need to use layers and masks, layers are where the objects are detected for collisions and masks where they look for collision.

The idea is to match layer and mask of the monitored and monitoring nodes (to use Area wording)


If your “world” is on layer 1, the special bodies need mask on 1 to detect it.

But you don’t want theese bodies detect each other, so they can’t be on layer 1, remove all the layers on them.


Now, if your game world or specific parts of it need to detect the special bodies, add layer on 2 to the bodies and mask on 2 to the detecting nodes too.


With 1 and 2 I mean the checkboxes on the inspector, look the docs to see how to use the methods.

ps: as of Godot 2.1.2, the 3D version, KinematicBody, is not using masks, just layers, and Area2D may look for both in some cases, do some tests first.

Thanks for the explanation!

bruteforce | 2017-02-26 16:24