让角色绕圆圈移动

发布于 2024-11-16 02:52:49 字数 681 浏览 0 评论 0原文

所以我有一个 2d 游戏,通常只有重力和“平坦”水平,但我添加了具有自己重力的“行星”。

我有一个名为 addForce(float xForce, float yForce) 的函数,我用它来移动角色。假设我调用了player.addForce(1, -1);玩家会向上并向右移动(尽管轻微)。这在重力正常向下的水平上效果很好,但对于行星则不然。 还有另一个名为 EarthAngle 的浮点数,它是:

atan2(player.getY()-earth.getY(),player.getX()-earth.getX());

我为行星上的跳跃代码所做的是:

player.addForce(cos(earthAngle)*1500, sin(earthAngle)*1500);

效果很好。然而,我一直在思考如何让角色在地球上行走。

目前对于运动代码我有:

player.addForce(25*x_*cos(earthAngle), 25+x_*sin(earthAngle));

它仅适用于某些部分,并且在底部相反,并且在某些部分上更强/更弱,x_ 可以是 -1(左)或 1(右)。我猜他们是一个非常优雅的解决方案,我只是忽略了。谢谢。

So I have a 2d game which normally just has gravity and "flat" levels however I have added in "planets" which have their own gravity.

I have a function called addForce(float xForce, float yForce) that I use to move my character. So say if I called player.addForce(1, -1); the player would move up and to the right(albeit slightly). This worked fine on the levels with regular downward gravity, however with planets it is not so.
There is another float called earthAngle which is:

atan2(player.getY()-earth.getY(), player.getX()-earth.getX());

What I did for the jumping code on the planets is:

player.addForce(cos(earthAngle)*1500, sin(earthAngle)*1500);

which works well. However I am stuck on how to make the character walk around the planet.

Currently for the movement code I have:

player.addForce(25*x_*cos(earthAngle), 25+x_*sin(earthAngle));

which only works on some parts and works in reverse on the bottom as well as being stronger/weaker on some parts, x_ can be either -1(left) or 1(right). I'm guessing their is a really elegant solution I am just overlooking. Thanks.

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

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

发布评论

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

评论(1

笑红尘 2024-11-23 02:52:49

由于您已经有了从地球中心到玩家的矢量,例如 (x,y),因此您可以使用垂直于该 (-y,x) 的矢量作为行走力的方向。

Since you already have the vector from the center of the planet to the player eg (x,y), you can use a vector perpendicular to that (-y,x) as the direction for the walking force.

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