DirectX11相机运动

时间:2014-03-26 20:10:57

标签: c++ visual-studio-2010 directx-11

所以我在我的程序中编写了一些汽车的动作。

我只是在Z position上增加相机的XMVECTOR pos吗?我之前做过这件事,它向前走,但几乎开始将相机转向地面。

XMVECTOR pos = XMVectorSet(x, y, z + mCarTranslation.z, 1.0f);

无论如何,这是我的代码的一部分。

/ Convert Spherical to Cartesian coordinates.
float x = mRadius*sinf(mPhi)*cosf(mTheta);
float z = mRadius*sinf(mPhi)*sinf(mTheta);
float y = mRadius*cosf(mPhi);

mEyePosW = XMFLOAT3(x, y, z);

// Build the view matrix.
XMVECTOR pos    = XMVectorSet(x, y, z, 1.0f);
XMVECTOR target = XMVectorZero();
XMVECTOR up     = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

XMMATRIX V = XMMatrixLookAtLH(pos, target, up);
XMStoreFloat4x4(&mView, V);

// Button down event.
if (GetAsyncKeyState('W') & 0x8000)
{
    mCarTranslation.z += -4.0f*dt;
}
if(GetAsyncKeyState('S') & 0x8000)
{
    mCarTranslation.z += 2.0f*dt;
}
if(GetAsyncKeyState('A') & 0x8000)
{
    mCarTranslation.x += 1.0f*dt;
}
if(GetAsyncKeyState('D') & 0x8000)
{
    mCarTranslation.x += -1.0f*dt;
}

XMMATRIX carScale = XMMatrixScaling(0.5f, 0.5f, 0.5f);
XMMATRIX carOffset = XMMatrixTranslation(mCarTranslation.x, mCarTranslation.y, mCarTranslation.z);
XMStoreFloat4x4(&mCarWorld, XMMatrixMultiply(carScale, carOffset));

1 个答案:

答案 0 :(得分:0)

相机最终向地面旋转,因为您每帧都在重建具有相同目标位置(0,0,0)的矩阵。如果要保持相同的方向,则需要将目标位置设置为相对于眼睛位置,即target = XMVectorSet(x - xInitial, y - yInitial, z - zInitial, 1.0f)