我如何制作射弹?

发布于 2024-10-15 16:02:25 字数 159 浏览 4 评论 0原文

我完全被这个问题难住了。我正在使用 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 技术交流群。

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

发布评论

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

评论(2

指尖上得阳光 2024-10-22 16:02:25

最简单的解决方案:
如果鼠标位于 Mx,My 且船舶位于 Sx,Sy,则计算从船舶到鼠标的方向:
Dx=Sx-Mx
Dy=Sy-My

现在标准化 D(这意味着对其进行缩放,使其长度为 1):

DLen=sqrt(Dx*Dx + Dy*Dy)
Dx/=DLen;
Dy/=DLen;

现在 Dx 是您想要在 x 轴上移动子弹的距离,以获得子弹速度 1。

因此,您移动的每一帧像这样的子弹(子弹位置:Bx,子弹速度:Bs [以像素每毫秒] 帧时间 Ft [以毫秒为单位])

Bx=Bx+Dx*Bs*Ft
By=By+Dy*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):

DLen=sqrt(Dx*Dx + Dy*Dy)
Dx/=DLen;
Dy/=DLen;

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])

Bx=Bx+Dx*Bs*Ft
By=By+Dy*Bs*Ft

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

谎言月老 2024-10-22 16:02:25

一种方法是使子弹面向鼠标,然后使用三角法将其移动到 x 和 y 轴上,以从角度找到下斜线。我认为我解释得不是很好,所以这里是使精灵从旋转中移动的代码:

void sMove(sf::Sprite& input,float toMove, int rotation){
bool negY = false;
bool negX = false;
int newRot = rotation;
if (rotation <= 90){
    negY = true;
}
else if (rotation <= 180){
    //newRot -= 90;
    negY = true;
}
else if (rotation <= 360){
    newRot -= 270;
    negY = true;
    negX = true;
}
float y = toMove*cos(newRot*PI/180);
float x = toMove*sin(newRot*PI/180);
if (negY){
    y = y*-1;
}
if (negX){
    x = x*-1
}
input.move(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:

void sMove(sf::Sprite& input,float toMove, int rotation){
bool negY = false;
bool negX = false;
int newRot = rotation;
if (rotation <= 90){
    negY = true;
}
else if (rotation <= 180){
    //newRot -= 90;
    negY = true;
}
else if (rotation <= 360){
    newRot -= 270;
    negY = true;
    negX = true;
}
float y = toMove*cos(newRot*PI/180);
float x = toMove*sin(newRot*PI/180);
if (negY){
    y = y*-1;
}
if (negX){
    x = x*-1
}
input.move(x, y);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文