Hi all;
Please, how to convert a given 3x3 rotation matrix to an euler angles with c# ?
thank’s.
Hi all;
Please, how to convert a given 3x3 rotation matrix to an euler angles with c# ?
thank’s.
You can produce any arbitrary rotation with the Quaternion.Euler()
factory method.
In what format are you receiving this 3x3 rotation matrix?
Unity has a 4x4 Transform Matrix called Matrix4x4:
And it has a ‘rotation’ property to get the quat off of it which can be turned into euler angle easily:
var e = m.rotation.eulerAngles;
But you’re talking about a 3x3 rotation matrix. I am unaware of a built in 3x3 Matrix type.
So for starters we need to determine is this 3x3 matrix you’re getting I assume from externally (the web, a plugin, whatever).
Is it a 3d rotation matrix, where each row or column represents the up/right/forw vector? If so, you’ll need to know the format (rows/cols/directions/handedness), and just copy it into a Matrix4x4 leaving the translation portion 0 and pull the rotation quat. (You can check the docs for needed info about format… if you need help due to lack of specificity in the documentation just come back and ask. Just keep in mind if this is coming from an external source the documentation about that is also important to get row/cols/directions/handedness).
If this is a 2D transform matrix (where it contains the 2 up/right vectors, and the translation vector). Then you use Mathf.Atan2 to find the angle of right vector off of +x axis. Noting you have to again keep in mind the the rows/cols/directions/handedness.
TLDR;
Where are you getting this matrix and what is the definition of its format?
As lordofduct said, the easiest way to convert a 3x3 matrix into a Quaternion is to use Unity’s Matrix4x4 as it has this conversion already built in. Just set the upper left part of the 4x4 matrix to your 3x3 matrix and keep the remaining values to 0 except for the 4th element on the main diagonal.
If you are really interested in manually converting a rotation matrix into a quaternion, have a look over here. Though be careful that there’s a difference between a pure rotation matrix and a combined rotation and scale matrix. If you don’t understand that difference, you may want to have a look at my matrix crash course.
Also keep in mind that Unity uses a left handed coordinate system. So if your “matrix” represents a right handed system you may have some orientation issues.
I have converted well using the matrix4X4 from unity. Problem solved.
Your answers were relevant I thank you all.