负角度值android

时间:2011-10-10 20:53:33

标签: android

我正在尝试为我的游戏应用程序包含射弹动作。当用户触摸imageview时,它会根据imageview的运动计算初始角度。但是在logcat中我看到这些角度值是负的。可以任何人解释为什么会如此。我想使用r来设置0到90度之间的角度值。

在下面找到我的计算角度的代码: -

float dy = motion.getY()-mImage.getPivotY();
float dx = motion.getX()-mImage.getPivotX();

double r = Math.atan2(dy, dx);

int angle = (int)Math.toDegrees(r);

其中motion的类型为MotionEvent。

1 个答案:

答案 0 :(得分:1)

垂直屏幕坐标(例如motion.getY()mImage.getPivotY()返回的坐标定义了原点,因为屏幕顶部从上到下正向移动。

标准数学惯例与此相反。你需要将这个因素考虑在你的等式中。在这种情况下,使用-dy

通过解释你原来的等式:

  

float dy = motion.getY() - mImage.getPivotY();

并将屏幕坐标调整为规范坐标

  

float dy2 =(Screen.getHeight() - motion.getY()) - (Screen.getHeight()    - mImage.getPivotY());

给出了:

  

float dy2 = mImage.getPivotY() - motion.getY()

因此:

  

float dy2 = -dy

这就是你看到负值的原因。

相关问题