I'm trying to spawn area2Ds in random places on the screen periodically, only thing I'm having a problem with is making sure that they don't spawn one on top of each other. I'm trying to use a space state query but my code doesn't work. Heres the code in Gdscript:
func _on_Timer_timeout():
var virusNode = virus.instance()
var space_state = get_world_2d().get_direct_space_state()
var query = Physics2DShapeQueryParameters.new()
query.set_shape(virusNode.get_shape(0))
var result = [1]
while result.size():
var x = rand_range(virusNode.get_child(0).get_texture().get_width()/2, Globals.get("display/width") - virusNode.get_child(0).get_texture().get_width()/2)
var y = rand_range(virusNode.get_child(0).get_texture().get_height()/2, Globals.get("display/height") - virusNode.get_child(0).get_texture().get_height()/2)
virusNode.set_pos(Vector2(x, y))
query.set_transform(virusNode.get_transform())
result = space_state.intersect_shape(query)
add_child(virusNode)
the result array is always empty, even when the spawned node intersects another one. The spawned nodes have collisionBody2D under the area2D too. the query also recognises the shape as a circle correctly, and gets transform coordinates. This is part of the script that attaches to the root node of the scene.