2

The majority of sprites for simple goes move up, down, left, right at 90 degree intervals, often using different sprite sheets for each direction.

However I would like to create a sprite that follows the mouse/finger input (but not on the finger, slightly slower) and rotates on an axis using relevant degrees as appropriate, so it is always following/looking at the finger, i would also like to include 1 sprite sheet for the movement of the sprite. Is there a way of rotating the sprite to get the desired affect?

user3165683
  • 347
  • 1
  • 9
  • 28
  • possible duplicate of [Rotate Image Clockwise using LibGDX](http://stackoverflow.com/questions/9445035/rotate-image-clockwise-using-libgdx) – LionC Jul 10 '14 at 13:15

1 Answers1

2

If you are using SpriteBatch for rendering sprites, then you can just pass the rotation argument to the draw function:

draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

To achieve a rotation around the center of the sprite, originX and originY should be respectively half of the sprite's width and height.

michailgames
  • 303
  • 2
  • 7
  • how would u gain the rotation from isTouched to follow the players input? – user3165683 Jul 10 '14 at 13:38
  • You can get the touch position by `Input.getX()` and `Input.getY()`, then use some basic trigonometry to find the angle between sprite's and touch position, see http://stackoverflow.com/questions/9970281/java-calculating-the-angle-between-two-points-in-degrees – michailgames Jul 10 '14 at 13:45
  • the thing id like to rotate is a sprite and the sprite batch for the texture is not applicable to the sprite how do i fix this? – user3165683 Jul 11 '14 at 20:40
  • `Sprite` extends `TextureRegion`, so the above call should work (it's a SpriteBatch's method). Alternatively, you can use Sprite's methods: `sprite.setRotation(float degrees)`, then `sprite.draw(Batch spriteBatch)`. – michailgames Jul 14 '14 at 11:42