Direction vector to rotation matrix

How to create a rotation matrix From direction (unit vector)

My 3x3 matrix, large column and right hand

I know "column1" is correct, "column2" is inserted, and "column3" is forward

But I can’t do it.

//3x3, Right Hand struct Mat3x3 { Vec3 column1; Vec3 column2; Vec3 column3; void makeRotationDir(const Vec3& direction) { //:(( } } 
+6
source share
2 answers

Thanks to everyone. Solvable.

 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; } } 
+5
source

To do what you want to do in your comment, you also need to know your player’s previous orientation. Actually, it is best to store all the data about the position and orientation of your player (and almost everything else in the game) in a 4x4 matrix. This is done by adding a fourth column and a fourth row to the 3x3 rotation matrix and using an additional column to store player position information. The math behind this (uniform coordinates) is quite simple and very important in both OpenGL and DirectX. I offer you this great tutorial http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ Now, to turn a player to an enemy using GLM, you can do this:

1) In the game and enemy classes, declare a matrix and a 3d vector for the position

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

2) rotate against the enemy 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 

3) to turn the enemy towards the player, do

 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 save everything in a matrix, do not 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 
+2
source

All Articles