基本上,我想让我的相机漂浮在一个角色(aCar)后面并在它转动时留在它后面,这样它总是漂浮在角色后面,此刻我的相机转向同一个方向作为我的角色,但我似乎无法让它相对于他们所面对的方向而落后。
我在这里找到了一个教程http://www.braynzarsoft.net/index.php?p=D3D11ThirdPersonCam 但是使用XMVECTOR这些非常不合作的东西,除此之外还不清楚某些功能在数学上实际上做了什么
不用多说,这是我的相机相关代码及其背后的对象
更新方法
XMFLOAT3 relativeMovement;
if (GetAsyncKeyState(0x57)) //W key press
{
relativeVectorCalc(0, true);
}
if (GetAsyncKeyState(0x53)) // S Key Press
{
relativeVectorCalc(1, true);
}
if (GetAsyncKeyState(0x41)) //A Key Press
{
relativeVectorCalc(3, true);
}
if (GetAsyncKeyState(0x44)) //D Key Press
{
relativeVectorCalc(2, true);
}
if (GetAsyncKeyState(0x45)) //E Key Press
{
CameraRotate(1);
//relativeVectorCalc(2, false);
aCar.at(0)->setWorld({ 1.0f, 1.0f, 1.0f }, aCar.at(0)->getPos(), 0.0f, 0.001f, 0.0f);
} else if(GetAsyncKeyState(0x51)) //Q Key Press
{
CameraRotate(0);
//relativeVectorCalc(3, false);
aCar.at(0)->setWorld({ 1.0f, 1.0f, 1.0f }, aCar.at(0)->getPos(), 0.0f, -0.001f, 0.0f);
}
cam->CalculateViewProjection();
void Application::relativeVectorCalc(int k, bool P)
{
XMFLOAT3 relativeMovement;
XMFLOAT4 camAt = cam->GetAt();
XMFLOAT4 camEye = cam->GetEye();
//Finds the direction the player is facing and moves relative to that based on the key the player pressed
if (k == 0)
{
relativeMovement.x = (camAt.x - camEye.x); //Calculates the distance between the camera's center and its eye
relativeMovement.y = 0.0f;
relativeMovement.z = (camAt.z - camEye.z); //See above
}
else if (k == 1)
{
relativeMovement.x = 0 - (camAt.x - camEye.x);
relativeMovement.y = 0.0f;
relativeMovement.z = 0 - (camAt.z - camEye.z);
}
else if (k == 2)
{
relativeMovement.x = (camAt.z - camEye.z);
relativeMovement.y = 0.0f;
relativeMovement.z = 0 - (camAt.x - camEye.x);
}
else if (k == 3)
{
relativeMovement.x = 0 - (camAt.z - camEye.z);
relativeMovement.y = 0.0f;
relativeMovement.z = (camAt.x - camEye.x);
}
relativeMovement = (relativeMovement); //Makes the vector normalised
//Here is where the speed of the movement is controlled via division
relativeMovement.x = relativeMovement.x / 100; //slows down movement
relativeMovement.z = relativeMovement.z / 100;
if (P)
{
PlayerMove(relativeMovement);
CameraMove(relativeMovement);
}
else if (!P)
{
CameraMove(relativeMovement);
}
}
void Application::CameraMove(XMFLOAT3 straightMovementVector)
{
XMFLOAT4 cameraEye = cam->GetEye();
XMFLOAT4 cameraAt = cam->GetAt();
cameraEye.x = cameraEye.x + straightMovementVector.x;
cameraEye.z = cameraEye.z + straightMovementVector.z;
cameraAt.x = cameraAt.x + straightMovementVector.x;
cameraAt.z = cameraAt.z + straightMovementVector.z;
cameraAt.y = aCar.at(0)->getPos().y + 3.0f;
cam->SetEye(cameraEye);
cam->SetAt(cameraAt);
}
void Application::PlayerMove(XMFLOAT3 move)
{
XMFLOAT3 p = aCar.at(0)->getPos();
p.x = p.x + move.x;
p.z = p.z + move.z;
aCar.at(0)->setPos(p);
}
void Application::CameraRotate(int x)
{
XMFLOAT4 camAt = cam->GetAt();
XMFLOAT4 camEye = cam->GetEye();
XMFLOAT3 carPos = aCar.at(0)->getPos();
XMFLOAT2 dist = { carPos.x - camEye.x, carPos.z - camEye.z };
if (x == 0)
{
camRotation += 0.001f;
camAt.x = cos(camRotation) + camEye.x;
camAt.z = sin(camRotation) + camEye.z;
} else if (x == 1)
{
camRotation -= 0.001f;
camAt.x = cos(camRotation) + camEye.x;
camAt.z = sin(camRotation) + camEye.z;
}
cam->SetEye(camEye);
cam->SetAt(camAt);
}
非常感谢任何帮助,谢谢!