- Home /
Size of collider bounding box regardless of rotation
My question is, how do I compute the smallest possible box that entirely contains my collider, in local coordinates ? I could use the collider.bounds.size
value, but the problem is that this value is always aligned to the world coordinates. This means that, if my boat is rotated on Y by 45 degrees in world coordinate, this box is going to be much bigger than if the boat wasn't rotated at all. I would like to find the size of the smallest bounding box having the same rotation as my collider. How can I do that ?
Here is more details about the context of my problem : I've written a "drag" scripts that computes more realistic drag physics than the default Unity drag property, which isn't good enough for boats. For that I have a "wall" of points that is constantly ahead of an object, regardless of the direction it is moving to. I.e if it goes forward, the wall is ahead of it and if it goes backward, the wall is behind it. That "wall" contains X * Y points from which I am casting rays in the opposite direction of the velocity. The ray casts hitting the object will apply a drag force on it, and those who don't hit it will not apply any force. I made that "wall" big enough so that it will always be at least as big as would be the projection of the object on the wall for any possible rotation. It's side length is collider.bounds.size.magnitude
. However, most of the time, only a small portion of the ray casts are going to hit the object. For example, if I have a narrow boat going forward, then there is probably only 10% - 20% of the ray casts going to hit the ship, so there are plenty of ray casts which I could avoid doing in order not to waste performance. I would like to have some "begin X", "end X", "begin Y" and "end Y" values so that I only cast rays from a sub-rectangle of that wall that would be big enough. For that I would like to know the size of the "rotated" bounding box of my collider.
EDIT : I thought about doing this :
// Save the original object rotation to restore it later.
Quaternion rotationBackup = gameObject.transform.rotation;
// Set a 0, 0, 0 rotation to the object in order to reduce as much as possible the size of its AABB.
gameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
// Get the size of the smallestAABB.
Vector3 aabbSize = collider.bounds.size;
// Restore the original rotation.
gameObject.transform.rotation = rotationBackup;
But I'm not sure whether it will work because I don't know if changing the rotation of the object will also update collider.bounds.size immediately or if that value is only updated later.
EDIT : See the comments below the accepted answer for more information.
Answer by FortisVenaliter · Jul 09, 2017 at 03:22 PM
The best way to do so, especially if you know it's a box collider, is to calculate each of it's 8 corners, use Transform.TransformPoint to move them to world space, then calculate the min/max for each of the vectors three components.
Thanks for your help. I have all the 8 points of the rotated bounding box. But now I'm a bit stuck. What do you mean by "then calculate the $$anonymous$$/max for each of the vectors three components." ? EDIT : Do you mean transfor$$anonymous$$g these 8 points to world space, inverse transfor$$anonymous$$g the 8 points from world space to the transform of my "wall", and from there, finding the $$anonymous$$ and max X and Y values ? I forgot to mention, but my wall has a Transform object "stolen" from a dummy game object created on the fly.
Loop through them. Here's some pseudocode to get you in the right direction:
Vector3 $$anonymous$$ = Vector3.$$anonymous$$axValue, max = Vector3.$$anonymous$$inValue;
for each cornerPt
{
$$anonymous$$.x = $$anonymous$$in($$anonymous$$.x, cornerPt.x)
max.x = $$anonymous$$ax(max.x, cornerPt.x)
// etc for y and z
}
Then your $$anonymous$$ and max variables will define an AABB perfectly bounding your unaligned BB
I'm not sure I understand what you're explaining to me, but you did put me into the right direction. I finally did what I wanted to do. The main idea was to compute the size of the "object-aligned" bounding box (i.e local size), compute the 8 vertices of the box, converting these 8 points from object-local to world coordinates, making the wall transform look at the object, converting these 8 points from world to wall-local coordinates, using the $$anonymous$$ and max values of the X and Y coordinates of each point to find the begin/end X/Y values. Thanks for the help !
Your answer
Follow this Question
Related Questions
How to project the position of a point relative to a rotated collider using its bounds? 1 Answer
"Recalculate" the mesh bounds to the objects rotation? 3 Answers
Wheel Collider Rotation Problem 0 Answers
Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer