让角色绕圆圈移动
所以我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经有了从地球中心到玩家的矢量,例如 (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.