I have been helping in the free project Goxel. And I am trying to set the camera with the ui widgets.
Well, I have chatted with the main author, and he said me that the input boxes (for rotation) must be in euler angles instead of quaternions, and yes this is good advise.
But I can't convert correctly quaternion -> euler and euler -> quaternion.
I have opened a question in stackoverflow Quaternion to euler angles and euler angles to quaternion in C, but at the moment nobody helps me.
I have looked for about this, and I saw this question in stackoverflow http://stackoverflow.com/questions/11103683/euler-angle-to-quaternion-then-quaternion-to-euler-angle. And in this question the developers used the library glm. I have been studying the code of this library and I trying copy its code, this is my last code (with the similar "gears" of glm code):
- {l Code}: {l Select All Code}
#include <stdio.h>
#include <math.h>
#define DR2D (180 / M_PI)
#define DD2R (M_PI / 180)
int main(int argc, char** argv)
{
float x, y, z;
float qx, qy, qz, qw;
x = 0 * DD2R;
y = 90 * DD2R;
z = 90 * DD2R;
printf("initial x=%f y=%f z=%f\n", x * DR2D, y * DR2D, z * DR2D);
float sx = sin(x / 2);
float sy = sin(y / 2);
float sz = sin(z / 2);
float cx = cos(x / 2);
float cy = cos(y / 2);
float cz = cos(z / 2);
qx = sx*cy*cz - cx*sy*sz;
qy = cx*sy*cz + sx*cy*sz;
qz = cx*cy*sz - sx*sy*cz;
qw = cx*cy*cz + sx*sy*sz;
printf("Quaternion -> (%f, %f, %f, %f)\n", qx, qy , qz , qw);
x = atan2(2 * (qy*qz + qw*qx), qw*qw - qx*qx - qy*qy + qz*qz);
float temp = -2*(qx*qz - qw*qy);
if (temp < -1)
temp = -1;
else if (temp > 1)
temp = 1;
y = asin(temp);
z = atan2(2 * (qx*qy + qw*qz), qw*qw + qx*qx - qy*qy - qz*qz);
printf("recover euler x=%.2f y=%.2f z=%.2f\n",
x * DR2D, y * DR2D, z * DR2D);
return 0;
}
But it still fail.
- {l Code}: {l Select All Code}
md@speccy-laptop ~/proyectos/goxel/test $ rm a.out; g++ test.c; ./a.out
initial x=0.000000 y=90.000003 z=90.000003
Quaternion -> (-0.500000, 0.500000, 0.500000, 0.500000)
recover euler x=0.00 y=89.97 z=0.00
I feel bad, because I don't understand completely the quaternions and I can't make a good code. Well, I know that there is a 3D rotations equivalets.
Regards.