Get cursor position in LIBGDX

前端 未结 2 938
说谎
说谎 2021-02-05 09:50

How to get cursor position in libgdx and apply it to sprite? Like this? \"enter

I want my

2条回答
  •  爱一瞬间的悲伤
    2021-02-05 09:56

    Get cursor position

    If you're polling for input, use Gdx.input.getX() and Gdx.input.getY() to get the current mouse x and y coordinates. (The doc says its only relevant for touch, but the code looks like it reports raw mouse values regardless of button state.)

    If you're using an InputProcessor you can use one of:

    • touchMoved callback (on older libGDX versions)
    • mouseMoved callback (on newer libGDX versions)

    to receive input events from a mouse with no buttons pressed.

    Apply position to sprite

    Update a Vector2 that points from the current sprite position to the cursor position. This can be your Sprite's heading. You'll want to rotate the sprite's heading to match this vector.

    Use Vector2.angle() to compute the angle of this vector, and set your sprite's rotation to this. (This is relative to the positive X axis,so you may need to add a constant if you want it relative to the Y axis.)

提交回复
热议问题