我正在libgdx中制作一个2D游戏,我想知道在屏幕上移动(在两个已知点之间转换)的标准方式是什么。两个固定点之间的动画翻译(Libgdx)
在一个按钮上按下,我试图为两个点之间的精灵对角线运动设置动画。我知道开始点和结束点的x和y坐标。但是我无法弄清楚在每次渲染调用之间确定纹理应该在哪里的数学。目前我的算法是这样的:
textureProperty = new TextureProperty();
firstPtX = textureProperty.currentLocationX
firstPtY = textureProperty.currentLocationY
nextPtX = textureProperty.getNextLocationX()
nextPtX = textureProperty.getNextLocationX()
diffX = nextPtX - firstPtX
diffY = nextPtY - firstPtY
deltaX = diffX/speedFactor // Arbitrary, controlls speed of the translation
deltaX = diffX/speedFactor
renderLocX = textureProperty.renderLocX()
renderLocY = textureProperty.renderLocY()
if(textureProperty.getFirstPoint() != textureProperty.getNextPoint()){
animating = true
}
if (animating) {
newLocationX = renderLocX + deltaX
newLocationY = renderLocY + deltaY
textureProperty.setRenderPoint(renderLocX, renderLocY)
}
if (textureProperty.getRenderPoint() == textureProperty.getNextPoint()){
animating = false
textureProperty.setFirstPoint(textureProperty.getNextPoint())
}
batch.draw(texture, textureProperty.renderLocX(), textureProperty.renderLocY())
但是,我可以预见这个代码的几个问题。 1)由于像素是整数,如果我将这个数字除以不均匀的东西,它会四舍五入。 2)由于数字1,它会错过目标。
另外,当我测试动画时,从point1移动的对象,错过了一个长镜头,这暗示着我的数学可能是错误的。
这里是我的意思图形:
实际结果:
当然这是一个标准的问题。我欢迎任何建议。