+1 vote

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()
Godot version 3.4.2
in Engine by (13 points)

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.