- Home /
Local Bounds, or not AABB bounds
The bounds for the collision object are in world space and axis aligned (AABB). I need to get the 8 vertices of the real bounding box for a calculation, not axis aligned. I could get them easily transforming the local bounding box. Is there a way to get this?
I could use this as well. Some random thoughts: You could find the center (or centroid?) of the points, and find the bounding cylinder easily enough. This give you $$anonymous$$/max Y, but then how to find optimal box on the Y plane? What if you sorted the verts by distance from Y, draw a line between the two most distant ones (ones with greatest separation from each other). Would that give you an axis? Then look for verts $$anonymous$$/max distance perpendicular to that axis in both directions? $$anonymous$$ore clues here: http://gis.stackexchange.com/questions/22895/how-to-find-the-$$anonymous$$imum-area-rectangle-for-given-points
Answer by Ricardo Arango · Dec 05, 2009 at 05:09 PM
Ok, found the solution.
The 8 points in world space of the mesh bounding box which is in local space, can be obtained as follows:
// var rb : Rigidbody
// var obj: GameObject
var mesh : Mesh = obj.GetComponent(MeshFilter).sharedMesh;
if(mesh){
var min = mesh.bounds.min;
var max = mesh.bounds.max;
var transform = rb.transform;
var point1 = transform.TransformPoint(Vector3(min.x, min.y, min.z));
var point2 = transform.TransformPoint(Vector3(min.x, min.y, max.z));
var point3 = transform.TransformPoint(Vector3(min.x, max.y, min.z));
var point4 = transform.TransformPoint(Vector3(min.x, max.y, max.z));
var point5 = transform.TransformPoint(Vector3(max.x, min.y, min.z));
var point6 = transform.TransformPoint(Vector3(max.x, min.y, max.z));
var point7 = transform.TransformPoint(Vector3(max.x, max.y, min.z));
var point8 = transform.TransformPoint(Vector3(max.x, max.y, max.z));
}
Wouldn't this only work if the mesh's center is at (0, 0, 0)? Wouldn't you use Bounds.max and Bounds.$$anonymous$$?
The above looks to solve a similar problem. For an actual BoxCollider, use Vector3 SZ = transform.GetComponent<BoxCollider>().size;
The size is end-to-end, so pos=SZ/2
is one corner. Use TransformDirection(pos) to go from world to local. Add transform.position
to get final world values.
Answer by grojguy · Feb 08, 2010 at 11:48 AM
Yes, but it seems the problem here is, this only works as expected if the mesh itself is AA (axis-aligned) when imported (or if it is a Unity Primitive...like a Cube). If it is not, it seems that the world-space bounding-box will not "fit" the object like you would expect. In other words, it seems like the renderer.bounds are basically the same as what the above post gives you...the local-to-world translated mesh bounds. I could be missing something, but IF this is the case, I have to say this is not useful at all.
You are correct, but almost every 3d model is made axis aligned. It would be very weird for someone to make a model non axis aligned.
$$anonymous$$odeled, yeah a lot of times, but so many times I've seen them fairly randomly aligned (and scaled for that matter). That said, if you're doing some AR thing (like HoloLens or Tango) and you're scanning in the model, you got no reference. This was my issued.
Your answer
Follow this Question
Related Questions
Does using "half res" in the quality settings reduce texture RAM usage? 1 Answer
Android device graphics problem !!! 1 Answer
Unity equivalent of ARB_occlusion_query (glGetQueryObject)? 0 Answers
Why does my game require so much rendering power? 1 Answer
Does Unity support NVIDIA frame synchronizing technology? 1 Answer