Home › Forums › Programming › Converting X,Y Thumbstick Coordinates to Radians?
 This topic has 4 replies, 3 voices, and was last updated 7 years, 3 months ago by Anonymous.

AuthorPosts


October 4, 2012 at 11:30 am #8563AnonymousInactive
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] 
October 4, 2012 at 11:52 am #49032AnonymousInactive
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 JAVAesque 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

October 4, 2012 at 12:24 pm #49033AnonymousInactive
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 
October 4, 2012 at 2:24 pm #49037AnonymousInactive
I was trying to remember the trick, I knew there was something I was missing :)! Forgot negating y.

June 20, 2013 at 4:55 pm #50206AnonymousInactive
Amethyst,that is a brilliant solution


AuthorPosts
 The forum ‘Programming’ is closed to new topics and replies.