java 如何制作平滑的相机跟随算法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13227994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:58:02  来源:igfitidea点击:

How to make a smooth camera follow algorithm?

javaopengl2dlibgdxsmooth-scrolling

提问by Avetis Zakharyan

I am making a game with LibGDX (Java).

我正在用 LibGDX (Java) 制作游戏。

I need the camera to follow a fast moving character. The easiest way to do it is to just write this:

我需要相机跟随一个快速移动的角色。最简单的方法就是这样写:

this.getCamera().position.set(obj.x, obj.y, 0);

But, is there any algorithm to make this more smooth? Like when camera is not that strict, and is always a bit late: character goes quick right, camera follows with slight delay, or if you suddenly appeared somewhere far, camera doesn't teleport instantly but travels at a top speed to you when it comes closer it slows down a bit and finds you again.

但是,是否有任何算法可以使这更平滑?就像当相机不那么严格并且总是有点晚时:角色向右快速移动,相机稍微延迟跟随,或者如果您突然出现在很远的地方,相机不会立即传送而是以最高速度向您移动靠近它会放慢一点,然后再次找到你。

Is there any libgdx libs that do that or anyone had this experience?

是否有任何 libgdx 库可以做到这一点,或者任何人都有这种经验?

回答by Rod Hyde

Try something simple like lerping a tenth of the distance. It works surprisingly well.

尝试一些简单的事情,比如 lerping 十分之一的距离。它的效果出奇的好。

float lerp = 0.1f;
Vector3 position = this.getCamera().position;
position.x += (Obj.x - position.x) * lerp * deltaTime;
position.y += (Obj.y - position.y) * lerp * deltaTime;

回答by MutantXenu

Take a look at Aurelion Ribon's Java Universal Tween Engine. This performs interpolation and has several easing equations that I think would get you what you are looking for. It also has other advanced features like waypointing and chaining certain actions together for other interesting effects.

看看 Aurelion Ribon 的Java Universal Tween Engine。这会执行插值并有几个缓动方程,我认为它们可以满足您的需求。它还具有其他高级功能,例如航点和将某些动作链接在一起以获得其他有趣的效果。

Your game logic could check to see if the character is moving quickly or has a step change in terms of position. In response to this, turn your current camera position over to the tween engine and let it take over -- smoothly zooming to the character's current position.

您的游戏逻辑可以检查角色是否快速移动或在位置方面是否有阶跃变化。作为回应,将当前的相机位置转到补间引擎并让它接管——平滑地缩放到角色的当前位置。