Reputation: 15
Hi I'm new to using DirectX 11 and I'm just trying to get my game object to move forward based on it's rotation. This is what I have but it just moves in a line no matter if im turning it or not.
void Object::MoveObject(float x, float y, float z)
{
Position.x += x;
Position.y += y;
Position.z += z;
_translate = XMMatrixIdentity();
_translate *= XMMatrixTranslation(Position.x, Position.y, Position.z);
XMVECTOR forward = { Position.x,Position.y,Position.z };
XMFLOAT4X4 currrentWorld = GetWorld();
XMMATRIX currentWorldM = XMLoadFloat4x4(&currrentWorld);
XMVECTOR scale = currentWorldM.r[0];
XMVECTOR rotation = currentWorldM.r[1];
XMVECTOR translation = currentWorldM.r[2];
forward *= currentWorldM.r[3];
forward = XMVector3Normalize(forward);
translation = XMVectorAdd(translation, forward);
XMStoreFloat4x4(&_world, XMMATRIX(scale, rotation, translation, forward));
UpdateWorld();
}
Upvotes: 0
Views: 1187
Reputation: 21956
A matrix don’t contain scale in the top row, rotation in the second and translation in the third. They are more complex than that.
To decompose a matrix, you can use XMMatrixDecompose. To compose back, you can use XMMatrixTranslationFromVector, XMMatrixRotationQuaternion, XMMatrixScalingFromVector, and multiply the results.
Fortunately, to move object in it’s local coordinates (if that’s what you want) you don’t need to decompose the matrix. You need to construct a localTranslation
matrix using XMMatrixTranslationFromVector
, and update your world matrix to XMMatrixMultiply( localTranslation, _world )
P.S. I recommend DirectX Toolkit library for that kind of things. Especially if you’re new to DirectX. The SimpleMath.h header contains easy to use wrappers around those vectors and matrices.
Upvotes: 1