I have a WPF project which contains a 3D model that I'm manipulating using the 3Dtools trackball and a mouse event overlay.
As part of this project I want to display the rotation of the X, Y, and Z axes as the object is rotated.
I have not been able to find a simple way of obtaining these values so I'm trying to obtain the rotation matrix from the trackball and calculate the values from that. To do this I am currently using the following code in the MouseEventOverlay_MouseMove event:
Transform3DGroup tgroup = (Transform3DGroup)trackball.Transform;
ScaleTransform3D scaleTransform = (ScaleTransform3D)tgroup.Children;
RotateTransform3D rotateTransform = (RotateTransform3D)tgroup.Children;
Matrix3D m = rotateTransform.Value;
double x, y, z = 0;
double sign = -1;
double y1 = -Math.Asin(m.M31);
if (Math.Cos(y1) > 0)
sign = 1;
double x1 = Math.Atan2(m.M32 / sign, m.M33 / sign);
double z1 = Math.Atan2(m.M21 / sign, m.M11 / sign);
x = x1;
y = y1;
z = z1;
x = x * (180.0 / Math.PI);
y = y * (180.0 / Math.PI);
z = z * (180.0 / Math.PI);
The values are to be shown in the project ranging from -180 to +180 with 0 being no rotation.
As far as I can tell the code for calculating the values from the matrix is correct, yet the values show for the Y and Z seem wrong; as I rotate the object the values go up to about +/- 70 degrees then start to move back towards 0 again. When I rotate just
the X axis it seems to work fine, but sometimes when I rotate the Y or Z as I get to around 70 the X axis will suddenly jump up to about 180 even though there has been no change in the X axis of the model on screen.
If anyone can provide some help obtaining the correct X, Y, Z rotation values from the trackball I would appreciate it greatly.