C++ 旋转矩阵的方向向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18558910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Direction Vector To Rotation Matrix
提问by UfnCod3r
How To Create Rotation matrix From Direction (unit vector)
如何从方向创建旋转矩阵(单位向量)
My Matrix Is 3x3, Column Major, And Right Hand
我的矩阵是 3x3,列主要和右手
I Know 'column1' is right, 'column2' is up and 'column3' is forward
我知道“column1”是对的,“column2”向上,“column3”向前
But I Can`t Do This.
但我不能这样做。
//3x3, Right Hand
struct Mat3x3
{
Vec3 column1;
Vec3 column2;
Vec3 column3;
void makeRotationDir(const Vec3& direction)
{
//:((
}
}
回答by UfnCod3r
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;
}
}
回答by darius
To do what you want to do in your comment, you also need to know the previous orientation of youe Player. Actually, the best thing to do is to store all the data about position and orientation of your player (and almost anything else in a game) into a 4x4 matrix. This is done by "adding" a fourth column and and fourth row to the 3x3 rotation matrix, and use the extra column to store the information about the player position. The math behind this (homogeneous coordinates) is quite simple and very important in both OpenGL and DirectX. I suggest you this great tutorial http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/Now, to rotate your player towards your enemy, using GLM, you can do this:
要在评论中做您想做的事情,您还需要知道您之前播放器的方向。实际上,最好的做法是将所有关于玩家位置和方向的数据(以及游戏中几乎所有其他内容)存储到一个 4x4 矩阵中。这是通过向 3x3 旋转矩阵“添加”第四列和第四行来完成的,并使用额外的列来存储有关玩家位置的信息。这背后的数学(齐次坐标)在 OpenGL 和 DirectX 中非常简单且非常重要。我向你推荐这个很棒的教程http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/现在,使用 GLM 将你的玩家旋转到你的敌人,你可以这样做:
1) In your player and enemy classes, declare a matrix and 3d vector for the position
1) 在你的玩家和敌人类中,为位置声明一个矩阵和 3d 向量
glm::mat4 matrix;
glm::vec3 position;
2) rotate towards enemy with
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) to rotate the enemy towards the player, do
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
If you want to store everything in the matrix, don't declare the position as a variable but as a function
如果要将所有内容都存储在矩阵中,请不要将位置声明为变量,而是将其声明为函数
vec3 position(){
return vec3(matrix[3][0],matrix[3][1],matrix[3][2])
}
and rotate with
并旋转
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