How Are Physics Affected by Objects on Other Layers?

: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’ve got an enemy RigidBody2D on Layer 1 / Mask 1, while the player KinematicBody2D is on Layer 0 / Mask 0. The StaticBody2Ds they stand on are on both Layers and Masks 0 and 1.

The enemy has an Area2D on Layer 0 which detects when the player enters and applies a fixed impulse which causes it to jump.

It jumps roughly the same height every time, unless the player enters that area from above, in which case it jumps half that height every time.

Here’s a video of it in action: you can see that the enemy does clip through the player cleanly when landing.

Can anybody think of common or expected reasons why PhysicsBody2Ds which should be on separate layers appear to be affecting each others’ physics?

:bust_in_silhouette: Reply From: mateusak

The difference between masks and layers are:

The mask decides which layers it’ll collide.
The layer decides which masks will collide with it.

This means that your player will collide with the enemy because this enemy is in Layer 1. At least that is what I’ve seen in my experiences. Related:

https://forum.godotengine.org/3020/collision-masks-and-its-propper-uses
https://forum.godotengine.org/4010/whats-difference-between-collision-layers-collision-masks

Edit:
Then it’s a error somewhere in the code you made to make a KinematicBody interact with a RigidBody. I’m certain you did a interaction code, right?

Sorry, sorry, serious typo on my part! The Player is Layer 0 / Mask 0 and the Enemy is Layer 1 / Mask 1.

Though it did take me a while to wrap my head around the differences between Layers and Masks, so that is a helpful post.

Hammer Bro. | 2016-09-09 23:56

I’ll scour the code in case I missed something, but by design the KinematicBody2D should not interact with the RigidBody2D.

I have Area2Ds on each object that handle interactions, that way they can specifically stay separate on the physics layers but still detect overlaps of each other. The Area2Ds are on the same layer and behave as expected. The PhysicsBody2Ds ought to be unrelated.

I’ll see if I can reproduce this phenomenon with a more minimal example.

Edit I’m still digging in, but it’s interesting that the RigidBody2D behaves as expected if I set can_sleep to false. Or, bizarrely, if I set_sleeping(true) just before the apply_impulse call.

Hammer Bro. | 2016-09-14 18:21