简体   繁体   中英

Rotate sprite in libgdx by following the position of finger on android

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.

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM