Godot Version
4.5.1
Question
Is it possible to use GetAabb() on a Node3D?
The bounding box data must be available, as the yellow visual representation is displayed in the editor.
4.5.1
Is it possible to use GetAabb() on a Node3D?
The bounding box data must be available, as the yellow visual representation is displayed in the editor.
There’s nothing in Node3D that could constitute a bounding box. You need to go one step ahead in inheritance chain to VisualInstance3D. This class implements the bounding box.
I couldn’t get VisualInstance3D to work from a Node3D, so I switched the node type to VisibleOnScreenNotifier3D. I’m not sure this was the ideal approach, but at least GetAabb() works now.
VisualInstance3D is an abstract base class. You can’t instantiate it directly.
What exactly are you trying to make here?
I’m trying to get the object boundaries ingame.
public partial class GMOnScreen : VisibleOnScreenNotifier3D
{
private Aabb GetCombinedAabbRecursive(Node node)
{
Aabb combinedAabb = new Aabb();
bool first = true;
foreach (Node child in node.GetChildren())
{
if (child is Node3D node3D)
{
Aabb childAabb;
if (node3D is VisualInstance3D visualChild)
{
childAabb = visualChild.GetAabb();
if (childAabb.Size.LengthSquared() > 0.0001f)
{
Aabb transformedAabb = node3D.GlobalTransform.Inverse() * childAabb;
if (first)
{
combinedAabb = transformedAabb;
first = false;
}
else
{
combinedAabb = combinedAabb.Merge(transformedAabb);
}
}
}
Aabb grandchildrenAabb = GetCombinedAabbRecursive(child);
if (grandchildrenAabb.Size.LengthSquared() > 0.0001f)
{
combinedAabb = combinedAabb.Merge(grandchildrenAabb);
}
}
}
return combinedAabb;
}
private List<Vector3> CalculateGlobalAabbCorners()
{
Aabb finalAabb = GetCombinedAabbRecursive(this);
Transform3D globalTransform = GlobalTransform;
List<Vector3> globalCorners = new List<Vector3>();
for (int i = 0; i < 8; i++)
{
Vector3 localCorner = finalAabb.GetEndpoint(i);
Vector3 globalCorner = globalTransform * localCorner;
globalCorners.Add(globalCorner);
}
return globalCorners;
}
public override void _Ready()
{
base._Ready();
List<Vector3> corners = CalculateGlobalAabbCorners();;
GD.Print($"Calculated Global corners: {corners.Count}");
foreach (var item in corners)
GD.Print($"Global corner: {item}");
}
}
You don’t need VisibleOnScreenNotifier3D for that. You can run the recursion from any type of 3d node, including Node3D
Node3D doesn’t have AABB.
VisualInstance3D inherits Node3D, normalized was telling you to get a node which inherits VisualInstance3D VisualInstance3D — Godot Engine (stable) documentation in English then you get the AABB.
![]()
Thanks for the replies, @normalized and @ngf!
However, I couldn’t get Node3D.GetAabb() to work.
Could someone, please, share an example of how to use it?
Why would you need Node3D::GetAabb()?
What’s the point of this script extending VisibleOnScreenNotifier3D?
It never calls GetAabb()on itself. It just recursively iterates over children and calls GetAabb()on nodes that inherit VisualInstance3Dand thus have the bounding box. This script doesn’t need to be attached to VisibleOnScreenNotifier3Dwhatsoever. It can run on Node3Djust fine.