在圆形路径中移动ImageView



我需要在圆形路径中移动ImageView。

程序规格:

1)每个ImageView都有一个流星类,包含ImageView的

    当前坐标
  • 目标坐标
  • 和其他一些变量和函数

2)当设定目标坐标时,ImageView将以正确的速度向目标坐标移动

3)我使用的圆的路径方程是k+sqrt(-h^2+2*h*x+r^2-x^2)(上半部分),k-sqrt(-h^2+2*h*x+r^2-x^2)(下半部分)

下面是我用来计算圆的上半部分的目标坐标的代码。

              if(meteor.getXCoord() == meteor.getTargetCoordsX() && meteor.getXCoord() != meteor.getH() + meteor.getR()) {
                     if (meteor.getYCoord() == meteor.getTargetCoordsY()) {
                         /*
                             b+sqrt(-a^2+2*a*x+r^2-x^2), b-sqrt(-a^2+2*a*x+r^2-x^2)
                          */
                         meteor.setDeltaX(meteor.getSpeedX() + meteor.getXCoord());
                         meteor.setDeltaY(meteor.getSpeedY() + meteor.getYCoord());
                         meteor.setTargetCoordsX(meteor.getDeltaX());
                         //where target coordinate y is set *****
                         meteor.setTargetCoordY((meteor.getK() + (float) Math.sqrt(-1 * meteor.getH() * meteor.getH() + 2 * meteor.getH() * meteor.getDeltaX() + meteor.getR() * meteor.getR() - meteor.getDeltaX() * meteor.getDeltaX())));

                         //bottom half
                     }
               }

我的问题是,根据logcat第一次运行后,目标坐标y变成了NaN。而且,据Log说。D每一个用来设置目标坐标y的值都设置妥当。附加信息:

  • 目标码&x是浮动
  • h、k、r为浮动
  • x &y是浮点数
  • 当前坐标x &y是浮点数

x &Y如上所示。用来设置它的值是速度和当前坐标。速度是1毫秒内移动的像素数

还有,都是流星。方法已经经过测试,并且有效。我认为这个问题与目标坐标y的计算有关。

使用一点三角函数可能更容易:如果你以恒定速度围绕(x0, y0)旋转,坐标更新可以写成

xn = x0 + (x-x0)*cos(u) + (y-y0)*sin(u)
yn = y0 - (x-x0)*sin(u) + (x-x0)*cos(u)

,其中u为角速度。这避免了使用sqrt

最新更新