I use DirectX3D 11 to wirte an application and my Camera target vector is determined by the variables xdelta, ydelta, and zdelta.
I have to PAN my view in the XY as I move my mouse across the screen with the RMB & LMB pressed.
I figured that I need to add the delta in the mouse movement to my VIEW space, so that it pans in X and Y relative to my view, not world X and Y.
However, being new to this, I’m not sure how to convert my VIEW coordinates back to WORLD coordinates.
I hope that I am following all of the formatting rules, as I don’t post here enough to remember all of them exactly.
Any help would be appreciated. below is my snippet of code. Perhaps there is also a better way.
if ( m_Input->RMBPressed() == true && m_Input->LMBPressed() == true ) // Pan View
{
if ( curX != mouseX || curY != mouseY ) {
//Get Target Coordinates in View Space
D3DXVECTOR3 targetView = (D3DXVECTOR3 ( mView(2,0), mView(2,1), mView(2,2) );
//Add mouse XY delta vector to target View Coordinates
D3DXVECTOR3 delta ( (float)curX-(float)mouseX,
(float)curY-(float)mouseY, zdelta );
targetView += delta;
//Convert back to World Coordinates
}
}
I’m Trying a different approach, that I believe is the correct approach, but it still doesn’t appear to be working correctly.
I get the the delta in X and Y from the screen as my mouse moves and store them in the variables “xdelta” and “ydelta”.
I then create a transformation matrix
D3DXMATRIX mTrans;
I then populate the values in the matrix
// Rotation
mTrans(0,0) = delta.x;
mTrans(1,1) = delta.y;
// Translation
mTrans(0,3) = delta.x;
mTrans(1,3) = delta.y;
Now to get their corresponding View coordinates, I think I should multiply it by the View Matrix, which I call mView.
mTrans= mTrans*mView;
Now add these translated values to my current target X and Y which is determind by the variables “target_x” and “target_y”, it should move my target vector, relative to my view coordinates X and Y (i.e. orthogonal to my current view).
target_x += mTrans(0,3);
target_y += mTrans(1,3);
But it doesn’t. It moves my target, along the world X and Y axis, not my View X and Y axis.
MORE SNIPPETS
I do use the D3DXMatrixLookAtLH function, but I’m trying to calculate the change in the target location based on my mouse movements to get new target to feed into that function. I added in some code snippets:
if ( m_Input->RMBPressed() == true && m_Input->LMBPressed() == true ) // Pan View
{
if ( curX != mouseX || curY != mouseY ) {
//Get mouse XY delta vector
D3DXVECTOR3 delta ( (float)curX-(float)mouseX, (float)curY-(float)mouseY, zdelta );
//Create Transformation Matrix
D3DXMATRIX mTemp;
// Rotation
mTemp (0,0) = delta.x;
mTemp (1,1) = delta.y;
mTemp (2,2) = delta.z;
// Translation
mTemp (0,3) = delta.x;
mTemp (1,3) = delta.y;
mTemp (2,3) = delta.z;
//Convert to View Coordinates
mTemp = mTemp*mView;
xdelta += mTemp(0,3);
ydelta += mTemp(1,3);
// Convert Spherical to Cartesian coordinates: mPhi measured from +y
// and mTheta measured counterclockwise from z.
float height = mRadius * cosf(mPhi);
float sideRadius = mRadius * sinf(mPhi);
float x = xdelta + sideRadius * cosf(mTheta);
float z = zdelta + sideRadius * sinf(mTheta);
float y = ydelta + height;
// Build the view matrix.
D3DXVECTOR3 pos(x, y, z);
D3DXVECTOR3 target(xdelta, ydelta, zdelta);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); // Y Axis is up. Looking down Z.
D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
First of all, thank you for all of your information. It is helping me slowly understand DirectX matrices.
Am I correct in the logic below?
Assume my mouse change is 5.0 in X and 5.0 in Y on the screen. that is my delta. Z would always be 0.0.
I could find the view coordinates as follows.
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); // Y Axis is up. Looking down Z.
D3DXVECTOR3 delta ( 5.0f, 5.0f, 0.0f );
D3DXVECTOR3 origin ( 0.0f. 0.0f, 0.0f );
D3DMATRIX mTemp;
D3DXMatrixIdentity (&mTemp);
D3DXMatrixLookAtLH (&mTemp, &delta, &origin, &up );
I should now have the view coordinates of the XY delta stored in the mTemp view matrix?
If so, is the best way to proceed, by adding the XY delta View coordinates to the mView XY coordinates and then translating back to world coordinates, to get the actual XY world value I have to set the target to?
I’m at a loss as to how to achieve this. It’s really not all that clear to me, and all the books that I purchased on the subject are not clear either.
You can calculate world coordinates from local coordinates by multiplying you local coords by world matrix.
If you want your camera to move, than just define their current position using 3D vector, and then use D3DXMatrixLookAtLH to calculate view matrix from your current position.
Check out this tutorial for more details: http://www.braynzarsoft.net/index.php?p=D3D11WVP