# Rotation angle based on touch move

I want to rotate my stick based on the movement of the touch on the screen. From my calculation I did not able to find correct angle in degree. So please provide guidance, my code snippet for that are below.

if (pSceneTouchEvent.isActionMove()) {
pValueX = pSceneTouchEvent.getX();
pValueY = CAMERA_HEIGHT - pSceneTouchEvent.getY();
rotationAngle = (float) Math.atan2(pValueX, pValueY);

}


I'm assuming you're talking about a joystick; one which, when active, always points toward your finger.

It looks like your X and Y values are positions relative to one corner of the screen - so you're just measuring the angle from one corner of the screen to your finger. Of course it would never work! Well, unless your stick is on the corner of the screen.

What you need to do find the angle of the player's finger relative to your joystick's position.

In Vector mathematics: the vector from B to A = A - B

So in your case: the vector from Stick to Touch = Touch - Stick

// where joyX and joyY are the position of the joystick on the screen
touchX = pSceneTouchEvent.getX();
touchY = CAMERA_HEIGHT - pSceneTouchEvent.getY();
directionX = touchX - joyX
directionY = touchY - joyY
rotationAngle = (float) Math.atan2(directionY, directionX);

• Thank you for your response. But I don't able to find my require solution. the stick was moving based on drag event but it was not on the correct angle. I was converting the radians into degree for required outcome like this. stick.setRotation(MathUtils.radToDeg(rotationAngle)); May 30 '12 at 5:06
• Yes, I mentioned that part. Your atan2 and radToDeg lines are not the issue. The issue is the coordinates you're using in those lines. May 30 '12 at 6:02
• I find a solution for my problem. I have to swap the value of my atan2 function like this and this work for me. rotationAngle = (float) Math.atan2(directionY, directionX); May 30 '12 at 7:31
• @Siddharth You should post that as an answer then, and accept it as correct. May 30 '12 at 7:38
• @Siddharth Oh, so this was right except for directionY and directionX being the wrong way around? May 30 '12 at 7:49

The answer for the above question is as below

    if (pSceneTouchEvent.isActionMove()) {

pValueX = pSceneTouchEvent.getX();
pValueY = CAMERA_HEIGHT - pSceneTouchEvent.getY();

directionX = pValueX - stick.getX();
directionY = (CAMERA_HEIGHT - pValueY) - stick.getY();

rotationAngle = (float) Math.atan2(directionY, directionX);