This is very placeholder code, but here’s the general idea to anyone stumbling upon this.
First, the reason AABB is not recommended is because its axis aligned which means you can’t rotate it. A solution I found was to create a points holder, then generate a marker3d for every point in the AABB while the initial object is unrotated (or its rotation matched the AABB) and then rerotate the object. You put this all in your objects ready function, and now you have a constant reference to a bounding box that matches your objects transform.
points = Node3D.new()
add_child(points)
var current_rot := global_rotation
global_rotation = Vector3.ZERO
var aabb : AABB = mesh.global_transform * mesh.get_aabb()
aabb.size = aabb.size * mesh.global_transform.basis.get_scale()
for i in 8:
var new_point := Marker3D.new()
points.add_child(new_point)
new_point.global_position = aabb.get_endpoint(i)
global_rotation = current_rot
The solution I ended up going with however since it is a fair bit cleaner was the get every point of your mesh and determine the smallest and largest vector2.
var top_left_x : float = 1000
var top_left_y : float = 1000
var bottom_right_x : float = 0
var bottom_right_y : float = 0
var mdt := MeshDataTool.new()
mdt.create_from_surface(current_object.mesh.mesh, 0)
for i in mdt.get_vertex_count():
var point := cam.unproject_position(current_object.mesh.global_transform * mdt.get_vertex(i))
if point.x < top_left_x:
top_left_x = point.x
if point.y < top_left_y:
top_left_y = point.y
if point.x > bottom_right_x:
bottom_right_x = point.x
if point.y > bottom_right_y:
bottom_right_y = point.y
var points := [Vector2(top_left_x, top_left_y), Vector2(bottom_right_x, bottom_right_y)]
if previous_points[0] - points[0] >= (Vector2.ONE * 0.1) or previous_points[0] - points[0] <= -(Vector2.ONE * 0.1):
previous_points = points
SignalManger.new_cursor_size.emit(points)
Keep in mind, this is all just placeholder stuff. I haven’t taken into account any optimization and am just trying to push the feature out. I hope this helps anyone in need!