• 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 vargonian · Dec 30, 2013 at 10:45 AM · boxcollider

How to convert a BoxCollider2D bounds to world space?

A BoxCollider2D has members called size and center, and these are supposedly in "local" coordinates. Is there an easy way to convert this to world coordinates, or even better, a way to get the world bounds Rect of a BoxCollider2D directly?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vargonian · Dec 31, 2013 at 04:04 AM

I couldn't find a super-easy way, but I created this extension method for BoxCollider2D which accomplishes the task:

 public static Rect GetWorldBounds(this BoxCollider2D boxCollider2D)
 {
     float worldRight = boxCollider2D.transform.TransformPoint(boxCollider2D.center + new Vector2(boxCollider2D.size.x * 0.5f, 0)).x;
     float worldLeft = boxCollider2D.transform.TransformPoint(boxCollider2D.center - new Vector2(boxCollider2D.size.x * 0.5f, 0)).x;
 
     float worldTop = boxCollider2D.transform.TransformPoint(boxCollider2D.center + new Vector2(0, boxCollider2D.size.y * 0.5f)).y;
     float worldBottom = boxCollider2D.transform.TransformPoint(boxCollider2D.center - new Vector2(0, boxCollider2D.size.y * 0.5f)).y;
 
     return new Rect(
         worldLeft,
         worldBottom,
         worldRight - worldLeft,
         worldTop - worldBottom
         );
 }
Comment
Add comment · Show 1 · 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 Venryx · Jan 09, 2015 at 09:38 AM 0
Share

What happens if the object's rotation makes it upside down? Wouldn't worldTop then be less than worldBottom, resulting in a bounds-with-negative-size.y/rect-with-negative-height?

avatar image
0

Answer by Venryx · Jan 09, 2015 at 09:32 AM

I haven't thoroughly tested it yet, but here's a 3D version that you could modify to work for 2D:

 // transform
 public static Bounds TransformBounds(this Transform self, Bounds bounds)
 {
     var center = self.TransformPoint(bounds.center);
     var points = bounds.GetCorners();
 
     var result = new Bounds(center, Vector3.zero);
     foreach (var point in points)
         result.Encapsulate(self.TransformPoint(point));
     return result;
 }
 public static Bounds InverseTransformBounds(this Transform self, Bounds bounds)
 {
     var center = self.InverseTransformPoint(bounds.center);
     var points = bounds.GetCorners();
 
     var result = new Bounds(center, Vector3.zero);
     foreach (var point in points)
         result.Encapsulate(self.InverseTransformPoint(point));
     return result;
 }
 
 // bounds
 public static List<Vector3> GetCorners(this Bounds obj, bool includePosition = true)
 {
     var result = new List<Vector3>();
     for (int x = -1; x <= 1; x += 2)
         for (int y = -1; y <= 1; y += 2)
             for (int z = -1; z <= 1; z += 2)
                 result.Add((includePosition ? obj.center : Vector3.zero) + (obj.size / 2).Times(new Vector3(x, y, z)));
     return result;
 }

You then can use it as follows:

var worldBounds = transformWithLocalBounds.TransformBounds(localBounds);

Comment
Add comment · Show 1 · 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 joergzdarsky · May 28, 2017 at 10:10 AM 0
Share

Venryx , have you continued testing your code?

I am asking because I am currently looking into a similar problem because I reimplement for an own engine a larger number of Unity3D classes into 64bit double precision pendants. I currently work on the Bounds and BoxCollider class.

In the Unity docs it says Bounds is used by Collider.bounds, $$anonymous$$esh.bounds and Renderer.bounds". Looking further Collider.bounds and Renderer.bounds are in worldspace while $$anonymous$$esh.bounds is in localspace. So I thing internally the Bounds class generally works in localspace, and also all bounds-setters generally expect localspace variables. But when reading bounds via getter, I guess in Collider and Renderer bounds is transformed to worldspace. So I am thinking about the right way to do transform the bounds there too.

I agree the center should be transformed via TransformPoint (so center is transformed by position, rotation, and scale).

However I am not sure if the bounds (edge points in your code, or in my case "Class Bounds variable Vector3d extends") should be transformed by TransformPoint also. $$anonymous$$y concern is that bounds is generally AABB thus always axis-aligned, and should not rotate. However TransformPoint will rorate. So I wonder if we just have to apply the scale (worldscale) property of the transform to the points (or at least to extents of the Bounds class)?

 /// <summary>
 /// The WORLD SPACE bounding volume of the collider.
 /// </summary>
 /// <remarks>
 /// Note that this will be an empty bounding box if the collider is disabled or the game object is inactive.
 /// </remarks>
 public Bounds bounds
 {
     get {
         Bounds result = this.m_bounds;
         result.center = transform.TransformPoint(result.center);
         result.extents = new Vector3d(result.extents.x * this.transform.scale.x, result.extents.y * this.transform.scale.y, result.extents.z * this.transform.scale.z);
         return result;
     }
     set { this.m_bounds = value; }
 }
















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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Box collider fell "inside" into another one (Collision) 3 Answers

How to use Box Collider limited Camera in a Area?? 0 Answers

boxcollider fixed bounds 1 Answer

Box Collider Trigger not working 1 Answer

Collider on prefab no longer loading 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