2013-09-01 24 views
6

方向ベクトルの方向(単位ベクトル)からの回転行列を作成する方法を

私のマトリックスは、「カラム1」は右である、「COLUMN2」私は知っている3×3、列メジャー、右手

はあります「列3」は前進

しかし、私はこれをやりません。

//3x3, Right Hand 
struct Mat3x3 
{ 
    Vec3 column1; 
    Vec3 column2; 
    Vec3 column3; 

    void makeRotationDir(const Vec3& direction) 
    { 
     //:((
    } 
} 
+1

回転行列の場合、方向ベクトル(例:軸)**と角度が必要です。あなたは何回転しているのですか? – 6502

+0

私はこれをやりたい: Vec3 dirToEnemy =(Enemy.Position - Player.Position).normalize(); Player.Matrix.makeRotationDir(dir); Player.Attack(); – UfnCod3r

+0

@ 6502:いいえ、回転行列の場合は、3つのベクトル(x、y、z)を使用することもできます – SigTerm

答えて

5

おかげで回転しません。 解決済み。

struct Mat3x3 
{ 
    Vec3 column1; 
    Vec3 column2; 
    Vec3 column3; 

    void makeRotationDir(const Vec3& direction, const Vec3& up = Vec3(0,1,0)) 
    { 
     Vec3 xaxis = Vec3::Cross(up, direction); 
     xaxis.normalizeFast(); 

     Vec3 yaxis = Vec3::Cross(direction, xaxis); 
     yaxis.normalizeFast(); 

     column1.x = xaxis.x; 
     column1.y = yaxis.x; 
     column1.z = direction.x; 

     column2.x = xaxis.y; 
     column2.y = yaxis.y; 
     column2.z = direction.y; 

     column3.x = xaxis.z; 
     column3.y = yaxis.z; 
     column3.z = direction.z; 
    } 
} 
+0

方向が上に対応する場合、回転は適用されませんか? –

2

あなたのコメントにしたいことをするには、以前のyoueプレーヤーの向きも知る必要があります。 実際には、プレイヤーの位置と向きに関するすべてのデータ(およびゲーム内のほとんどすべてのデータ)を4x4マトリックスに保存することをお勧めします。これは、3行3列の回転行列に4列目と4行目を「追加」し、余分な列を使用してプレーヤーの位置に関する情報を格納することによって行われます。これの背後にある数学(同次座標)は、OpenGLとDirectXの両方で非常に単純で非常に重要です。位置

のために行列と3Dベクトルを宣言し、お使いのプレーヤーと敵のクラスでは)

1:私はあなたがこれを行うことができ、GLMを使用して、あなたの敵に向けてプレーヤーを回転させるために、今、あなたにこの偉大なチュートリアルhttp://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ を提案します

glm::mat4 matrix; 
glm::vec3 position; 

2)

を行い、プレイヤーの方に敵を回転させるように

player.matrix = glm::LookAt(
player.position, // position of the player 
enemy.position, // position of the enemy 
vec3(0.0f,1.0f,0.0f));  // the up direction 

3)で敵に向けて回転させますあなたは行列のすべてを保存したい場合は

は、

vec3 position(){ 
    return vec3(matrix[3][0],matrix[3][1],matrix[3][2]) 
} 

変数としてではなく関数としての位置を宣言し、すべてに

player.matrix = glm::LookAt(
player.position(), // position of the player 
enemy.position(), // position of the enemy 
vec3(0.0f,1.0f,0.0f));  // the up direction 
関連する問題