简体   繁体   中英

C++ Quaternion rotation not working as intended

I'm trying to rotate a model around one of the axis with quaternions for example Y. I'm using this algorithm, pretty straightforward axis = (0,1,0) angle+= 0.01f (radiants)

quat.w = cos(angle);
quat.x = axis.x* sin(angle / 2.0);
quat.y = axis.y* sin(angle / 2.0);
quat.z = axis.z* sin(angle / 2.0);

It works for probably the first 60 degree (edit: it's probably 90 to be honest) than the model just behave strangely and stretch at its centre. I'm using GLUT and c++

this is the method I use to convert it again into a matrix and than using it in the Model View Perspective matrix. I did some debugging and the maths seems alright, I probably missing something obvious ( new to quaternions)

void Quaternion::QuatToMatrix(Quaternion& a, GLfloat* matrix)

// First row
matrix[0] = 1.0f - 2.0f * (a.y * a.y + a.z * a.z);
matrix[1] = 2.0f * (a.x * a.y - a.w * a.z);
matrix[2] = 2.0f * (a.x * a.z + a.w * a.y);
matrix[3] = 0.0f;
// Second row
matrix[4] = 2.0f * (a.x * a.y + a.w * a.z);
matrix[5] = 1.0f - 2.0f * (a.x * a.x + a.z * a.z);
matrix[6] = 2.0f * (a.y * a.z - a.w * a.x);
matrix[7] = 0.0f;

// Third row
matrix[8] = 2.0f * (a.x * a.z - a.w * a.y);
matrix[9] = 2.0f * (a.y * a.z + a.w * a.x);
matrix[10] = 1.0f - 2.0f * (a.x * a.x + a.y * a.y);
matrix[11] = 0.0f;

// Fourth row
matrix[12] = 0;
matrix[13] = 0;
matrix[14] = 0;
matrix[15] = 1.0f;

Hope you can help me, sorry if it is a stupid question thanks in advance.

四元数的实部也应使用半角:

quat.w = cos(angle / 2.0);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM