Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 旋转矩阵的方向向量_C++_C_Opengl_Matrix - Fatal编程技术网

C++ 旋转矩阵的方向向量

C++ 旋转矩阵的方向向量,c++,c,opengl,matrix,C++,C,Opengl,Matrix,如何从方向(单位向量)创建旋转矩阵 我的矩阵是3x3,主列,右手 我知道“column1”是对的,“column2”是向上的,“column3”是向前的 但我不能这么做 //3x3, Right Hand struct Mat3x3 { Vec3 column1; Vec3 column2; Vec3 column3; void makeRotationDir(const Vec3& direction) { //:((

如何从方向(单位向量)创建旋转矩阵

我的矩阵是3x3,主列,右手

我知道“column1”是对的,“column2”是向上的,“column3”是向前的

但我不能这么做

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

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

要在评论中做你想做的事情,你还需要知道你的玩家之前的定位。 实际上,最好的办法是将所有关于玩家位置和方向的数据(以及游戏中几乎所有其他内容)存储到4x4矩阵中。这是通过向3x3旋转矩阵“添加”第四列和第四行来完成的,并使用额外的列来存储有关球员位置的信息。这个(齐次坐标)背后的数学非常简单,在OpenGL和DirectX中都非常重要。我向你推荐这个很棒的教程 现在,要使用GLM将玩家转向敌人,您可以执行以下操作:

1) 在你的玩家和敌人职业中,为这个位置声明一个矩阵和3d向量

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 
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) 要将敌人转向玩家,请执行以下操作

enemy.matrix =  glm::LookAt(
enemy.position, // position of the player
player.position,   // position of the enemy
vec3(0.0f,1.0f,0.0f) );        // the up direction 
如果要将所有内容存储在矩阵中,请不要将位置声明为变量,而应声明为函数

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


对于旋转矩阵,需要方向向量(例如轴)和角度。给定一个方向,你在说什么旋转?我想这样做:Vec3 dirToEnemy=(敌方.Position-Player.Position).normalize();Player.Matrix.makeRotationDir(dir);Player.Attack()@6502:不,对于旋转矩阵,你也可以使用三个向量(x,y,z)@SigTerm:你当然可以自由定义一个函数,只要你愿意(包括宠物狗的名字),它就会返回一个旋转:这不是非法的,你也不会进监狱。但即使从维度的角度来看,三个
x^2+y^2+z^2=1的实数也是不够的(围绕原点的旋转比单位球体上的点多)。@6502:3个向量,其中每个向量有3个分量,并定义全局坐标系中局部坐标系的轴(x,y,z),形成3x3旋转矩阵。也可以使用euler角度进行旋转。或3分。或者四元数。所以,如果方向对应于向上,则不应用旋转?
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;
    }
}