Godot Version
4.3
Question
So I’m trying procedural generation with a ‘dungeon’ where each room have differents ‘exits positions’ and the main problem is that sometime, 2 rooms are generated and are overlapping. Of course I thought about Area3D. Each room have an Area3D being the soil but it doesn’t work. I tried putting on_area_entered() signal but nothing trigger it and I tried get_overlapping_areas() and nothing work either. I wanted to make the generation in a main gdscript file where each room are instantiate and then I look up for possible overlapping and if there is I try another evit position. please help I started yelling alone in the dark.
Could you show your generation code
@tool
extends Node3D
@export var generate_dungeon: bool = false : set = trigger_generation
@export var reset_dungeon: bool = false : set = trigger_reset
@export var attempts : int = 10
@export var nbRooms : int = 20
const ROOMS : Array = [
preload("res://scenes/rooms/DungeonRoom_1.tscn")
]
var all_exits_positions : Array = []
var all_generated_rooms_areas : Array = []
func trigger_generation(value: bool):
if value:
update_preview()
generate_dungeon = false
func trigger_reset(value: bool):
if value:
reset_preview()
reset_dungeon = false
func _ready() -> void:
update_preview()
func reset_preview():
for child in get_children():
child.queue_free()
all_exits_positions.clear()
all_generated_rooms_areas.clear()
func update_preview():
reset_preview()
generate_main_room()
generate_rooms()
func generate_rooms():
for i in nbRooms:
var next_room = get_random_position()
generate_room(0, next_room.global_position, next_room.global_rotation)
func generate_main_room():
var main_room = generate_room(0, Vector3(0, 0, 0), Vector3(0, 0, 0))
func generate_room(index : int, position : Vector3, rotation : Vector3):
var new_room = ROOMS[index].instantiate()
add_child(new_room)
new_room.global_position = position
new_room.global_rotation = rotation
var exits : Array = new_room.get_node("exits_folder").get_children()
all_exits_positions.append_array(exits)
var area = new_room.get_node("SquareSoil")
all_generated_rooms_areas.append(area)
#print(check_collision())
if check_collision(area):
print("Overlapping")
func get_random_position():
all_exits_positions.shuffle()
return all_exits_positions.pop_back()
func check_collision(target : Area3D) -> bool:
for area in all_generated_rooms_areas:
print(area.overlaps_area(target))
if area.get_overlapping_areas().has(target):
return true
return false
Ok I figured that Area3D are sh*t for my code. Instead I Used AABB with the basic constructor AABB(position, size) instantiate with my CollisionShape3D and with the intersects method. Works perfectly fine.
1 Like