【发布时间】:2015-12-29 08:47:16
【问题描述】:
我正在 libgdx 中制作 2d 游戏,我想知道在屏幕上移动(在两个已知点之间平移)的标准方式是什么。
按下按钮时,我正在尝试为精灵在两点之间的对角线运动设置动画。我知道起点和终点的 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,它会错过目标。
另外,当我测试动画时,从点 1 开始移动的对象远未命中,这表明我的数学可能有问题。
这就是我在图形上的意思:
实际结果:
这当然是一个标准问题。我欢迎任何建议。
【问题讨论】:
标签: java android animation libgdx