Detecting if a 3d object encloses another?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Anilforextra

So, I want to detect if an object encloses other object i.e if an object is inside another object(CollisionShape in an ‘Area’ node).

One of the forums had the similiar type of discussion, where it mentioned to use encloses method from AABB class.

So, I came with this.

var OwnShapeOrigin = $CollisionShape.global_transform.origin
var OwnShapeExtents = $CollisionShape.shape.extents
var OwnShapeAABB = AABB(OwnShapeOrigin , OwnShapeExtents)

var TargetShapeOrigin = $Target/CollisionShape.global_transform.origin
var TargetShapeExtents = $Target/CollisionShape.shape.extents
var TargetShapeAABB = AABB(TargetShapeOrigin , TargetShapeExtents)

if OwnShapeAABB.encloses(TargetShapeAABB):
       print("Object is inside the other object")		

This is wrong.

The problems with this solution:
So,the problem is when you scale their parent nodes, they are not affected because the extents are same.I guess I have to use their scale values and not extents.

AABB class takes the origin and the scale values only:
So, when the cube collision shapes are scaled to look like rectangles and rotated,this doesn’t work.

So,I guess this is the wrong approach.
So, What is the best way to detect if an object is completely inside another?

:bust_in_silhouette: Reply From: Bernard Cloutier

I’m not sure how to do this, just spitting out some ideas off the top of my head.

You could try multiplying the *ShapeExtents by the global_transform. However, you’d need to remove the translation component. Something like this:

var OwnShapeOrigin = $CollisionShape.global_transform.origin
var OwnShapeExtents = $CollisionShape.shape.extents
var MyGlobalTransform = Transform($CollisionShape.global_transform.basis, Vector3(0,0,0))
OwnShapeExtents  = MyGlobalTransform.xform(OwnShapeExtents)
var OwnShapeAABB = AABB(OwnShapeOrigin , OwnShapeExtents)

This might take care of your non-local scaling issue. Although if the parent is rotated, I assume it could cause issues since AABB are axis-aligned.

Thank you for trying.
I couldn’t figure it out,though.

Anilforextra | 2020-02-22 15:38