Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 zerophase · Jan 13, 2015 at 08:04 AM · c#collisionmatrix

Generate a 3x3 rotation matrix from a Matrix4x4

I'm working through Real Time Collision Detection, and generating my own Collision system based off of Unity's Colliders. (Mostly to learn more about collision algorithms) I'm just having a hard time figuring out how to convert the built in Matrix4x4 to a 3x3. Right now I'm using the 4x4 Matrix to Update the the center and HalfWidths of my boxes.

This is what that code looks like (I know it needs some redesign for readability):

 public void UpdateAABB(AABB3D a, float[][] m, Vector3 t, ref AABB3D b)
 {
     Vector3 modify = b.Center;
     modify.x = t.x;
     modify.y = t.y;
     modify.z = t.z;
     b.HalfHeight = 0.0f;
     b.HalfWidth = 0.0f;
     b.HalfDepth = 0.0f;
     for (int i = 0; i < 3; i++)
     {
         for (int j = 0; j < 3; j++)
         {
             if (i == 0)
             {
                 if(j == 0)
                 {
                     modify.x += m[j][i] * a.Center.x;
                     b.HalfWidth += Math.Abs(m[j][i]) * a.HalfWidth;
                 }
                 else if (j == 1)
                 {
                     modify.x += m[j][i]*a.center.y;
                     b.HalfWidth += Math.Abs(m[j][i]) * a.HalfHeight;
                 }
                 else if (j == 2)
                 {
                     modify.x += m[j][i]*a.center.z;
                     b.HalfWidth += Math.Abs(m[j][i])*a.HalfDepth;
                 }
                         
             }
             else if (i == 1)
             {
                 if(j == 0)
                 {
                     modify.y += m[j][i] * a.Center.x;
                     b.HalfHeight += Math.Abs(m[j][i]) * a.HalfWidth;
                 }
                 else if(j == 1)
                 {
                     modify.y += m[j][i] * a.Center.y;
                     b.HalfHeight += Math.Abs(m[j][i]) * a.HalfHeight;
                 }
                 else if (j == 2)
                 {
                     modify.y += m[j][i]*a.Center.z;
                     b.HalfHeight += Math.Abs(m[j][i])*a.HalfDepth;
                 }    
             }
             else if (i == 2)
             {
                 if (j == 0)
                 {
                     modify.z += m[j][i]*a.Center.x;
                     b.HalfDepth += Math.Abs(m[j][i])*a.HalfWidth;
                 }
                 else if (j == 1)
                 {
                     modify.z += m[j][i]*a.Center.y;
                     b.HalfDepth += Math.Abs(m[j][i])*a.HalfHeight;
                 }
                 else if (j == 2)
                 {
                     modify.z += m[j][i]*a.Center.z;
                     b.HalfDepth += Math.Abs(m[j][i])*a.HalfDepth;
                 }
             }
         }
     }
 
     b.Center = modify;
 }

  

This is how it gets called:

 matrix4X4 = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
 
 for (int i = 0; i < 4; i++)
 {
     for (int j = 0; j < 4; j++)
     {
         rotation[i][j] = matrix4X4[i, j];
     }
 }
 
 boundingBox.UpdateAABB(rotationBox, rotation, transform.position, ref boundingBox);

It works fine for re-sizing boxes when I had the algorithm set up for 2D, but won't update the z axis correctly in 3D. I believe it comes from Unity's 4x4 Matrix. So, how could I go about building a 3x3 Matrix from either the Matrix4x4 class or a Quaternion?

Comment
Add comment · Show 1
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 Jeff-Kesselman · Jan 14, 2015 at 02:14 AM 0
Share

Any direct mapping is going to be dependent on unpublished and thus unstable assumptions about the internal layout of Unity's matrices.

I suggest you extract a quaternion from the 4x4 and then convert back to your 3x3 representation. That will at least be stable.

Its more work but if you intelligently cache then you shouldn't have to do it that often.

If you REALLY want to write unstable code, then you should be able to find out easily yourself how the matrix is organized. Put known values for the X, Y, Z rotations and then dump the matrix and see where they are.

2 Replies

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

Answer by zerophase · Jan 14, 2015 at 04:22 AM

In case anyone else needs a 3x3 Matrix here's how I did it:

 private static float[][] identityMatrix =
 {
     new [] {1.0f, 0.0f, 0.0f},
     new []{0.0f, 1.0f, 0.0f},
     new []{0.0f, 0.0f, 1.0f}
 };
 
 public static float[][] QuaternionTo3x3(this Quaternion value)
 {
     float[][] matrix3x3 =
     {
         new float[3],
         new float[3],
         new float[3],
     };
 
     float[][] symetricalMatrix =
     {
         new float[3] {(-(value.y * value.y) - (value.z * value.z)), value.x * value.y,                                       value.x * value.z},
         new float[3] {value.x * value.y, (-(value.x * value.x) - (value.z * value.z)), value.y * value.z},
         new float[3] {value.x * value.z, value.y * value.z, (-(value.x * value.x) - (value.y * value.y))} 
     };
 
     float[][] antiSymetricalMatrix =
     {
         new[] {0.0f, -value.z, value.y},
         new []{value.z, 0.0f, -value.x},
         new []{-value.y, value.x, 0.0f}
     };
 
     for (int i = 0; i < 3; i++)
     {
         for (int j = 0; j < 3; j++)
         {
             matrix3x3[i][j] = identityMatrix[i][j] +
                       (2.0f*symetricalMatrix[i][j]) + 
                       (2.0f*value.w * antiSymetricalMatrix[i][j]);
         }
     }
 
     return matrix3x3;
 }




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
avatar image
0

Answer by Bulvyf · Apr 03, 2016 at 10:11 PM

Recommend you investigate the use of Math.NET numerics libraries in (C# solutions). For me to get the numerics library to work with the Unity3D C3(v3) scripts, I had to

  1. download Math.NET Numerics via NUGET to a full .NET 4+ project (alternatively you could download from GITHUB at https://github.com/mathnet/mathnet-numerics ).

  2. Then move the MathNet.Numerics.dll to the Assets/Plugins directory

  3. Close and (re) open Unity, for Unity to load the dll as a reference within its older Visual Studio project version. You can delete or unload the the previously mentioned .NET 4.5 project, thereafter.

From there, it's possible to use their matrix and vector libraries (with so much more as well).

Cross reference on how to include a dll in Unity: http://answers.unity3d.com/questions/333829/including-a-dll-in-unity.html

Explanation for using a Math.NET Matrix: http://numerics.mathdotnet.com/Matrix.html

Some links on using Matrices with their Linear Regression examples.

  • http://numerics.mathdotnet.com/Regression.html

  • http://christoph.ruegg.name/blog/linear-regression-mathnet-numerics.html

  • http://www.imagingshop.com/linear-and-nonlinear-least-squares-with-math-net/

I hope that's of use?

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

27 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

I am not able to call a function from OntriggerEnter event :( 2 Answers

Text never displays count variable 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