Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by D43DB33F · Jul 09, 2017 at 03:10 PM · rotationcolliderboundsboundingbox

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image D43DB33F · Jul 09, 2017 at 03:31 PM 0
Share

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.

avatar image FortisVenaliter D43DB33F · Jul 09, 2017 at 03:35 PM 0
Share

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

avatar image D43DB33F FortisVenaliter · Jul 09, 2017 at 04:41 PM 1
Share

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 !

alt text

capture.png (371.2 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Problem with wheelJoint2D car rotations - my car needs to be able to do double jumps and mid-air rotations. 0 Answers

Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges