要做你想在你的评论中做的事情,你还需要知道你的玩家以前的方向。实际上,最好的办法是将所有关于玩家的位置和方向(以及游戏中的其他任何东西)的数据存储为4x4矩阵。这是通过将第四列和第四行“添加”到3x3旋转矩阵来完成的,并使用额外的列来存储关于玩家位置的信息。背后的数学(齐次坐标)非常简单,在OpenGL和DirectX中都非常重要。我建议你这个伟大的教程http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ 现在,您的播放器向你的敌人旋转,使用GLM,你可以这样做:
1)在您的播放器和敌对阶级,宣告矩阵和三维矢量位置
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)旋转朝着玩家的敌人,做
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])
}
与
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
对于旋转矩阵,您需要一个方向矢量(例如轴)**和**一个角度。给出一个方向你在说什么轮换? – 6502
我想这样做: Vec3 dirToEnemy =(Enemy.Position - Player.Position).normalize(); Player.Matrix.makeRotationDir(dir); Player.Attack(); – UfnCod3r
@ 6502:不,对于旋转矩阵,你也可以使用三个向量(x,y,z) – SigTerm