直接 x c++相机运动

发布于 2024-12-14 02:14:59 字数 245 浏览 0 评论 0原文

我对 directx 还很陌生,所以这听起来可能很基础。

我已经开始开发一款第一人称游戏,你可以在其中走过房间,我编码的语言是 c++,我使用 directx 来帮助我创建我的游戏。

到目前为止,我已经绘制了所有带有门等的房间,但我有点困惑如何制作第一人称相机并允许用户使用键盘上的箭头键向前、向后和左右移动。

越简单越好,因为我是初学者。

谁能帮助我解决这个问题或为我指出正确的方向?

提前致谢

I'm fairly new to directx so this may sound really basic.

I have started working on a first person game where you can walk through rooms, the language i am coding in is c++ and I'm using directx to help me create my game.

So far i have all the rooms drawn with doors etc but im a bit stuck how to make a first person camera and allow the user move forwards, backwards and side to side using the arrow keys on a keyboard.

The simpler the better as i am a beginner.

Could anyone help me out with this or point me in the right direction?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-12-21 02:14:59

网上有很多关于这个主题的教程,所以 Google 肯定会帮助你。

至于基础知识:您将需要存储您的位置和相机旋转。假设 Z 是您的上轴,您应该使用箭头仅更改 X 和 Y。

我们还可以说,您将存储相机方向作为沿 Z 轴(移动方向)和 X 轴的旋转的组合(从上往下看)。

简单类:

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.
};

sinf 和 cosf 函数前面的“-”符号是因为您可能想要这种行为,请随意更改它们。

至于相机,您必须在帧之间实现鼠标增量。在每一帧中将鼠标位置与前一帧进行比较。然后将其乘以转动和观察速度并直接设置为相机值。

希望这有帮助。

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.

暗恋未遂 2024-12-21 02:14:59

我更喜欢 OpenGL,所以我无法在技术方面帮助你,我能做的就是给你一个方向。

一般来说,3D 相机具有:

平移 - 相机所在位置 (x, y, z)

旋转 - 相机围绕每个轴的角度

与您想要做的相关仅适用于翻译部分:

  1. 监视用户输入,并在用户按下任一箭头键时进行捕获。
  2. 当按下箭头键时,您想要开始更改相机平移。如果用户按下向上键,您将在主游戏循环的每次迭代中向相应的相机翻译组件 (x) 添加一个常量值,直到他释放该键。如果他按下向下键,您会想要减去该值而不是相加。
  3. 当他释放按键时,您的代码需要停止将该值添加到相机翻译中。

假设您的游戏以 60Hz 运行,并且您在每次迭代中为用户想要移动的每个方向的相机平移添加 1/60 个单位。如果用户按住向上箭头键 2 秒钟,相机将向前移动 2 个单位。

这是一般的“理论”,现在我只能向您指出我发现的可能对解决您的问题的技术方面有用的网页:

DirectX 相机移动 - 我猜这篇文章的内容超出了您的需要,但它看起来相当不错,我认为您应该阅读无论如何......但你可以跳到视图转换部分。

输入处理 - 没什么可做的例如,常规 Win32 输入处理。如果您不熟悉 win32 输入处理,我认为您应该先花一两个小时来学习。

好的,就是这样,希望对你有帮助

I am more of an OpenGL guy so I cannot help you with the technical side, what I can do is give you a direction.

In general, a 3D camera has:

Translation - where the camera is (x, y, z)

Rotation - angle of the camera around each axis

What you want do is related to the translation part only:

  1. Monitor the user input, and capture when he presses one of the arrow keys.
  2. When an arrow key is down, you want to start changing the camera translation. If the user presses the up key, you would add a constant value to the corresponding camera translation component (x) each iteration of your main game loop until he releases the key. If he presses the down key, you would want to subtract that value instead of adding it.
  3. When he releases the key, your code needs to stop adding that value to the camera translation.

Let's assume that your game runs at 60Hz, and you add, say, 1/60 units to the camera translation each iteration for each direction the user wants to go. If the user held up the up arrow key for 2 seconds, the camera would have moved forward 2 units.

This is the "theory" in general, now I can only point you to web pages I found that may be useful for solving the technical side of your problem:

DirectX camera movement - I'm guessing this article has a lot more than you need, but it looked pretty good and I think that you should read it anyway... But you can just skip to the View Transformation part.

Input handling - nothing much to say, regular Win32 input handling. If you are not familiar with win32 input handling I think that you should take an hour or two to learn that first.

Alright that's it, hope I helped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文