要做你想在你的評論中做的事情,你還需要知道你的玩家以前的方向。實際上,最好的辦法是將所有關於玩家的位置和方向(以及遊戲中的其他任何東西)的數據存儲爲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