|
|
|
|
Attention |
Topic was automatically imported from the old Question2Answer platform. |
|
Asked By |
Vododovoodvod |
I would like to procedurally spawn objects which are basically Area2D nodes with circular collision shapes. This Areas should not overlap.
I am not cure how to do check if certain circular surface of the scene is free of existing collision shapes.
|
|
|
|
Reply From: |
timothybrentwood |
Using math
Assuming your Area2D is it’s own scene with CollisionShape2D as its child:
var area_2d_scene = preload("res://default_env.tres")
func spawn_units():
var objects_spawned = []
var num_to_spawn = 10
while objects_spawned.size() < num_to_spawn:
var potential_spawn_location = get_random_vector_within_bounds()
var can_place = true
for object in objects_spawned:
var object_radius_squared = pow(2 * object.get_node("CollisionShape2D").shape.radius, 2)
if potential_spawn_location.distance_squared_to(object.position) < object_radius_squared:
can_place = false
break
if can_place:
var new_object = area_2d_scene.instance()
new_object.position = potential_spawn_location
self.add_child(new_object)
objects_spawned.append(new_object)
Few things to note: using a while loop is generally inadvisable here. You would need to implement get_random_vector_within_bounds()
. This assumes all objects have the same radii.
Thank you for your help!
Vododovoodvod | 2021-05-03 18:27