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

0 votes

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

Godot version
3.3.4

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.

0 votes

Best answer

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

https://github.com/Zylann/godot_debug_draw

+1 vote

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()
```

- All categories
- Engine 33,345