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
in Engine by (24 points)

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.

3 Answers

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

by (24 points)
+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()
by (1,100 points)

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

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

–1 vote

In the IDE, select Debug/Visible Collision Shapes.

by (511 points)

That option does not display bounding boxes.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.