[英]DirectX11 Camera Movement
因此,我正在為我的程序中的汽車編程一些運動。
我是否只是增加XMVECTOR pos
上攝像機的Z position
XMVECTOR pos
Z position
? 我之前做了這個,它向前移動,幾乎所有人都開始將相機轉向地面。
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));
攝像機最終向地面旋轉,因為您要在具有相同目標位置(0,0,0)的每幀中重新創建矩陣。 如果要保持相同的方向,則需要將目標位置設置為相對於眼睛位置,即target = XMVectorSet(x - xInitial, y - yInitial, z - zInitial, 1.0f)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.