本文是DirectX11 With Windows SDK--10 基于Transform的摄像机类与GameObject类 的课后作业
答案仅供参考
原文链接:DirectX11 With Windows SDK--10 基于Transform的摄像机类与GameObject类
在第三人称模式下,让物体也能够进行前后、左右的平移运动
//修改代码片段 else if (m_CameraMode == CameraMode::ThirdPerson) { // 第三人称摄像机的操作 cam3rd->SetTarget(woodCrateTransform.GetPosition()); // 绕物体旋转 cam3rd->RotateX(mouseState.y * dt * 2.5f); cam3rd->RotateY(mouseState.x * dt * 2.5f); cam3rd->Approach(-mouseState.scrollWheelValue / 120 * 1.0f); if (keyState.IsKeyDown(Keyboard::W)) { woodCrateTransform.Translate(XMFLOAT3(0.0f,0.0f,1.0f), dt * 0.6f); } if (keyState.IsKeyDown(Keyboard::S)) { woodCrateTransform.Translate(XMFLOAT3(0.0f, 0.0f, -1.0f), dt * 0.6f); } if (keyState.IsKeyDown(Keyboard::A)) { woodCrateTransform.Translate(XMFLOAT3(-1.0f, 0.0f, 0.0f), dt * 0.6f); } if (keyState.IsKeyDown(Keyboard::D)) { woodCrateTransform.Translate(XMFLOAT3(1.0f, 0.0f, 0.0f), dt * 0.6f); } }
在第三人称模式下,使用平躺的圆柱体,让其左右平移运动改为左右旋转运动,前后运动改为朝前滚动
// 切换 第三人称时改变物体 ComPtr<ID3D11ShaderResourceView> texture; // 初始化圆柱 HR(CreateDDSTextureFromFile(m_pd3dDevice.Get(), L"..\\Texture\\WoodCrate.dds", nullptr, texture.GetAddressOf())); m_WoodCrate.SetBuffer(m_pd3dDevice.Get(), Geometry::CreateCylinder()); m_WoodCrate.SetTexture(texture.Get()); Transform& wTransform = m_WoodCrate.GetTransform(); wTransform.SetRotation(0.5f * 3.1415926f, 0.0f, 0.0f); //修改代码 else if (m_CameraMode == CameraMode::ThirdPerson) { // 第三人称摄像机的操作 cam3rd->SetTarget(woodCrateTransform.GetPosition()); // 绕物体旋转 cam3rd->RotateX(mouseState.y * dt * 2.5f); cam3rd->RotateY(mouseState.x * dt * 2.5f); cam3rd->Approach(-mouseState.scrollWheelValue / 120 * 1.0f); if (keyState.IsKeyDown(Keyboard::W)) { woodCrateTransform.Translate(XMFLOAT3(0.0f,0.0f,1.0f), dt * 0.6f); woodCrateTransform.RotateAxis(XMFLOAT3(1.0f, 0.0f, 0.0f), -dt * 0.6f * 3.1415926f); } if (keyState.IsKeyDown(Keyboard::S)) { woodCrateTransform.Translate(XMFLOAT3(0.0f, 0.0f, -1.0f), dt * 0.6f); woodCrateTransform.RotateAxis(XMFLOAT3(1.0f, 0.0f, 0.0f), dt * 0.6f * 3.1415926f); } if (keyState.IsKeyDown(Keyboard::A)) { woodCrateTransform.Translate(XMFLOAT3(-1.0f, 0.0f, 0.0f), dt * 0.6f); woodCrateTransform.RotateAxis(XMFLOAT3(0.0f, 0.0f, 1.0f), dt * 0.6f * 3.1415926f); } if (keyState.IsKeyDown(Keyboard::D)) { woodCrateTransform.Translate(XMFLOAT3(1.0f, 0.0f, 0.0f), dt * 0.6f); woodCrateTransform.RotateAxis(XMFLOAT3(0.0f, 0.0f, 1.0f), -dt * 0.6f * 3.1415926f); } }
尝试实现带有父子关系的Transform变换和GameObject
放弃