Rotating a 4D matrix about the Y axis with an X-Z vector

I should mention that I'm pretty new to Matrices and will probably need a layman's answer to my question.

I have a 4D matrix which represents the rotation of an object in 3D space. I have a normalized 2D vector which represents the direction i want the object to be facing by rotating the Y axis.

I need to be able to change the Y axis rotation of the object only, by applying some sort of calculation to the XZ vector i have. What do i need to do to make this work? And if possible, can any answers come with an explanation of the process so that i can do it again on my own in the future!

• So would you simply want to create a new matrix with the new facing direction? Or do you specifically want to rotate the matrix for other reasons? – RobM Sep 22 '15 at 12:03
• Creating a new matrix would work fine – Lex Webb Sep 22 '15 at 12:33

I depends on the format of your 4d matrix. I'm guessing the orientation is in there somewhere. This is a way to recreate a matrix given a z & y

pseudocode...

Vector2d my2d; // assuming this is your 2d vector with x,z components.

Vector3 z = new Vector3( my2d.x, 0, my2d.z);   // looking dir
Vector3 y = new Vector3( 0,1,0);               // we'll make an upward y dir
Vector3 x = y.cross(z);                        // y cross z gives us perpendicular to y&z

You then need to insert the xyz vectors into your matrix. I'm not sure of your matrix format but i guess they will be described as rows or columns depending if it is left/right handed coords.

The order of the cross product depends on left or right handed also. If you find that its not working try

x = z.cross(y);

• Did it work for you? – Školstvo Dec 21 '15 at 23:33