4
\$\begingroup\$

Consider a Main.tscn with:

(1) a player:

+ Player extends Area2D |_ AnimatedSprite |_ CollisionShape2D 

The player has:

func _on_Player_body_entered(body): hide() 

(2) a falling rock:

+ Rock extends RigidBody2D |_ AnimatedSprite |_ CollisionShape2D 

(3) a trap:

+ Trap extends Area2D |_ AnimatedSprite |_ CollisionShape2D 

The collision between Player and falling Rock (Area2D vs RigidBody2D) makes the player disappear as expected. While, the collision between Player and Trap (Area2D vs Area2D) does nothing. Is it expected?

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

Does collision between two Area2D work differently than collision with Rigidbody2D

Yes.

the collision between Player and Trap (Area2D vs Area2D) does nothing. Is it expected?

Yes.


We have three main PhysicsBody2D types:

  • StaticBody2D: Intended to not move. If you move it by code it causes no collision response (if you want to move it, it is easier to use KinematicBody2D instead). And it does not provide collision response for things colliding into it (handle those collision on things that can collide into it).
  • KinematicBody2D: Intended for you to move it from code. Movement can cause collisions. You are supposed to handle collision response from code.
  • RigidBody2D: Intended to be moved by physics. You can apply forces to change the way it moves, but ultimately physics moves it (to move it directly make sure to set the mode as needed or you will be fighting the physics system, an that is a potential source of glitches). Godot handle collision response by default.

The type Area2D is not in that list because it is not a physics body. Instead both Area2D and PhysicsBody2D are both CollisionObject2D.

A consequence is that the "body_entered" signal in Area2D, will detect StaticBody2D, KinematicBody2D, RigidBody2D but not other Area2D.

It is possible to detect overlapping between Area2D by using get_overlapping_areas.


However, the intended way to do things is to:

  1. Make the player avatar a KinematicBody2D. The icon of KinematicBody2D should be a clue.
  2. Put the collision logic on the Area2D.

That is, the trap (Area2D) would detect the player avatar (KinematicBody2D) and act on it.

That leaves the question of how to handle the rock (RigidBody2D) collision with the player avatar (KinematicBody2D). Well, RigidBody2D also has a "body_entered" signal that you can use for that.

I also mentioned that you can handle collision in your KinematicBody2D, that would be with move_and_collide or get_slide_collision. However, note those are meant to handle collisions are result of the motion of the KinematicBody2D. Thus, when a RigidBody2D (which is handled by physics) hits the KinematicBody2D, you probably want to handle that on the RigidBody2D.

Another advantage of placing the collision response logic on the objects that the player avatar can collide with, is that the player avatar code is not aware of them. As a consequence it is easier to extend the logic of the game by adding more things that response to collision with the player avatar.


More complex collision logic can be archived by having Area2D as child of a KinematicBody2D or using RayCast2D.


Note: "body_entered" will also handle collision with tile with collision shapes from a tilemap even though a tilemap is not a physic body.

\$\endgroup\$
7
  • \$\begingroup\$ I want you to be my mentor! \$\endgroup\$ Commented Apr 26, 2021 at 15:27
  • \$\begingroup\$ The logic for collision you proposed here is not the one used in the first tutorial: docs.godotengine.org/en/latest/getting_started/first_2d_game/…. \$\endgroup\$ Commented Apr 26, 2021 at 16:06
  • \$\begingroup\$ @pietrodito I was unaware of that. Sounds to me we need better tutorials. Perhaps I should try my hand at writing them one of these days. \$\endgroup\$ Commented Apr 26, 2021 at 16:35
  • \$\begingroup\$ twitter.com/nathangdquest made them and he acheived a lot of great games... But I guess he keep some secrets (as he called them in his monetized tutorials) \$\endgroup\$ Commented Apr 26, 2021 at 16:39
  • \$\begingroup\$ @pietrodito Ah, yes, I have seen GDQuest. They'd use KinematicBody2D, see Make Your First 2D Game with Godot. Perhaps it is just that one tutorial on the official docs that should be better. Addendum: see also kidscancode.org/godot_recipes \$\endgroup\$ Commented Apr 26, 2021 at 16:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.