Can i get rotation(yaw, pitch,roll) from this matrix algorithm? thanks
matrix[0][0] = cos(pitch)*cos(yaw);
matrix[0][2] = cos(pitch)*sin(yaw);
matrix[0][1] = sin(pitch);
matrix[0][3] = 0;
matrix[1][0] = -cos(roll)*sin(pitch)*cos(yaw)+sin(roll)*sin(yaw);
matrix[1][1] = cos(roll)*cos(pitch);
matrix[1][2] = -cos(roll)*sin(pitch)*sin(yaw)-sin(roll)*cos(yaw);
matrix[1][3] = 0;
matrix[2][0] = -sin(roll)*sin(pitch)*cos(yaw)-cos(roll)*sin(yaw);
matrix[2][1] = sin(roll)*cos(pitch);
matrix[2][2] = cos(roll)*cos(yaw)-sin(roll)*sin(pitch)*sin(yaw);
matrix[2][3] = 0;
matrix[3][0] = 0;
matrix[3][1] = 0;
matrix[3][2] = 0;
matrix[3][3] = 1;
edit: i used this code but it not working:
D3DXMATRIX matrix2 = D3DXMATRIX(matrix);
yaw = atan2(matrix2._13, matrix2._11);
pitch = asin(matrix2._12);
roll = atan2(matrix2._32, matrix2._22);
Edit 3: this is my old function, it works but not 100%
VECTOR getAnglefromMatrix(float* m)
{
float pitch, yaw, roll;
D3DXMATRIX matrix = D3DXMATRIX(m);
if (matrix._11 == 1.0f)
{
yaw = todegree(atan2f(matrix._13, matrix._34));
pitch = 0;
roll = 0;
}
else if (matrix._11 == -1.0f)
{
yaw = todegree(atan2f(matrix._13, matrix._34));
pitch = 0;
roll = 0;
}
else
{
yaw = todegree(atan2(-matrix._31,matrix._11));
pitch = todegree(asin(matrix._21));
roll = todegree(atan2(-matrix._23,matrix._22));
}
return vector3d(yaw,pitch,roll);
}
i did something wrong?
We can go through the entries and find some that are nice for back-calculations:
matrix[0][0] = cos(pitch)*cos(yaw);
matrix[0][2] = cos(pitch)*sin(yaw);
This gives us:
yaw = atan2(matrix[0][2], matrix[0][0])
For the pitch:
matrix[0][1] = sin(pitch);
This gives us:
pitch = asin(matrix[0][1]) //this assumes pitch is between -pi/2 and +pi/2
Then
matrix[1][1] = cos(roll)*cos(pitch);
matrix[2][1] = sin(roll)*cos(pitch);
This gives us:
roll = atan2(matrix[2][1], matrix[1][1])
I omitted the cases where the arguments to atan2
become zero. I am confident you can figure them out. In these cases, the Euler angles are not uniquely defined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.