0

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.

2 Answers2

0

I'm really stupid, my problem was that I can only get a number between 0 and 3.1 for the angle so what I ended up doing is

if logic:controllerAxisForce(4) <= 0 then
        playerLookArrow.rotation = math.atan(logic:controllerAxisForce(3)/-logic:controllerAxisForce(4))
    elseif logic:controllerAxisForce(4) > 0 then
        playerLookArrow.rotation = math.atan(logic:controllerAxisForce(3)/-logic:controllerAxisForce(4))+3.1
    end

so if the left analog stick is to the right it just adds 3.1 or 180 degrees to the angle

0

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 :)

Oliver
  • 27,510
  • 9
  • 72
  • 103