- Home /
Calculate bounds of a box including rotation
hi
I working on collision system for my game i have custom colliders.
I used "http://answers.unity3d.com/questions/777855/bounds-finding-box.html" to create bounding box for collisions.
I am getting problem for the boxes where right and forward values have been used to create box. Otherwise its working fine. Anybody has any idea how to include right and forward vectors in calculating 8 points for the cube.
Below is my code for calculating points.
public static void GetBoundsPointsNoAlloc(BoxRegion bx, Matrix4x4 colliderBox4x4,Vector3[] points, Matrix4x4 mx) {
Transform tr = Utils.FromMatrix4x4(ref DebugCollisionTestSphere.trans,mx);
Vector3 v3Center = bx.center;
Vector3 v3ext = bx.GetExtents();
Vector3 right = bx.right;
Vector3 forw = bx.forward;
//Quaternion qua = Quaternion.LookRotation (bx.forward,Vector3.Cross (bx.forward, bx.right));
// tr.TransformDirection (bx.forward);
//tr.localRotation = qua;
tr.forward = bx.forward;
tr.right = bx.right;
//tr.rotation = qua;
points [0] = tr.TransformPoint (new Vector3 (v3Center.x - v3ext.x, v3Center.y + v3ext.y, v3Center.z - v3ext.z)); // Front top left corner
points [1] = tr.TransformPoint (new Vector3 (v3Center.x + v3ext.x, v3Center.y + v3ext.y, v3Center.z - v3ext.z)); // Front top right corner
points [2] = tr.TransformPoint (new Vector3 (v3Center.x - v3ext.x, v3Center.y - v3ext.y, v3Center.z - v3ext.z)); // Front bottom left corner
points [3] = tr.TransformPoint (new Vector3 (v3Center.x + v3ext.x, v3Center.y - v3ext.y, v3Center.z - v3ext.z)); // Front bottom right corner
points [4] = tr.TransformPoint (new Vector3 (v3Center.x - v3ext.x, v3Center.y + v3ext.y, v3Center.z + v3ext.z)); // Back top left corner
points [5] = tr.TransformPoint (new Vector3 (v3Center.x + v3ext.x, v3Center.y + v3ext.y, v3Center.z + v3ext.z)); // Back top right corner
points [6] = tr.TransformPoint (new Vector3 (v3Center.x - v3ext.x, v3Center.y - v3ext.y, v3Center.z + v3ext.z)); // Back bottom left corner
points [7] = tr.TransformPoint (new Vector3 (v3Center.x + v3ext.x, v3Center.y - v3ext.y, v3Center.z + v3ext.z)); // Back bottom right corner
}
Answer by kblood · Jun 06, 2020 at 08:07 AM
Probably easier to go through all the vertices instead and find the topmost, leftmost, bottommost and rightmost of them. That way you can use TransformPoint on each of them and get their actual point in real world space.
Example:
var meshes = this.transform.GetComponentsInChildren<MeshFilter>();
if (!meshes.Any())
{
Debug.Log("No gltf meshes found.");
return;
}
// Recalculate min Y value now that the mesh has been rescaled
float minY = meshes.SelectMany(m => m.mesh.vertices).Min(v => transform.TransformPoint(v).y);
print("Lowest y: " + minY);
// Snap the mesh to the floor
transform.position = new Vector3(transform.position.x, transform.position.y - minY, transform.position.z);
Just ignore the snapping part. If you want the topmost go with:
float maxY = meshes.SelectMany(m => m.mesh.vertices).Max(v => transform.TransformPoint(v).y);
And then use x and z to get the other points. Better a late answer than no answer I guess.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045354im_/https://answers.unity.com/themes/thub/images/avi.jpg)