How do I detect instant collision?

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

I’m working on a level generator node. This node places RigidBody2D’s within a predefined area. I don’t want them to overlap so they should be deleted as soon as they collide. If they are not deleted, they will inform their parent node that they have been placed so that the desired number of them will be generated. If I was using the old Godot this solution would have worked:

func _ready():
if is_colliding():
	get_parent().placed += 1

What is the equivalent of this in Godot 3? I don’t have time to call the move_and_slide function as it should be done instant and only once. Maybe there is an alternative way to detect overlapping?

Have you tried using the signals which are a part of theRigidBody2D class? Signals such as body_entered() and body_shape_entered()?

Ertain | 2021-10-26 08:20

:bust_in_silhouette: Reply From: dethland

I think this is the direct way to do it. But if you want to avoid seeing the sprites clipping when they just spawn, you simply hide sprites when spawning them and show sprites after the is_colliding() method.

But is_colliding is not present in Godot 3.

Martik | 2021-10-24 16:44

:bust_in_silhouette: Reply From: skysphr

Set contact_monitor to true and iterate through get_colliding_bodies() to check for incompatible collisions.

I don’t think there is an equivalent for KinematicBody2D’s. I need the same process on them too.

Martik | 2021-10-24 20:26

Kinematic bodies have move_and_collide() which you can use to get information about possible collisions.

skysphr | 2021-10-24 20:45