Advertisement

Calculate target pos from transform matrix and distance to target.

Started by January 20, 2016 09:30 PM
1 comment, last by Happy SDE 8 years, 7 months ago

Part1

I have a camera. It is specified by 4x4 matrix from XML.
Also I have scalar value “distance to target”.
I need to calculate the target pos.

First step: calculate cameraPos and direction quaternion.


XMMATRIX M = LoadMatrixFromXML();
XMVECTOR scale, rotationQuaternion, cameraPos;
XMMatrixDecompose(&scale, & rotationQuaternion, &cameraPos, M);
float distanceToTarget = LoadDistanceFromXML();

Question1:
How to calculate position of a target, if I have cameraPos, distanceToTarget and rotationQuaternion?

=============================================

Part2.

I found many sources about what are quaternions, vectors, matrices and the math that is behind of them.
I found a lot of DirectXMath functions in MSDN.
But I can’t find a missed link between them: how to link math tasks like I am struggled with the DirectXMath library.

Quesion2:
Can anyone share a good connection link between math tasks and how are they can be solved with DirectXMath library?

It might be a link to a book, url, ....

Thanks in advance.

So your camera is pointing at the target, you have it's rotation, you have the distance to the target and you have the camera's position?

It should be a case or rotating your 'forward' vector by the rotation, multiplying it by the distance and adding it to your camera's position. It's been a while since I have done anything like this but it should be along the lines of:

toTarget = rotation*forward;

targetPos = cameraPos + toTarget * distancetoTarget;

Forward will be just whatever is considered the default forward in the space you use (I think for DirectX is would be (0, 0, 1) but I could be wrong. toTarget is just the forward vector rotated by the rotation.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Advertisement

Thank you, Nanoha!

It works.

The vector (0,0,1) is exactly the view direction.

This topic is closed to new replies.

Advertisement