16

クォータニオンで回転させたモデルがあります。回転を設定することしかできず、何も足したり引いたりすることはできません。軸の値を取得し、それに角度(おそらく度またはラジアン?)を追加してから、変更されたクォータニオンを再度追加する必要があります。

これどうやってするの?(各軸で答えてください)。

4

2 に答える 2

38

2つのクォータニオンを乗算して、2つの回転の結果である3番目のクォータニオンを生成できます。クォータニオン乗算は可換ではないことに注意してください。つまり、順序が重要です(これを頭の中で数回行うと、理由がわかります)。

次のようなものを使用して、特定の軸の周りの特定の角度による回転を表すクォータニオンを生成できます(Javaではなくc ++であるという事実を失礼します)。

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)、モデルの現在の回転クォータニオンで乗算します。

于 2010-12-14T07:42:55.527 に答える
5

後で他の詳細をテストするためにsje397の投稿から実行可能なコードを作成しました。最終的にCを使用すると、Quaternionクラスがない理由になります。

#include <iostream>
#include <math.h>
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 (((a.x * a.x) + (a.y * a.y)) + (a.z * a.z)) + (a.w * a.w);
}
float4 normalize(float4 q)
{
   float num = dot(q);
   float inv = 1.0f / (sqrtf(num));
   return make_float4(q.x * inv, q.y * inv, q.z * inv, q.w * 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 << ", " <<quat.y << ", " <<quat.z << ", " <<quat.w << ")" << endl; 
   return 0;
}

出力

(0.0871557、0、0、0.996195)

于 2016-01-15T05:43:05.513 に答える