My Goal: To create a routine that will move an entity relative to that entity''s current position and rotation in the world.
Example: Given the vector (4,0,5) the routine will update the position matrix of the entity so as to move it 5 units forward (based on left handed system) and 4 units to the right of whatever direction it is facing.
In each frame I setup a Scale, Rotation and Position matrix that are combined in the following routine to produce the WorldMatrix. If the user presses the up arrow, the vector called "RelativePosition" is set to (0,0,5) to move the entity forward.
VOID Entity::Transform(LPDIRECT3DDEVICE8 Device)
{
D3DXMATRIX tempMatrix;
D3DXVECTOR4 transformedVector;
if (D3DXVec3Length(&RelativePosition))
{
D3DXVec3Transform(&transformedVector, &RelativePosition, &RotationMatrix);
D3DXMatrixTranslation(&tempMatrix, transformedVector.x, transformedVector.y, transformedVector.z);
PositionMatrix *= tempMatrix;
RelativePosition.x = 0;
RelativePosition.y = 0;
RelativePosition.z = 0;
}
WorldMatrix = ScaleMatrix * RotationMatrix * PositionMatrix;
Device->SetTransform( D3DTS_WORLD, &WorldMatrix );
}
So in order to move relative to my current orientation I am transforming the RelativePosition vector by the current rotation matrix, converting it into a translation matrix, and multiplying it into my current position matrix.
This works except in one situation I have found so far. The routine that sets up my rotation matrix prior to this can rotate around an arbitrary pivot point. The above does not work when using a pivot point other than 0,0,0.
Details on the rotation by a pivot point...
First I create a translation matrix that aligns the pivot point of the entity with the world origin before multiplying in the X,Y, and Z rotation matrices and then I multiply in the inverse of the tranlsation matrix I made to move the entity back.
RotationMatrix = TranslateToPivotMatrix * RotX * RotY * RotZ * InverseTranslateToPivotMatrix;
Does anyone know from this what might be going wrong?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform.
You can accept analytics or continue with necessary cookies only.
Learn more