Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (alibaba), 721 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19059 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
calculate angles from rotation matrix #405139
07/24/12 13:12
07/24/12 13:12
Joined: Jul 2011
Posts: 69
P
pjotr987 Offline OP
Junior Member
pjotr987  Offline OP
Junior Member
P

Joined: Jul 2011
Posts: 69
i have the following problem.

i have a 3x3 rotation matrix and want to have angles instead.

is it possible to calculate the angle changes around x, y and z achsis from a rotation matrix, that has following format:

[xx,xy,xz
yx,yy,yz
zx,zy,zz]


or is it possible to rotate bones with this rotation matrix somehow, then i dont need the angle calculation.

it would be a great help if someone can give me an idea how to procced.

thanks in advance

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
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

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:

Code:
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: HeelX] #405144
07/24/12 15:38
07/24/12 15:38
Joined: Jul 2011
Posts: 69
P
pjotr987 Offline OP
Junior Member
pjotr987  Offline OP
Junior Member
P

Joined: Jul 2011
Posts: 69
thanks for the fast reply,
i plan to rotate bones of a model and have this 3x3 rotation matrixes as source for the 3D rotations. now i have to move the bones according to the data in the matrix but have no idea how to do it.

i still have dificulties to understand ur answer. can u give me an working example code, for the following scenarios:

Matrix:

xx xz xy 0
zx zy zz 0
yx yy yz 0
0 0 0 1

is it DirectX-style?

0 0 1 0
1 0 0 0
0 1 0 0
0 0 0 1

1) now how to use the ang_for_matrix function to get the angles?

2) is it also possible to use the matrix above directly for rotation of my models bones?

thanks in advance

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
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Originally Posted By: pjotr987
is it DirectX-style?


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.

Code:
// 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.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1