【问题标题】:Move an ImageView in a circular path在圆形路径中移动 ImageView
【发布时间】:2017-02-21 12:24:31
【问题描述】:

我需要在圆形路径中移动 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
                     }

               }

我的问题是,目标坐标y在第一次运行后根据logcat变为NaN。 此外,根据 Log.d,每个用于设置目标坐标 y 的值都已正确设置。 附加信息:

  • TargetCoord y & x 是浮点数
  • h、k 和 r 是浮点数
  • delta x & y 是浮点数
  • 当前坐标 x 和 y 是浮点数

Delta x & y 在上面设置。用于设置它的值是速度和当前坐标。 speed 是 1 毫秒内移动的像素数

此外,所有流星.methods 都已经过测试,并且可以正常工作。我认为问题与目标坐标y的计算有关。

【问题讨论】:

  • 计算sqrt的参数值并在sqrt调用之前检查它是否为非负数
  • 它是否定的。不幸的是,我不明白为什么。
  • 可能的原因:1)你的逻辑错误2)由于浮动计算错误导致的非常小的负值
  • 太棒了,谢谢。我只是在输入 logcat 打印出来的值,我想我错过了阅读它 delta x 和 y 为零,这可能是问题所在。

标签: java android floating-point imageview geometry


【解决方案1】:

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

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

其中u 是角速度。这避免了需要采取sqrt

【讨论】:

  • 太棒了,谢谢。我会在周末玩它。实际上,我在获得恒定速度时遇到了问题,因为 x 速度和 y 速度不同,因为屏幕尺寸不同。
  • 我查看了您的方法,看起来不错,但我将保留我当前的方法,以便与我所有其他模式的移动方式保持一致。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多