Home Forums Programming Converting X,Y Thumbstick Coordinates to Radians?

Viewing 4 reply threads
  • Author
    Posts
    • #8563
      Anonymous
      Inactive

      Hi there, Making a 2d game where I want to control the position of a ship using the right thumbstick of an Xbox 360 controller. I currently have this much, but the control seems a bit off. It only works correctly when I release to thumbstick (setting X & Y to 0,0 ) and then moving to the direction I want. Ideally I would like to be able to hold the thumbstick in a certain direction (say right) and swing it up and the ship would follow this. If anybody could help out on this it would be much appreciated :D[code:1:ac30c56199]double x = input->getGamepadThumbRX(0);
      double y = input->getGamepadThumbRY(0);
      double result = atan2 (y,x)*3.14;
      if()
      ship.setRadians(result);[/code:1:ac30c56199]

    • #49032
      Anonymous
      Inactive

      If I was doing this I’d construct a unit vector from the X and Y of the thumb stick positions and use that as a "heading normal" for my space ship.

      Anyway using your method:

      Not sure what language you are using there, assuming C#?

      Why are you multiplying the result of atan2 by 3.14? That doesn’t seem right, the results are already in radians.

      The range of atan2 is -PI to +PI. You first need to make sure you check for negative results and add 2PI to any negative results to give you a range of 0 to 2PI.

      I would recommend something like this (in JAVA-esque pseudo code):

      [code:1:41042be24b]
      //atan2( 0, 0 ) is undefined, in some languages returns 0, YMMV.
      if( (x == 0) && (y == 0) )
      return;

      double result = atan2( y, x );

      //Range of atan2 is -PI to PI. Add 2PI to negative results get a 0 to 2PI range
      if( result < 0 )
      result += (Math.PI * 2.0d)

      ship.setRadians( result );
      [/code:1:41042be24b]

      References: http://en.wikipedia.org/wiki/Atan2

    • #49033
      Anonymous
      Inactive

      Thanks for the help! This is being done in C++. I managed to get it seemingly working fine by inverting the Y result
      [code:1:272ff93818]double result = atan2 (-y,x);[/code:1:272ff93818]
      Now onto deadzones & movement! Thanks again :D

    • #49037
      Anonymous
      Inactive

      I was trying to remember the trick, I knew there was something I was missing :)! Forgot negating y.

    • #50206
      Anonymous
      Inactive

      Amethyst,that is a brilliant solution

Viewing 4 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.