I’d like to do a statistic for my application in which the user can move and rotate an object.
In the end I would like to display how far the object has been moved overall and how much the user has rotated the objects.
For the change in position I have the straight forward approach of always adding the change in the objects position to one Vector3 holding the distance in the 3 axis overall. I do this in the Update() method:
overallMovedDist += vector3.Distance(oldPosition, transform.position);
old.position = transform.position;
transform.hasChanged = false;
Now for the rotation I wonder, how one would best approach such a statistic?
For users it would probably be best to display the values in Eulerangles, I suppose. So after doing two full rotations around for instance the x-axis one would get something like (4pi, 0, 0) or (2360, 0,0) as a result:
With the same approach as for the position I first tried to calculate this by always directly comparing the change in eulerangles that I got from the quaternions. However if an object is rotated around one axis too far, you get a jump in the values for the other axis as well as some point. If I am correct, this comes from the fact that Unity uses Quaternions internally and the conversion.
I tried some stuff with Quaternions already which you can see beneath.
Quaternion relativeChange = Quaternion.Inverse(oldRotation) * transform.rotation; // get rotation between old and new transform.rotation
overallRotation = overallRotation * relativeChange; //add the two Quaternions
oldRotation = transform.rotation;
However when I test this in my code, slowly rotate the object around an axis and watch the Log I always get the same thing printed, which is (0.0, 0.0, 0.0). But obviously this is not correct, as I have rotated the object.
Can anybody assist me with how to approach this correctly? Thank you in advance!