###

» Sun Oct 05, 2014 7:38 am

Solved it. Create the following events:

System: Every tick

----Gamepad: Gamepad 0 Right Analog X axis > 0

--------Gamepad: Gamepad 0 Right Analog Y Axis < 0 : Player: Set Angle to 360+atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2)))

--------System: Else : Player: Set angle to atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2)))

----Gamepad: Gamepad 0 Right Analog X Axis < 0

--------Gamepad: Gamepad 0 Right Analog Y Axis Not Equal To 0 : Player: 180+atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2)))

Note that the parameters for Gamepad.Axis(A,B) may be different for you. These were for an XBOX One controller who's index was 0 (first controller connected), making the A value 0 (it would be 1 if it were the second of two controllers connected). The value B is 2 for the right stick's X-Axis and 3 for the right stick's Y axis. My controller is also set up so that the analog stick Y-Axis is a negative value when pressed up and positive when down. Your controller may be different. To check, I went to the controller settings in windows and could see the Y rotation bar increase as the stick was pushed down, and decreased as the stick was pressed up.

For those interested in what this is actually doing:

The atan((Gamepad.Axis(0,3))/(Gamepad.Axis(0,2))) expression performs an arctangent function on a Y and an X variable to get a degree value as follows: arctangent (Y/X), where Y is the position of the right analog stick along the Y axis and X is the position of the stick along the X axis. Because of how the arctangent function works, it is necessary to set this up into different events because different combinations of + or - stick positions along each axis can return the same value.

Adding the result from the either 360 or 180 is necessary to get the angle we want since the arctangent function only returns a value between -90 and + 90 degrees. In the first event we add the arctangent result to 360 because performing the function on any of the possible combinations of X and Y values for this event will always equal between -90 and 0 degrees. For example, if the stick position is 50,-50, diagonally right and up, the function arctangent (-50/50) gives us -45 degrees. Adding 360 to -45 gives us 315 degrees, which makes the player face up and right, just like the stick.

You can do the math for the other events if you want to.

Edit: Corrected a mistake in the events list. Turns out I accidentally set one of my conditions to check the Left Analog Y-Axis instead of the Right Analog Y-Axis. I was able to merge two events into one, reducing the number of events required.