1 registered members (alibaba),
721
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: calculate angles from rotation matrix
[Re: pjotr987]
#405141
07/24/12 14:51
07/24/12 14:51
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
Are you transforming that in a shader? If yes you don't need to transform it to an angle representation:
float3x3 myMatrix = ...;
float3x3 myBoneMatrix = ...;
float3x3 rotMatrix = mul(myBoneMatrix, myMatrix); // bone matrix rotated by myMatrix
float3 pos = ...;
float3 transformedPos = mul(pos, rotMatrix);
Otherwise, there are two functions ang_to_matrix and ang_for_matrix which bring an Euler angle to a 4x4 rotation matrix and back. You will have to expand your 3x3 matrix then by adding a zeros, but the lower right fields must be 1. Important: the functions work with DirectX-style matrices, therefore the Z/Y components are switched. With mat_multiply you can multiply two matrices, with the code given above, you can easily rotate your bone matrix then, if you like. If you are interested into the insights, well... to decompose a 3x3 rotation matrix into Euler angles by yourself, you will have a hard time. That is because there is no unique solution to that; regarding Gimbal Lock. A good explanation is found here: Computing Euler angles from a rotation matrix (Gregory G. Slabaugh). It describes how rotation matrices are built from an euler angle representation and how you can resolve it back. He also gives pseudo code and an example.
|
|
|
Re: calculate angles from rotation matrix
[Re: pjotr987]
#405145
07/24/12 16:00
07/24/12 16:00
|
Joined: Jul 2001
Posts: 6,904
HeelX
Senior Expert
|
Senior Expert
Joined: Jul 2001
Posts: 6,904
|
No, it's neither lefthanded nor righthanded. See: the x-axis projects on the y-axis, the y-axis to the z-axis and the z-axis to the x-axis. I don't know what you are doing there... from where do you get the matrix? This is untested code, but it basically should be your solution - it expands a 3x3 float matrix to a corresponding affine 4x4 matrix, translates it to an euler angle and rotates a bone by that.
// alread filled
float m3x3[3][3];
// expanded 3x3 matrix
float m4x4[4][4];
// expand
memset(m4x4, 0, sizeof(float) * 16);
m4x4[3][3] = 1;
// copy 3x3
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m4x4[i][j] = m3x3[i][j];
// get euler angle
ANGLE angle;
ang_for_matrix(&angle, m4x4);
// rotate bone by euler angle
ent_bonerotate(my, "arm", &angle);
Maybe this can be done slicker, dunno. [EDIT] Your matrix is a transformation, resulting from a +90° degree rotation around the (Gamestudio) z-axis followed by a -90° rotation arround the (new) y-axis. Maybe you need to do the inverse rotation (you can compute the corresponding rotation matrix beforehand) on your input data before you do things to your bones in Gamestudio's coordinate system. IMHO.
Last edited by HeelX; 07/24/12 16:04.
|
|
|
|