Marke - 4 months ago 40

Android Question

I'm trying to rotate a sprite based upon where my finger is placed on the touch screen.

I have an arrow and i want the arrow to be pointing at my finger at all times as i'm drag it around over the screen.

I could easily set the sprite to any rotation using sprite.setRotation(angle)

How should i go about this?

Much appreciated if you can point me in the right direction.

Answer

`(ax, ay)`

is coordinates of the center of the arrow, and `(fx, fy)`

is the coordinates of the finger, and `a`

is the angle, here is some pseudocode:

```
dx = fx - ax
dy = fy - ay
if (dx == 0) {
a = 90
return
}
if (dy == 0) {
a = 0
return
}
//tan(a) == abs(dy) / abs(dx) therefore
a = arctan(dy / dx)
if (dx > 0 && dy > 0) {
// do nothing, a is correct
} else if (dx > 0 && dy < 0) {
a = 360 - a
} else if (dx < 0 && dy > 0) {
a = 180 - a
} else {
a = 180 + a
}
```

I didn't implement and test it yet, I'll do it later if there will be need