When thinking of a 2D, angled-camera style game (think Stardew Valley), what is generally the best approach in Godot for full body character collisions vs “leg collision”? For example:
I want the player to be able to walk up to the box up to some point, and the green collision area defines that the player can get close to the box, but not walk on it or get too close to it.
On the other hand, I want the pink ball to bounce off of the players head, so I introduce the yellow collision area. This way, anything that hits the player will bounce off, regardless if they hit the head or the legs. I also want to be notified when this collision happens so I can handle it (player taking damage, for example).
In Godot, however, this isn’t straight forward, since collision is defined through collision layers and masks, which are attached directly to the CharacterBody2D. Meaning I cannot put different collision areas on the player, define each area to interact with different types of objects, and be done with it.
I tried adding a RigidBody2D child onto the player with the “all-encapsulating” collision area, but this led to unwanted behaviour, including the collision area shrinking in size when the game starts, and moving around on its own.
What would be some best practice or options to implement this?
This is what collision layers are for. Two collision areas on different layers will not collide, only collision areas that share a collision layer can collide and thus notify.
I understand, but collision layers are defined on the CharacterBody2D, so I am not able to set two different collision shapes on the same character and define different layers for each collision shape.
Oh, my bad. I misunderstood. Then all You need is an Area2D. So your tree will look something like this:
You can use the shape directly on the character for the feet and the one inside the area for other things, since you probably want to use the feet for movement, should be easier that way.
Thanks! This is something I’ve tried, but the issue here is that Area2D doesn’t simulation collisions, but rather just detects bodies entering/exiting. I want to simulate collisions, e.g. to create the ball bouncing on the character’s head as I described in the original post. Is there a particular way to approach that? Is there a way to have Area2D do that?
Oh, that has to be a RigidBody then. Make sre it does not collide with your character by setting it to a different layer and pin it with a joint so it doesn’t move away.
Thanks! The pinning option seems to work!
It does feel a little hacky. I assume then there is no best-practice in a scenario like this?
That IS how you construct complex RigidBodies out of primitives, in every physics engine I know. If you’ve ever seen g-mod you’ve probably seen joints. You can even have animation Skeleton (aka armature) that interacts with RigidBody through joints. Some floppy clothes are designed that way, for example.