Search code examples
c++controllerluajoystickangle

Xbox360 Analog stick angle SDL


Okay, straight to the point I'm working on a game engine in C++ using SDL and openGL with lua scripting and I need to get the angle of the analog stick to determine the direction of the 2d gun using this lua code

playerLookArrow.rotation = math.atan(logic:controllerAxisForce(3)/-logic:controllerAxisForce(4))

logic:controllerAxisForce(int AXIS) returns

SDL_JoystickGetAxis(Joystick, AXIS);

The problem is that my gun will only point to the left instead of left and right.


Solution

  • You should use math.atan2 which does this logic for you (http://www.lua.org/manual/5.1/manual.html#pdf-math.atan2):

    playerLookArrow.rotation = math.atan2(
        logic:controllerAxisForce(3), 
        -logic:controllerAxisForce(4))
    

    Note that the return value is in radians (180 deg = Pi rad) and that Pi is 3.141592, notre 3.1 :)