- Edited
Been struggling for past week on how to place nodes on top of each other based on AABB.
So far with @xyz help i got the correct position of the top most extent of child nodes/meshes in node, but i cant figure out how to place the next node above it so that its bounding box starts where the existing bounding box ends.
Going from this:
To this:
THe node that is picked up and placed on top of other nodes could be in any orientation/rotation. And that node could have basepoint in the middle of mesh or at an end of a mesh. So the solution has to take it into account.
THe white plane is placed at the start of the node where all the other objects are placed and each next white plane is placed at the end of the existing extent of AABB. Which is correct, and then i want to place the picked up node on top of the plane, so that its AABB starts where the existing ones ends. Vector3(0,y,0)
func calculate_pickup_position(picked_up_obj:Item):
var total_size:Vector3
var p_aabb = picked_up_obj.item_mesh.global_transform* picked_up_obj.item_mesh.get_aabb()
var p_aabb_o = picked_up_obj.item_mesh.get_aabb()
var p_aabb_c = p_aabb.get_center()
for n in pickup_node.get_children():
if n is Item:
var child:Item = n
var mesh=child.item_mesh
var m_aabb = mesh.global_transform*mesh.get_aabb().abs()
total_size.y = max(total_size.y,m_aabb.end.y) - pickup_node.global_position.y
var pp = PLANE.instantiate()
if pickup_node.get_children().size() == 0:
pickup_node.add_child(pp)
else:
pickup_node.add_child(pp)
pp.position=total_size
p_aabb.position.y=total_size.y
total_size.y = p_aabb.get_center().y
return total_size
THe return value should be the :Item nodes position taking into account its own AABB and positioned i such way that its own AABB starts at the existing nodes AABB end (vertically, along Y axis)
The problem atm is that if the Item node is rotated so that its basepoint is not aligned with its own AABB position (starting corner ) then it is no placed correctly.
My latest attempt was to put the total_size.y value into the picked_up_obj aabb.position, and grab the new center of it, but now i need to somehow offset it from nodes.position. it works if the center position of AABB alighns with the nodes basepoint, but doesnt when basepoint is in different location ( you can see that hats basepoint is at the bottom of the AABB but the turned over potion bottle - 2nd potion, basepoint is about aligned with AABB center. Im lacking brains to do so ... cant wrap my head around ..