我怎样才能对顶视图游戏的摄像机进行lerp(线性插值),该摄像机跟随玩家,因此当它接近目标时它不会颤抖。这是我用于摄像机翻译的代码:
//Creating a vector 3 which represents the target location (my player)
Vector3 target = new Vector3(
(float)player.getPosition().x*map.getTileWidth()+(float)map.getTileWidth()/2,
(float)player.getPosition().y*map.getTileHeight()+(float)map.getTileHeight()/2,
0);
//Creating vector 3 which gets camera position
Vector3 cameraPosition = camera.position;
//Interpolating (lerping) vector
cameraPosition.lerp(target, 0.1f);
//Updating camera and setting it for batch
camera.position.set(cameraPosition);
camera.update();
batch.setProjectionMatrix(camera.combined);
我认为我做得对,但是alpha值可能太小,但如果我把它做得更大,那么相机移动得太快而出现其他问题。我可以用这个alpha值来制作颤抖(在我的例子中alpha是0.1f)吗?
答案 0 :(得分:2)
我认为你不想要线性插值,但是这样的事情(没有经过测试,矢量操作应该被认为是伪代码):
//Change speed to your need
final float speed=0.1f,ispeed=1.0f-speed;
//The result is roughly: old_position*0.9 + target * 0.1
cameraPosition.scale(ispeed);
target.scale(speed);
cameraPosition.add(target);
camera.position.set(cameraPosition);
答案 1 :(得分:2)
所以我尝试了libgdx,它运行得很好,只改变了Vector3中scl函数的缩放比例和delta的速度
public void updateCam(float delta,float Xtaget, float Ytarget) {
//Creating a vector 3 which represents the target location myplayer)
Vector3 target = new Vector3(Xtaget,Ytarget,0);
//Change speed to your need
final float speed=delta,ispeed=1.0f-speed;
//The result is roughly: old_position*0.9 + target * 0.1
Vector3 cameraPosition = camBox2D.position;
cameraPosition.scl(ispeed);
target.scl(speed);
cameraPosition.add(target);
camBox2D.position.set(cameraPosition);
}
答案 2 :(得分:0)
你做错了是cameraPosition.lerp(target, 0.1f);
,因为这会立即将lerp应用到相机位置,因此下次调用它时,cameraPosition不再相同,因此你在不同的值之间进行前一次迭代
您可以做的是在开始播放时存储开始位置。
//call once when you want to move
private void move()
{
beginPosition = cameraPosition.cpy();
//make sure you copy so it's not a reference of cameraPosition.
lerpAlpha = 0;
}
public update()
{
//..
//Calculate lerp alpha
lerpAlpha += .1;
//..
camera.position.set(beginposition.cpy().lerp(target, lerpAlpha), 0);
//Copy again so it does not change.
//..
}
每当你使用向量获得奇怪的结果时,它是99%的时间,因为你没有复制Vector并直接将方法应用于它,因为它使用了一个链接模式(它改变了给定的对象并返回它)。