+1 vote

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:

https://i.imgur.com/Qbyx07s.gif

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.

in Engine by (53 points)
edited by

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.

2 Answers

+1 vote
Best answer

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

by (145 points)
selected by

Well, my descriptions of the layers is accurate.
I can provide some screenshots:
https://imgur.com/a/XN5jSnU

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.

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?

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.
Here.

Alright, so your problem lies here:

host.set_collision_mask_bit(3,false);
host.set_collision_mask_bit(4,false);

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.

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!

0 votes

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 onbodyentered emitter or something. In any case, it's working properly with that patchwork applied.

by (53 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.