如何制作平滑的相机跟随算法? [英] How to make a smooth camera follow algorithm?
问题描述
我正在使用 LibGDX (Java) 制作游戏.
I am making a game with 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.
是否有任何 libgdx 库可以做到这一点,或者任何人都有这种经验?
Is there any libgdx libs that do that or anyone had this experience?
推荐答案
尝试一些简单的事情,比如 lerping 十分之一的距离.效果出奇的好.
Try something simple like lerping a tenth of the distance. It works surprisingly well.
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;
这篇关于如何制作平滑的相机跟随算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!