PDA

View Full Version : Convert Stick's values to rotation?



Magnus2552
08-05-2014, 02:53 PM
Hello, I want to convert the Stick's horizontal and vertical state values to a rotation (0-360) value.
I need this for my skillgame, where the player should set a direction using the right stick (the direction is set via OPE, so I need a rotation value). If I only would use the Stick's own events, then there only would be 8 directions that you can point to. But I want smooth rotation, so I need the horizontal and vertical values of the stick.
I know, that I need the trigonometric functions (http://en.wikipedia.org/wiki/Trigonometric_functions) for this (arcsin for horizontal and arcsin for vertical), but there are some problems:

First of, the arcsin or arccos return only correct values in specific rotation areas, for example arcsin(0) is equal to 0 AND to 180, but arcsin will always return 0, also if 180 would be correct. (There are multiple x values, that belong to one y value)
Maybe some math genius here has a equation that somehow combines them? :)

The bigger problem: The sticks return values from -3 to 3, depending on how far the stick was moved. So, if you just would divide the value by 3 and then pass it on to the arcsin or arccos, it will only work if the stick is pressed at maximum to the edge. But whenever the stick is somewhere between 0 and 3, the arcsin or arccos will return false values, because sin(degree)^2 +cos(degree)^2 was not equal to 1.
I hope you understand, what I mean and hopefully someone has a solution to this? :)


Thanks for everyone, trying to help me; I hope that I did not describe my problem too complicated :confused:
Currently, its only glitching around and I hope that someone has a solution, because only 8 directions would be very lame. :)

dasraizer
08-05-2014, 04:04 PM
hi
method 1:- try using arctan2, pass the horizontal value into one operand and the vertical into the other, now depending on if say if vertical is negative, add 90 to the results of the arctan2 to get the right quadrant for the circle, there are a few generic filter logic checks you need to figure out for working out the correct value to add to the arctan2 fuinction.

method2:
create a two dimensional vector out of the horizontal and vertical values and then normalize the vector (one of the vector operators has this function,scalar perhaps), this will instantly convert your vector to the sin/cos coordinates that arccos and arcsin use to get back to degrees, again you will need filter the original stick values to add/subtract the correct circle segment to the final angle.

Magnus2552
08-05-2014, 04:41 PM
Thank you for the quick help.
I was not familiar with the arctan2 Operator, but it sounds (relatively) simple, so I must try out this one.
The second method seems to be very reliable too, but the first one sounds simpler :D