3

I'm having problem trying to point my "arrow_sprite" to the touch position.

The result I want is that the arrow(the sprite that I want to rotate)will point to the touch position.

How can I acheive this?

4

3 回答 3

3

You need to calculate the vector between the touch position and the sprite position. To do so you have to unproject the touch position received through your InputProcessor.

public class MyInputProcessor implements InputProcessor {
   ...

   @Override
   public boolean touchDown (int x, int y, int pointer, int button) {
      Vector3 touchDown = new Vector3(x, y, 0);
      camera.unproject(touchDown);
      Vector3 spritePosition = new Vector3(yourSpriteX, yourSpriteY, 0);
      spritePosition.sub(touchDown);
      return false;
   }

   ...
}

Now we have a vector pointing from your sprite to the touch position. From there you just need to calculate the rotation angle. One way would be to use Vector2.angle(), as such creating a Vector2 from the spritePosition Vector3.

于 2012-07-24T11:14:04.710 回答
3

如果你想绘制 2D,那么你必须将 SpriteBatch 设置为 Camera.combined,

像这样:yourBatch.setProjectionMatrix(yourCamera.combined);

然后你必须像“批量”所说的那样取消投影相机。

之后,您必须选择是要使用矢量还是二维坐标。角度计算的基本相同结束:

首先定义一个浮点度,然后在 touchDown 方法中使它像这样:

degrees = (float) ((Math.atan2 (touchPoint.x - arrowPosition.x, -(touchPoint.y - arrowPosition.y))*180.0d/Math.PI)+90.0f);

假设您对输入和箭头都使用 Vector3 或 2。

然后在渲染箭头精灵时:

Batch.draw(Arrow, x, y, originX, originY, width, height, scaleX, scaleY, degrees);

希望这会有所帮助。

于 2012-08-04T00:15:12.873 回答
0

就像 Psilopat 说的那样,我们通过以下方式定义度数:

degrees = (float) ((Math.atan2 (touchPoint.x - arrowPosition.x, -(touchPoint.y - arrowPosition.y))*180.0d/Math.PI)+90.0f);

但如果旋转看起来不对,请将行尾的“+90.0f”更改为其他内容。我的箭头指向上方,所以我将其更改为“-180.0f”

于 2013-04-17T12:16:36.273 回答