How can I display AABBs/Bounding Boxes for debugging purposes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Leakbang

How can I see the created AABBs while running the game? I need to be able to see them inorder to debug them.

Could you elaborate on what are your AABBs? AFAIK there’s no built-in AABB nodes. If they are colliders, you can show them under Debug/Visible Collision Shapes.
Edit: ah, you speak about bounding boxes in title. So do you mean VisualInstance’s get_aabb? I’m not sure if there’s a way to show bounding boxes automatically, but suppose you can write a script for that.

aXu_AP | 2021-10-19 03:08

:bust_in_silhouette: Reply From: aXu_AP

Here’s what I came up with assuming you mean VisualInstance’s aabbs. Set target to parent of nodes which you want to show aabbs of.

class_name DrawAabbs
extends ImmediateGeometry

export(NodePath) var target
# How far recursive search goes. -1 = everything
export(int, -1, 10) var subchildren_levels = -1

var rand = RandomNumberGenerator.new()

func _ready() -> void:
	if target is NodePath:
		target = get_node(target)
	var mat = SpatialMaterial.new()
	mat.vertex_color_use_as_albedo = true 
	set_material_override(mat)


func _process(_delta):
	clear()
	# Give random colors, but similiar each frame by initializing with same seed
	rand.seed = get_instance_id()
	draw_aabbs(target, subchildren_levels)


func draw_aabbs(node : Node, recursive := -1):
	# Draw aabb if there is one
	if node.has_method("get_transformed_aabb") and node != self:
		var aabb = node.get_transformed_aabb()
		var c = Color(rand.randi())
		c.a = 1
		draw_aabb(aabb, c)
	# Then draw aabbs of children
	if recursive != 0:
		for child in node.get_children():
			draw_aabbs(child, recursive - 1)


func draw_aabb(aabb:AABB, color:Color) -> void:
	begin(Mesh.PRIMITIVE_LINES)
	set_color(color)
	set_normal(Vector3(1, 0, 0))

	# Bottom
	add_vertex(to_local(aabb.position))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 0, 0)))

	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 0, 0)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 0, 1)))

	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 0, 1)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 0, 1)))

	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 0, 1)))
	add_vertex(to_local(aabb.position))
	
	# Top
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 1, 0)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 1, 0)))

	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 1, 0)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 1, 1)))

	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 1, 1)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 1, 1)))

	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 1, 1)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 1, 0)))
	
	# Sides
	add_vertex(to_local(aabb.position))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 1, 0)))
	
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 0, 0)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 1, 0)))
	
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 0, 1)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(0, 1, 1)))
	
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 0, 1)))
	add_vertex(to_local(aabb.position + aabb.size * Vector3(1, 1, 1)))
	end()
	

Thank you for the detailed answer. I found an add-on that easily allows me to draw debug boxes.

Leakbang | 2021-10-19 07:51

That’s cool. Well, at least I got some experience in 3D, as I haven’t tried it earlier :slight_smile:

aXu_AP | 2021-10-19 08:32

:bust_in_silhouette: Reply From: Leakbang

I found this add-on by Zylann that easily allows me to do this.

:bust_in_silhouette: Reply From: stormreaver

In the IDE, select Debug/Visible Collision Shapes.

That option does not display bounding boxes.

Leakbang | 2021-10-19 12:27