简体   繁体   English

如何创建水平居中的“重力”? -libGDX-

[英]How would I create Horizontally centered “Gravity” ? - libGDX -

This is a seemingly simple game mechanic that I've been trying to figure out how to do. 这是一个看似简单的游戏机制,我一直在努力寻找方法。

To try and explain I will describe a idea (problem): 为了尝试解释,我将描述一个想法(问题):

  • Basically we say there's a vertical line that is centered in the screen. 基本上,我们说有一条垂直线位于屏幕中心。

  • We have a sprite object that changes it's horizontal velocity to dodge missiles, however in doing that the object would just drift away. 我们有一个精灵对象,可以改变它的水平速度来躲避导弹,但是这样做时,该对象只会漂移掉。

  • How can I add a strong gravity force to the horizontal "center line" of my screen so that my sprite will "fall" back into it every time it boosts its velocity outwards? 如何向屏幕的水平“中心线”添加强大的重力,以便每次精灵向外提高速度时,精灵都会“掉进”其中?

I could post my source code but it wouldn't be too helpful to solving the question in this particular situation. 我可以发布我的源代码,但是在这种特殊情况下解决问题不会太有用。

I've searched around for days trying to figure this out so any help especially with code examples would be very helpful! 我已经搜寻了好几天,试图找出答案,所以任何帮助,尤其是代码示例,都将非常有帮助!

在此处输入图片说明

I've programmed this type of thing in the past. 我过去已经编写过这类程序。 Gravity (in physics) is an acceleration, so 重力(在物理学中)是一种加速度,所以

1) if the sprite is to the right of the line you subtract from its horizontal velocity every 1/n seconds, and 1)如果子画面位于直线的右侧,则每1 / n秒从其水平速度中减去一次,并且

2) if the sprite is to the left of the line you add to its horizontal velocity every 1/n seconds. 2)如果精灵位于行的左侧,则每1 / n秒将其添加到其水平速度。

Experiment with adding/subtracting a constant, or with adding/subtracting a number that increases the farther the sprite is from the center line. 尝试加/减一个常数,或加/减一个使子图形离中心线越远的数字越大的值。

Either way you do it, that's going to create a pendulum effect. 无论采用哪种方式,都将产生摆的效果。 You'll also have to add a dampening factor if you don't want that. 如果您不想这样做,还必须添加一个阻尼因子。 One simple approach is that if the sprite is headed away from the center line, the value you add/subtract is larger than if the sprite is heading back towards the center line. 一种简单的方法是,如果精灵离开中心线,则您添加/减去的值大于精灵返回中心线时的值。 So the "gravity" that pulls the sprite to a stop is greater than the gravitational acceleration that brings the sprite back to the center line. 因此,将子图形拉到停止位置的“重力”大于使子图形返回中心线的重力加速度。

As you are using libgdx you should also use camera. 当您使用libgdx时,还应该使用相机。 So you don't have to calculate verything in pixels. 因此,您不必计算像素数。 So for example you say my screen is 16 worldunits width and 9 world units height (16/9 aspect ratio). 例如,您说我的屏幕是16个世界单位的宽度和9个世界单位的高度(16/9纵横比)。 So you can say the center of gravity is in the center of that 16, so at 8.5 if i am not wrong. 所以你可以说重心在那16的中心,如果我没记错的话,在8.5。 Now you can say: if (player.center.x < 8.5f) { player.xSpeed += GRAVITY_HORIZONTAL } and if (player.center.x > 8.5) { player.xSpeed -= GRAVITY_HORIZONTAL } . 现在您可以说: if (player.center.x < 8.5f) { player.xSpeed += GRAVITY_HORIZONTAL }if (player.center.x > 8.5) { player.xSpeed -= GRAVITY_HORIZONTAL } In this case the gravity is a constant value. 在这种情况下,重力是恒定值。 But as @BrettFromLA said you can also let the value grow if the distance to the center grows. 但是正如@BrettFromLA所说,如果到中心的距离增加,您也可以让值增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM