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
while(overlapping):
creature.position = get_random_position()
yield(get_tree(), "physics_frame")
var space_state := get_world_2d().direct_space_state
var shape := Physics2DShapeQueryParameters.new()
var collision_shape = creature.get_collision_shape()
shape.set_shape(collision_shape.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()