1

I am trying to find a method which will rotate an object to make it face a specified point, however the numbers I am getting don't make much sense and I don't think I understand what the math methods are returning.

Here is my code:

public void rotate(double x, double y) {
    rotateRight(Math.toDegrees(Math.atan2((x - getX()), (y - getY()))));
}

x and y are the specified point and getX() and getY() are the objects current point, so I am trying to find the number of degrees that object has to turn right to be facing the specified point.

Can anyone recommend a reliable way to do this?

4

4 回答 4

1

你需要一个矩阵变换才能正确地做到这一点。

于 2011-08-02T01:22:27.627 回答
1

你的命名有点混乱,所以我在这里做一些假设。您的“对象”位于原点 (0,0) 并且 getX() 和 getY() 返回对象当前面对的点,并且您希望对象面对一个新点。

从这里开始就是简单的三角和算术。首先是弄清楚你当前的角度:

current_angle=atan(getX()/getY())

你的新角度将是

new_angle=atan(newX/newY)

你需要旋转的角度自然是,

rotation_angle=new_angle - current_angle
于 2011-08-02T01:29:11.450 回答
1

这取决于您使用的单位。就个人而言,以弧度表示,我的片段中有这个功能。

public static double getAngleBetween(double x1, double y1, double x2, double y2)
{
      return(Math.atan2(y1-y2, x1-x2));
}

这导致任何两个抽象点之间的角度(以弧度为单位)。

于 2011-08-02T03:16:35.780 回答
0

返回的角度Math.atan2()是绝对的。为了将其变成相对旋转,您首先需要知道当前角度是多少,然后需要将两者相减。

于 2011-08-02T01:26:28.130 回答