Avoid that 2 objects appear colliding

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

Hi there! I have a problem that I dont know how to solve. I’ve created in my game a spawn area in which there are different objects appearing randomly. My problem is that some times the objects appear really close each others and sometimes they overlap too.

I tried to solve it putting a collision shape to the objects that I’m instancing, but I dont know that didn’t work (using the apropiate nodes types). I think that maybe using a function like “is_colliding” or something like that I can make that the object moves until it is not colliding with other object, but there are not a function called like that (I tried all the different physics objects).

Also the problem is that when they appear (when they are instancied) using signals it dont detect the collision because it is already inside the shape. (Sorry for my english jaja it is not very well).

Can someone help me? Thanks!

Do you use layer and layer mask?

Jimmy | 2019-03-19 09:32

Yes, but all of them have the same layer because they are instancied objects. I mean, they have a different layer that the player has, this way the player don´t collide with them, but the objects have all the same layer and mask because they are the same object but instancied several times :smiley:

Thortilla | 2019-03-19 09:38

:bust_in_silhouette: Reply From: flurick

In the Platformer 2D Demo this is used in player.gd to not collide as soon as projectiles are instanced into the scene:

if Input.is_action_just_pressed("shoot"):
	var bullet = preload("res://bullet.tscn").instance()
	...
	bullet.add_collision_exception_with(self)

There are other functions for managing collisions in PhysicsBody2D.
If they are RigidBody2D there is a contact_monitor that can be enabled.

If nothing of that helps please sharing your project so we get some more context of your question.