Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
    stick.setRotation((float) MathUtils.radToDeg(rotationAngle));

share|improve this question

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);
share|improve this answer
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)); – Siddharth 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. – doppelgreener 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); – Siddharth May 30 '12 at 7:31
@Siddharth You should post that as an answer then, and accept it as correct. – doppelgreener May 30 '12 at 7:38
thank you for you reply – Siddharth May 30 '12 at 7:47
up vote 2 down vote accepted

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);


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.