- Home /
Question by
Noxury · Jul 22, 2020 at 05:30 PM ·
quaternionorientationpointboundingbox
Check if point is inside OBB without Collider?
I am trying to solve how I can check if an arbitary point is inside an oriented bounding box, without using colliders. Right now I have the following method, where
p is the point in worldspace
center is the bounding box position in worldspace,
extends is the half size of the bounding box
rotation is the orientation of the bb, meaning Quaternion.identity would be axis aligned
.
public static bool InsideBox(Vector3 p, Vector3 center, Vector3 extends, Quaternion rotation){
Matrix4x4 m = Matrix4x4.TRS(center, rotation, 2*extends);
p = m.inverse.MultiplyPoint3x4(p);
//p = rotation * p - center;
return p.x <= extends.x && p.x > -extends.x
&& p.y <= extends.y && p.y > -extends.y
&& p.z <= extends.z && p.z > -extends.z;
}
But it seems it is still Axis Align checking. Then, I created a Unit Test to debug the output:
public Transform pointForBB;
void OnDrawGizmos(){
bool pointInsideBB = VectorExtensions.InsideBox (pointForBB.position, transform.position, transform.localScale*0.5f, transform.rotation);
Gizmos.color = Color.cyan;
Gizmos.DrawSphere (pointForBB.position, .1f);
Gizmos.color = pointInsideBB ? Color.cyan : Color.red;
Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
Matrix4x4 cubeTransform = Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale);
Gizmos.matrix *= cubeTransform;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
Gizmos.matrix = oldGizmosMatrix;
}
Comment
Best Answer
Answer by Noxury · Jul 22, 2020 at 06:27 PM
Changed to
Matrix4x4 m = Matrix4x4.TRS(center, rotation, Vector3.one);
This worked.