Chris Bradfields book / Question Tutorial / Escape the maze

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


to better understand how the Godot engine works, I started to learn from Chris Bradfields (fantastic) book. Has anyone else tried the ‘Escape the Maze’ tutorial?
My question is:
In the original code the enemies overlap and do not collide with each other.
I can’t find a good solution to modify the enemies overlapping behavior.
How can I make them collide with each other?

:bust_in_silhouette: Reply From: kidscancode

When you say “collide” you need to be a bit more specific. It’s not clear what exactly you want to change.

All of the entities in this game are Area2D nodes. As written in the book, we use collision layers to make the enemies not “see” each other. Instead you can set the “Collision Mask” of the enemy to include the “enemies” layer as well. If you do this, then one enemy overlapping another will trigger the “area_entered” signal.

If what you mean by “collide” is that the enemies will not move into each others’ square (just as they do with the walls), then you instead need to change the collision behavior of the RayCast2D nodes that are used to validate movement. In the book project, these rays only collide with the “walls” layer. If they also include the “enemies” layer then they will prevent movement into a square occupied by another enemy.

Thank you for the quick response! And sorry for the (too easy) beginner question

In my question, I meant the second paragraph of the answer (raycast).
I had already tried that on my own (raycast flags), but it doesn’t work 100%. I thought I had misunderstood something.

With the raycasts set, the enemies collide and it seems to work. But
in level 2 (or earlier) 2 of the enemies overlap and stop moving (stand still).

Maybe it’s because of the inaccurate (not 100%) CollisionShape?

Or is it better to use the “area_entered” trigger (and emit a signal)? But what would the “ememy_process-function” look like?

iuaj7 | 2018-10-15 18:42

It may be a case of two enemies moving into the same empty square. The ray wouldn’t detect a collision for either one since the square is empty. Once you fix that problem, you could have the situation where the enemies get bunched up in a spot because they keep trying to move into each other - in a corner for example. This is why I left the enemies able to move through each other for this project and avoid the extra complexity.

kidscancode | 2018-10-16 23:08

That’s too bad. It’s such a nice example and you do not have to write much code.
My first own little game has the same problem (Area2D Collisions). I will try the KinematicBody2D. There is also an interesting option in the “Tilemap->Collisions->Use Kinematic”.
Nevertheless many thanks

iuaj7 | 2018-10-17 17:25