所以我在我的程序中的一辆车上做了一些运动编程。
我只是在XMVECTOR pos
上增加相机的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));
发布于 2014-03-26 22:06:02
相机最终会向地面旋转,因为你要重新创建每一帧的矩阵,目标位置相同(0,0,0)。如果您希望保持相同的方向,则需要将目标位置设置为相对于眼睛位置,即target = XMVectorSet(x - xInitial, y - yInitial, z - zInitial, 1.0f)
。
https://stackoverflow.com/questions/22672013
复制相似问题