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
1
Question by bhupi · Nov 17, 2015 at 02:26 PM · transformvector3quaternionbounds

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
 }

@robertbu

alt text

screen-shot-2015-11-17-at-40929-pm.png (12.8 kB)
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

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.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

c# modify only one axis of a quaternion 2 Answers

How to smoothly rotate to certain directions using input axis 1 Answer

How do I find the correct speed when using Vector3.MoveTowards and Quaternion.RotateTowards together? 0 Answers

Finding direction of objects movement, and playing an anim specific to it 0 Answers

Transform a Vector by a Quaternion 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