AABB bounding box in 3D

Godot Version

4.2.2.stable.mono

Question

MY QUESTION IS SIMILAR TO THIS, BUT NOT EXACTLY THE SAME

Hello, i am trying to get a bounding box on my interactible objects, so far i have got only to positioning the bounding box to the right place, but one thing that bothers me and i cant figure it out is the scale of the bounding box.

I am aiming for something similar on how the game VOTV has it done

how it looks in VOTV:

I want to achieve a similar effect, i tried using AABB, but failed, I might just be stupid enough to not understand it

TLDR; Have a 2D NinePatchRect on a canvas layer that gets scaled accordingly to the 3D bounding box, taking rotation into account (eg. if the object is tall from front, bounding box is also tall, when viewed from top, its smaller, always fitting the bounding box of the 3d Object)

An AABB alone is axis-aligned to cover the object without transform. So that will not match to your view or the object rotation and scale and will not cover well more complex objects that are not just boxes or circles.

What you need to do for any shaped object is get all the relevant corner positions of your object, loop through them and convert them to global positions, then map those 3D positions to your camera and 2D canvas, and then keep the max left, top, right, bottom and that is what you use to draw your 2D rect.

The issue that you linked basically has the basic solution already using an AABB but if you want a better view aligend box you just dont use an axis-restricted AABB but manual defined corner positions.

so from what i understand i need to manually add the corners to my object, that could be simple enough.

But how could i go about the second part? making the 2d box around that, as shown in the video

But how could i go about the second part? making the 2d box around that, as shown in the video

You use your current 3D camera and convert the global 3D positions of your corners to 2D positions on your canvas. Then you filter for the max left, top, right, bottom and draw a 2D texture of that size, either with one of the Control node types or by using the Node2D _draw() API or the RenderingServer. If you want the rect to hug the object very tight you need also calculate your camera angle to correct positions for your object height, else you have just a rotated bounding box.

1 Like

coming back to this, as i am very uncertain how to do the UI part

Right now I have made it so that i have all 8 corners defined, and then i get the 4 closest to the camera, which works as i would like it to.

I am can’t seem to be smart enough to make it so i can draw a rect around it, and also i would preferably like to not draw the box manually, but have a NinePatchRect with a custom texture on it and scale that to fit between the points and also rotate it accordingly.

If someone could help me, it would be very apriciated :slight_smile:

Hi, i am new to Godot, but i did same thing in UnrealEngine some years ago. Easiest way to do this for me was to cast trace to the object along vertical and horizontal axis in view space with this object(up and right camera vector as direction and end point is object position) and then transforming collision points from wolrd space to view space. Those collision points will lay on desired rectangle, so simple min(coll_up.x,coll_down.x) and so on will get you all coordinates of it.