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 se · May 21, 2016 at 05:50 PM · rotationquaternionvertices

Calculating where vertices would be without rotation

I have a rotated object (a box collider in that case) and I want to calculate where its vertices would be, if it weren't rotated.

My failed attempts for converting bounds.min:

The object is rotated 20 degrees in x and y and the non-rotated bounds.min is (1.0, 1.0, 1.0).

 Collider collider = transform.GetComponent<Collider>();

 Vector3 center = collider.bounds.center;
 Vector3 min = collider.bounds.min;

 Quaternion rotation;
 Vector3 unrotatedMin;

 // Using Quaternion.Inverse()
 rotation = Quaternion.Inverse(transform.rotation);
 unrotatedMin = rotation * (min - center) + center;
 Debug.Log(unrotatedMin); // (1.2, 0.1, 0.6) != (1.0, 1.0, 1.0)
 Debug.Log(rotation); // (-0.2, -0.2, 0.0, 1.0)
 Debug.Log(rotation.eulerAngles); // (341.3, 338.8, 7.1)

 // By hand
 rotation = new Quaternion();
 rotation.eulerAngles = new Vector3(-20.0f, -20.0f, 0.0f);
 unrotatedMin = rotation * (min - center) + center;
 Debug.Log(unrotatedMin); // (1.1, 0.3, 0.6) != (1.0, 1.0, 1.0)
 Debug.Log(rotation); // (-0.2, -0.2, 0.0, 1.0)
 Debug.Log(rotation.eulerAngles); // (340.0, 340.0, 0.0)

The same method for rotating vertices works later on in the code, when I rotate from zero rotation back to 20 degrees rotation. I've been trying all day, but I just can't figure it out.

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
1
Best Answer

Answer by Oribow · May 22, 2016 at 12:45 AM

Use transform.localToWorldMatrix. To ignore the scale part of the matrix, use Matrix4x4.MultiplyVector. Bounds are really just that bounds. A cube that contains every vertex of the object. Bounds are never rotated (view left side of picture). You want calc the collider verts. I did something similiar for 2D, but you can easily convert it to 3D. Just assume a box, not rotated and with a scale of 1. Then rotate, scale, translate every vert of that box to your target box local space. (Dont forget to include the collider offset and scale)

   private void LoadColliderVerts(Collider2D collider, List<Vector2> verts)
         {
             verts.Clear();
             Type cTyp = collider.GetType();
 
             if (cTyp == typeof(BoxCollider2D))
                 GetBoxColliderVerts((BoxCollider2D)collider, verts);
             else if (cTyp == typeof(CircleCollider2D))
                 GetCircleColliderVerts((CircleCollider2D)collider, verts, _circleVertCount);
             else if (cTyp == typeof(EdgeCollider2D))
                 verts.AddRange(((EdgeCollider2D)collider).points);
             else if (cTyp == typeof(PolygonCollider2D))
                 verts.AddRange(((PolygonCollider2D)collider).points);
         }
 
         private static void GetBoxColliderVerts(BoxCollider2D collider, List<Vector2> verts)
         {
             Vector2 halfSize = collider.size / 2;
             verts.Add(collider.transform.TransformPoint(halfSize + collider.offset));
             verts.Add(collider.transform.TransformPoint(new Vector2(halfSize.x, -halfSize.y) + collider.offset));
             verts.Add(collider.transform.TransformPoint(-halfSize + collider.offset));
             verts.Add(collider.transform.TransformPoint(new Vector2(-halfSize.x, halfSize.y) + collider.offset));
         }
 
         private static void GetCircleColliderVerts(CircleCollider2D collider, List<Vector2> verts, int circleVertCount)
         {
             float anglePerCircleVert = (Mathf.PI * 2) / circleVertCount;
             for (int i = 0; i < circleVertCount; i++)
             {
                 verts.Add(collider.transform.TransformPoint(new Vector2(collider.radius * Mathf.Sin(anglePerCircleVert * i), collider.radius * Mathf.Sin(anglePerCircleVert * i))));
             }
         }



Comment
Add comment · Show 4 · 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 se · May 22, 2016 at 12:16 PM 0
Share

@Oribow: Do you mean unrotated$$anonymous$$in = transform.localToWorld$$anonymous$$atrix.$$anonymous$$ultiplyVector($$anonymous$$);? That gives me (0.8, 0.5, 0.4) ins$$anonymous$$d of (1.0, 1.0, 1.0). I tried various other combinations and matrices, but nothing works. I'm not sure why I need local to world space transformation since I only have one object and bounds.$$anonymous$$ and bounds.center seem to always be absolute world space coordinates, with or without parent. I'm pretty much just guessing at this point, so I would really appreciate any further help, thanks.

avatar image Oribow · May 22, 2016 at 01:27 PM 0
Share

Updated the answer.

avatar image Oribow · May 22, 2016 at 01:31 PM 1
Share

Oh, I found my 3D script for boxColliders. Here you go:

  BoxCollider bc = (BoxCollider)mf;
                         foreach (Vector3 v in generic_cube_mesh.vertices)
                         {
                             Vector3 tmpv = v;
                             tmpv.x *= bc.size.x;
                             tmpv.y *= bc.size.y;
                             tmpv.z *= bc.size.z;
                             tmpv += bc.center;
                             verticiesList.Add(bc.transform.TransformPoint(tmpv));
                         }


Generic cube mesh holds the verts of a not rotated, scale = 1 cube. Just so I didnt have to type all 8 verts per hand.

avatar image se Oribow · May 22, 2016 at 04:45 PM 0
Share

Yes! Thanks so much, the code works and the picture made me realize my mistake to begin with.

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

Rotate a sphere by its vertices... 1 Answer

Rotation problem 1 Answer

Rotate Rigidbody to always face a force 0 Answers

Problem with normal snapping and rotations. 1 Answer

Relative rotation while moving on parent. 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