我如何制作射弹?
我完全被这个问题难住了。我正在使用 C++ 和 SFML 1.6 来开发我正在开发的游戏,但我没有任何想法。如何制作射弹(如子弹)?我只是不明白。可能是我睡眠不足,但我不知道。
所以我的问题是如何创建一个根据鼠标所在位置朝确定方向移动的精灵? (想象一下带有鼠标瞄准的自上而下的射击游戏)
I am totally stumped on this one. I'm using C++ and SFML 1.6 for a game I'm developing, and I have no bloody idea. How do I make projectiles (like bullets)? I just don't understand it. It could be my lack of sleep but I don't know.
So my question is how do I create a Sprite that moves in a definite direction based on where the mouse is? (Think of a top down shooter with mouse aiming)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的解决方案:
如果鼠标位于 Mx,My 且船舶位于 Sx,Sy,则计算从船舶到鼠标的方向:
Dx=Sx-Mx
Dy=Sy-My
现在标准化 D(这意味着对其进行缩放,使其长度为 1):
现在 Dx 是您想要在 x 轴上移动子弹的距离,以获得子弹速度 1。
因此,您移动的每一帧像这样的子弹(子弹位置:Bx,子弹速度:Bs [以像素每毫秒] 帧时间 Ft [以毫秒为单位])
这给你一个子弹,它以与鼠标方向无关的速度向鼠标位置移动鼠标或游戏的帧速率。
编辑:正如@MSalters所说,当鼠标位于船正上方时,您需要检查 DLen==0 情况,以避免标准化时除以零错误
Easiest solution:
If the mouse is at Mx,My and the ship is at Sx,Sy then calculate the direction from the ship to the mouse:
Dx=Sx-Mx
Dy=Sy-My
Now normalise D (this means scale it so that it's length is one):
Now Dx is the distance you want to move the bullet on the x axis in order to get bullet speed of 1.
Thus each frame you move the bullet like so (position of bullet: Bx,By Speed of bullet: Bs [in pixels per millisec] Frame time Ft[in millisec])
This give you a bullet that moves towards the mouse position at a speed independent of the direction of the mouse or framerate of the game.
EDIT: As @MSalters says you need to check for the DLen==0 case when the mouse is directly above the ship to avoid division by zero errors on the normalise
一种方法是使子弹面向鼠标,然后使用三角法将其移动到 x 和 y 轴上,以从角度找到下斜线。我认为我解释得不是很好,所以这里是使精灵从旋转中移动的代码:
One way to do it is to make the bullet face the mouse and then move it across the x and y axis by using trigonometry to find the hypotinuse from the angle. I don't think i explained this very well, so here the code to make a sprite move from its rotation: