javaandroidlibgdxrotationtouchpad

Rotate player using libgdx touchpad to face moving direction


i have this Problem now for over 2 days. Constantly tweaking. I just cant get it done.

I have a Player texture ( Player is facing the left on it ) which i want to rotate using the touchpad. So the player will be facing his running direction.

So far i have this :

double facerotation = Math.atan2(touchpad.getKnobPercentY(), touchpad.getKnobPercentX());

spriteBatch.draw(runningFrame, player.getPosition().x, player.getPosition().y, Player.getSize() / 2, Player.getSize() / 2, Player.getSize(), Player.getSize(), 1, 1, facerotation * 100, false);

But with "roation*100" he spins like 2 times around and without he barely rotates. I even tried switching the X and Y values for the atan2 function above. But i never got him rotate only in the direction i am Moving. I also tried the atan function, also with swapping the X and Y values.

Please help me. I tried thousands of ways, Different calculations and things i saw on google. Nothing brought me the desired effect.


Solution

  • Just use a Vector2. Use it to store your knob percent y and x. Then you can get the rotatation in degrees with vector2.angle().

    Vector2 v = new Vector2(touchpad.getKnobPercentX(), touchpad.getKnobPercentY());
    float angle = v.angle();
    runningFrame.setRotation(angle);