【问题标题】:Animating translation between two fixed points (Libgdx)两个固定点之间的动画平移 (Libgdx)
【发布时间】: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


    【解决方案1】:

    假设您有起始坐标 X1,Y1 和结束坐标 X2,Y2。假设您有一些变量 p 包含传递路径的百分比。因此,如果p == 0 表示您在 X1、Y1,如果 p == 100 表示您在 X2、Y2,如果 0<p<100 表示您介于两者之间。在这种情况下,您可以根据 p 计算当前坐标,例如:

    X = X1 + ((X2 - X1)*p)/100;
    Y = Y1 + ((Y2 - Y1)*p)/100;
    

    因此,您不是将当前坐标基于前一个坐标,而是始终根据起点和终点以及通过路径的百分比进行计算。

    【讨论】:

      【解决方案2】:

      首先你需要一个Vector2 direction,给出两点之间的方向。
      这个Vector 应该被归一化,所以它的长度是1:

      Vector2 dir = new Vector2(x2-x1,y2-y1).nor();
      

      然后在render 方法中你需要移动对象,这意味着你需要改变它的位置。您有 speed(以距离/秒为单位)、标准化的 Vector,给出了方向以及自上次更新以来的时间。
      所以新的位置可以这样计算:

      position.x += speed * delta * dir.x;
      position.y += speed * delta * dir.y;
      

      现在你只需要将位置限制在目标位置,这样就不会走远了:

      boolean stop = false;
      if (position.x >= target.x) {
          position.x = target.x;
          stop = true;
      }
      if (position.y >= target.y) {
          position.y = target.y;
          stop = true;
      }
      

      现在是像素问题:
      不要使用像素!使用像素将使您的游戏分辨率依赖。
      请改用 Libgdx Viewport 和 Camera。 这允许您以自己的世界单位计算所有内容(例如 meters),Libgdx 将为您转换。

      【讨论】:

        【解决方案3】:

        我没有看到任何大错误,但我看到有些人像您正在使用 == 和 != 比较两个对象,但我建议您使用 a.equals(b) 和 !a.equals (b) 那样。其次,我发现您的 renderLock 坐标始终在 textureProperty.setRenderPoint(renderLocX, renderLocY) 中设置相同,您正在分配相同的返回值。也许你应该使用 newLocation 坐标。

        顺便说一句,感谢您的代码,我正在搜索我得到的东西

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-23
          相关资源
          最近更新 更多