Collisions Across CanvasLayers

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

I’m working on a game with a handful of CanvasLayers (for custom parallax purposes). I’d like the player to be able to interact with certain objects, but it seems that by default collisions aren’t reported between layers.

If I have an Area2D_1 on CanvasLayer_1 and an Area2D_2 on CanvasLayer_2, I can connect the “area_enter” signal for both of them but neither will report on each other, even though they will report on other Area2Ds within their own CanvasLayer.

Is there any way to monitor collisions across CanvasLayers?

Edit: It looks like separate CanvasLayers have separate World2Ds. I’m willing to delve into Physics2DDirectSpaceState related answers if need be.

:bust_in_silhouette: Reply From: avencherus

I did this with a RigidBody in one CanvasLayer, and then a StaticBody inside another CanvasLayer. The code here is written inside that StaticBody that I used, and it acts as a simple floor.

What you do is use the Physics2DServer to assign a new space to the body. You pull the RID from the body, and the RID from the World2D of the desired CanvasLayer. Though I don’t know what other considerations or side effects this might have in your design.

extends StaticBody2D

func _ready():
	var target_layer = get_parent().get_parent().get_node("CanvasLayer")
	Physics2DServer.body_set_space(get_rid(), target_layer.get_world_2d().get_space())

Brilliant, thanks! I had to use Physics2DServer.area_set_space, but the idea’s the same.

Much better than my interim solution of having the Area2Ds reparent themselves to the gameplay layer then follow their old parent manually.

Hammer Bro. | 2016-12-31 16:16

You’re welcome, glad it helped.

avencherus | 2017-01-01 03:44

hi avencherus, i have a similar question (or not) could you have a look at it please?

The link : setting parallaxLayer collision - Archive - Godot Forum

Thank you in advance.

mokalux | 2017-02-04 00:25

:bust_in_silhouette: Reply From: mokalux

hi avencherus, i have a similar question (or not) could you have a look at it please?

The link : setting parallaxLayer collision - Archive - Godot Forum

Thank you in advance.

commented 1 hour ago by mokalux