Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D - Rotation Matrix from direction vector (Forward, Up, Right)

I need to get rotation matrix from direction vector (vForward) I also have vRight and vUp vectors. All those vectors are unit vectors.

I just need to get rotation matrix.

To get rotation matrix for rotation in only one plane (xy) parallel to ground, I do this: XMMATRIX xmResult;

Vec3f vFwd = pPlayer->VForward;
vFwd.z = 0;
vFwd.Normalize();

xmResult = XMMatrixSet( vFwd.y,     -vFwd.x,    0,      0,
    vFwd.x,      vFwd.y,    0,      0,
    0,          0,          1,      0,
    0,          0,          0,      1);

Above code only get rotation matrix to rotate around Z axis:

I would like to get the code to rotate around all axis. This is coordinate system I'm forced to use. I know it is strange:

Coordinate System I'm using

This is how I'm using my matrix later in code:

XMStoreFloat3((XMFLOAT3*)&vStart, XMVector3Transform(XMLoadFloat3((XMFLOAT3*)&vStart), xmTransformation));
XMStoreFloat3((XMFLOAT3*)&vEnd, XMVector3Transform(XMLoadFloat3((XMFLOAT3*)&vEnd), xmTransformation));
like image 541
Hooch Avatar asked Jun 25 '12 14:06

Hooch


People also ask

How do you convert a rotation vector to a rotation matrix?

rotationMatrix = rotationVectorToMatrix( rotationVector ) returns a 3-D rotation matrix that corresponds to the input axis-angle rotation vector. The function uses the Rodrigues formula for the computation.

Which way does the rotation matrix rotate?

A rotation matrix can be defined as a transformation matrix that operates on a vector and produces a rotated vector such that the coordinate axes always remain fixed. These matrices rotate a vector in the counterclockwise direction by an angle θ. A rotation matrix is always a square matrix with real entities.


1 Answers

Depending on how you use your matrices, Right, Up and Forward should correspond to the rows or columns of your matrix.

xmResult = XMMatrixSet( vRight.x, vRight.y, vRight.z, 0, vFwd.x, vFwd.y, vFwd.z, 0, vUp.x, vUp.y, vUp.z, 0, 0, 0, 0, 1);
like image 87
Andreas Brinck Avatar answered Oct 24 '22 10:10

Andreas Brinck