- Home /
Axis Aligned Bounding Box Vs Object Orientated Bounding Box
Hi guys,
I'm trying to get the closest point on the bounds of one object to another game object. This is what I am currently trying out:
public class object1 : MonoBehaviour { void OnDrawGizmos() { Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach (Collider hit in colliders)
{
if( hit.CompareTag("Block") )
{
Gizmos.color = Color.red;
Gizmos.DrawSphere( hit.ClosestPointOnBounds(transform.position), 0.1f );
}
}
}
}
All this gives me is the AABB (Axis Aligned Bounding Box) of the hit object. Unity Uses the OOBB (Object Aligned Bound Box) for it's rigidbody collisions, but using hit.rigidbody.ClosestPointOnBounds
only gives the AABB again.
Is there a way to get the closest point on the OOBB that isn't computationally expensive?
Or at the very least accessing the OOBB which is used by the rigidbody?
Answer by Statement · Apr 05, 2011 at 12:16 PM
I think you are misunderstanding the terms box collider and bounding box.
A (axis aligned) bounding box is used for quick culling of objects. This is the bounds you're trying to get the closest point from with that call, not the actual box collider data.
you could do like this though:
// ...
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach (Collider hit in colliders)
{
if( hit.CompareTag("Block") )
{
var dir = hit.transform.position - transform.position;
var ray = new Ray(transform.position, dir.normalized);
RaycastHit rayhit;
if (hit.Raycast(ray, out rayhit, dir.magnitude)) {
// rayhit.point now contains the closest point
Gizmos.color = Color.red;
Gizmos.DrawSphere( rayhit.point, 0.1f );
}
}
}
// ...
Unfortunately while this does get the closest point on the surface of the colliding object to Object1, the point is in a line to the centre of the object.
As an example I've created a basic scene in which there is a box with a scale of x = 2, y = 10, z = 2 and a rotation of 45 degrees in the z axis. When Object1 gets close, the red sphere gizmo is not the closest point. :(
Answer by Ray-Pendergraph · Apr 05, 2011 at 12:05 PM
I am not sure how the Unity engine performs collision detection but you could try using the AABB reported by Mesh.bounds.
Your answer
Follow this Question
Related Questions
Create and divide a bounding box into cells 0 Answers
Possible to Give Rigid bodies collision bounding boxes? 0 Answers
OnMouseDown not firing if character isn't touching ground 0 Answers
Move object with mouse without passing through colliders 0 Answers
Vertical push doesn't let the object fall down instantly 1 Answer