在1轴上旋转四元数?

我有一个由四元数旋转的模型。 我只能设置旋转,我不能添加或减去任何东西。 我需要获取轴的值,然后为它添加一个角度(可能是度数或弧度?),然后再重新添加修改后的四元数。

我怎样才能做到这一点? (在每个轴上回答)。

您可以将两个四元数相乘以生成第三个四元数,这是两个旋转的结果。 请注意,四元数乘法不是可交换的,这意味着顺序很重要(如果你在头脑中做了几次,你就会明白为什么)。

您可以使用类似的东西生成一个四元数,该四元数表示围绕特定轴的给定角度的旋转(原谅它是c ++,而不是java):

Quaternion Quaternion::create_from_axis_angle(const double &xx, const double &yy, const double &zz, const double &a) { // Here we calculate the sin( theta / 2) once for optimization double factor = sin( a / 2.0 ); // Calculate the x, y and z of the quaternion double x = xx * factor; double y = yy * factor; double z = zz * factor; // Calcualte the w value by cos( theta / 2 ) double w = cos( a / 2.0 ); return Quaternion(x, y, z, w).normalize(); } 

因此,例如,要围绕x轴旋转,可以使用createFromAxisAngle(1, 0, 0, M_PI/2)创建四元数,并将其乘以模型的当前旋转四元数。

从sje397的post做了一个可运行的代码,用于稍后测试其他细节,我想我会分享。 最终使用C没有Quaternion类的原因。

 #include  #include  using namespace std; struct float4{ float x; float y; float z; float w; }; float4 make_float4(float x, float y, float z, float w){ float4 quat = {x,y,z,w}; return quat; } float dot(float4 a) { return (((ax * ax) + (ay * ay)) + (az * az)) + (aw * aw); } float4 normalize(float4 q) { float num = dot(q); float inv = 1.0f / (sqrtf(num)); return make_float4(qx * inv, qy * inv, qz * inv, qw * inv); } float4 create_from_axis_angle(const float &xx, const float &yy, const float &zz, const float &a) { // Here we calculate the sin( theta / 2) once for optimization float factor = sinf( a / 2.0f ); float4 quat; // Calculate the x, y and z of the quaternion quat.x = xx * factor; quat.y = yy * factor; quat.z = zz * factor; // Calcualte the w value by cos( theta / 2 ) quat.w = cosf( a / 2.0f ); return normalize(quat); } int main() { float degrees = 10.0f; float4 quat = create_from_axis_angle(1, 0, 0, degrees*(3.14159f/180.0f)); cout << "> (" << quat.x << ", " < 

产量

(0.0871557,0,0,0.996195)