直接 x c++相机运动
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
网上有很多关于这个主题的教程,所以 Google 肯定会帮助你。
至于基础知识:您将需要存储您的位置和相机旋转。假设 Z 是您的上轴,您应该使用箭头仅更改 X 和 Y。
我们还可以说,您将存储相机方向作为沿 Z 轴(移动方向)和 X 轴的旋转的组合(从上往下看)。
简单类:
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:
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 输入处理,我认为您应该先花一两个小时来学习。
好的,就是这样,希望对你有帮助
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:
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