4
\$\begingroup\$

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

}
\$\endgroup\$
5
\$\begingroup\$

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);
\$\endgroup\$
6
  • \$\begingroup\$ 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)); \$\endgroup\$
    – Siddharth
    May 30 '12 at 5:06
  • \$\begingroup\$ 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. \$\endgroup\$ May 30 '12 at 6:02
  • \$\begingroup\$ 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); \$\endgroup\$
    – Siddharth
    May 30 '12 at 7:31
  • \$\begingroup\$ @Siddharth You should post that as an answer then, and accept it as correct. \$\endgroup\$ May 30 '12 at 7:38
  • \$\begingroup\$ @Siddharth Oh, so this was right except for directionY and directionX being the wrong way around? \$\endgroup\$ May 30 '12 at 7:49
2
\$\begingroup\$

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

        stick.setRotation(MathUtils.radToDeg(rotationAngle));

    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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