Area2d Triggering Physics Collision?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tyo

So, I’m making a platformer and am currently working on enemy-player collisions. I figured the best way to do this was to make Physics collisions and damage collisions different types of collisions - Physics collisions will generally use kinematic bodies and be for interacting with the environment primarily, while damage collisions will use Area2Ds to detect when two hostile objects come into contact.

This almost works, but for some strange reason, the Area2D nodes are interacting physically. Here’s a gif to show how I mean:

At first, you might be able to see the behavior acting correctly - on the condition that the creature and the player directly collide head-on. However, any other way and the two act as physical barriers to each other. I know the Area2D is doing this, because when I turn off the relevant masks on the Area2Ds, the two objects don’t interact at all.

For clarity, I have both of their physics bodies on distinct layers(a player layer and an enemy layer respectively), and both of them interact with environment masks, and only those masks.
The player area occupies the player layer (along with the player physics body) and only interacts with two masks - enemies and enemy attacks.
Similarly, the creature area occupies the creature layer (again, along with its physics body) and only interacts with two masks as well - the player and player attack masks.

One thing that might be causing it, though I couldn’t imagine why, is that the physics body and the area for each object are using functionally the same collision shape. I duplicated it because I thought it would make sense that the damage hitbox and the collision hit box for each object would be the same.

Is there something I’m missing? Are Area2Ds supposed to behave this way?
I appreciate the assistance.

One additional potentially relevant detail: The Area2Ds are child nodes of the KinematicBody2Ds. Though, I’ve been testing code which makes them sibling nodes(or attempts to do so, at least), and so far I’m getting the same exact results when I seem to re-parent the nodes successfully.

tyo | 2018-11-06 19:38

:bust_in_silhouette: Reply From: tyo

After pondering and experimenting for a while, I think I get it.

It would seem the problem was my initial skeptical hunch. The KinematicBody is detecting the Area2D as a “floor” (or “wall” in the case of trailing behind the creature). This keeps the areas from intersecting. My working solution is just making the Area2D collision box a little bigger than the physics collision box.

This is still pretty weird, since I thought the documentation heavily implied that Area2Ds wouldn’t interact with physics bodies unless you were doing something with an on_body_entered emitter or something. In any case, it’s working properly with that patchwork applied.

:bust_in_silhouette: Reply From: Calamander

Works fine for me:
test example
Maybe you messed up with layers?

Well, my descriptions of the layers is accurate.
I can provide some screenshots:
Imgur: The magic of the Internet

Having compared with yours, the difference seems to be that you’re having the player area on the same layer as the enemy area. I can’t do that for other parts of my game’s logic to work properly(player attacks specifically), but even when I do try, I get the same behavior as before if I use an area hitbox that is equal to or smaller than that of the physics hitbox. The only way it works is if the area hitbox is larger.

Given yours does seem to be working fine though, I seriously have no idea why this isn’t working as expected on my end. I started thinking it was something to do with parenting and inheritance, but your example proves that’s not the case.

Very strange.

tyo | 2018-11-08 16:28

Applied your layers, result still the same. You can check the link in my previous answer, i reuploaded. Im not sure what’s your problem, maybe you can simplify your project, removing unrelated stuff and upload it so I can check?

Calamander | 2018-11-08 17:18

Sorry for the late response, I’m not quite working this full time.
Here’s a simplified project that still exhibits the issue. Currently, the player’s Area2D shape is smaller than the player’s KinematicBody2D’s shape. Making it larger, instead, allows the interaction to fire correctly. Disabling the enemy’s Area2D causes no collision to be detected.

tyo | 2018-11-12 20:44

Alright, so your problem lies here:


in your “Hurt” node. at the expiration of your hit timer you change player’s kinematic body mask bits to monitor “Enemy” and “Enemy attack” bits for collision instead of doing it for area. Now two collision shapes from kinematic body and area2d monitor same bits and most of the time kinematic body wins, because it is parent and process collisions earlier stopping both kinematic bodies and preventing them to get closer and trigger areas collision too.

Calamander | 2018-11-12 21:52

Ohhhh! I feel silly now!

This was a remnant from when I was trying to do all the collisions using the physics collision. I just copied the code over and forgot entirely about that part!

This makes total sense now. Thanks for pointing that out. I removed the offending code and it’s working. I just mentally floated over that text each time, taking it for granted I was probably doing what’s intended there. Bah!

Much appreciated!

tyo | 2018-11-12 22:02