我对directx 还很陌生,所以这听起来很基础。
我已经开始开发第一人称游戏,您可以在其中穿行房间,我编码的语言是 c++,我正在使用 directx 来帮助我创建我的游戏。
到目前为止,我已经用门等绘制了所有房间,但我有点卡住了如何制作第一人称相机并允许用户使用键盘上的箭头键向前、向后和左右移动。
我是初学者,越简单越好。
谁能帮我解决这个问题或指出我正确的方向?
提前致谢
我对directx 还很陌生,所以这听起来很基础。
我已经开始开发第一人称游戏,您可以在其中穿行房间,我编码的语言是 c++,我正在使用 directx 来帮助我创建我的游戏。
到目前为止,我已经用门等绘制了所有房间,但我有点卡住了如何制作第一人称相机并允许用户使用键盘上的箭头键向前、向后和左右移动。
我是初学者,越简单越好。
谁能帮我解决这个问题或指出我正确的方向?
提前致谢
There's a lot of tutorials in web covering this topic, so Google will certainly help you.
As for the basics: You will want to store your position and camera rotation. Assuming Z is your up-axis, you should use the arrows to change only the X and Y.
Let's also say, that you will store your camera orientation is stored as a composition of rotations along Z-axis (movement direction) and X axis (looking up-down).
Simple class:
class Player
{
protected:
float3 Position; // Z-up
float2 CameraRotation; // X for turning, Y for up-down
public:
void MoveForward()
{
Position.X += -cosf(CameraRotation.X) * PLAYER_SPEED;
Position.Y += -sinf(CameraRotation.X) * PLAYER_SPEED;
}
// when using any other arrow, just add a multiply of PI/2 to the camera rotation
// PI for backwards, +PI/2 for left strafe and -PI/2 for right strafe.
// If you don't want to use mouse, use left and right arrow to modify camera rotation
// and MoveForward and Backward will look the same, having different signs.
};
The '-' signs in front of sinf and cosf functions are there because you will probably want this kind of behavior, feel free to change them.
As for camera, you will have to implement mouse delta between frames. In every frame compare the mouse position with the previous one. Then multiply it by turning and looking speed and set directly to camera value.
Hope this helped.
我更像是一个 OpenGL 的人,所以我无法在技术方面帮助你,我能做的就是给你一个方向。
通常,3D 相机具有:
平移- 相机在哪里 (x, y, z)
旋转- 相机围绕每个轴的角度
您要做的仅与翻译部分有关:
假设您的游戏以 60Hz 运行,并且您在每次迭代中为用户想要去的每个方向添加 1/60 单位到相机平移。如果用户按住向上箭头键 2 秒,相机将向前移动 2 个单位。
这是一般的“理论”,现在我只能将您指向我发现可能对解决您的问题的技术方面有用的网页:
DirectX 摄像机移动——我猜这篇文章比你需要的要多得多,但它看起来很不错,我认为无论如何你都应该阅读它……但你可以直接跳到视图转换部分。
输入处理- 没什么好说的,常规的 Win32 输入处理。如果您不熟悉 win32 输入处理,我认为您应该先花一两个小时来学习。
好的,就是这样,希望对我有所帮助