Is there a way to check for collisions in the same frame an object is created?

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

I am trying to make a 2D find the object game, where I am generating a lot of objects at random positions and I don’t want them to overlap.

I have tried using intersect shape but I couldn’t get it to detect the collisions without waiting for the next physics frame.

Here’s the code I wrote to check the overlapping objects

		creature.position = get_random_position()
		yield(get_tree(), "physics_frame")
		var space_state := get_world_2d().direct_space_state
		var shape :=
		var collision_shape = creature.get_collision_shape()
		shape.collide_with_areas = true
		shape.transform = collision_shape.global_transform
		shape.exclude = [creature]
		var intersections = space_state.intersect_shape(shape, 1)
		overlapping = not intersections.empty()


You’re not the first one to have had this problem. It seems that isn’t possible.

You’d have to spawn one per frame but if you want to spawn many that’s going to take a little while.

A faster way is to spawn a bunch of areas first without caring if they overlap. Then next frame you remove all those that overlap and try to respawn them. Then repeat that cycle until none overlap.

If there is a way to force the space state to update I’m curious to know how.

Zylann | 2022-03-04 13:59