Xbox360模拟摇杆角度SDL

时间:2014-03-13 23:02:26

标签: c++ controller lua joystick angle

好吧,直截了当地说我正在使用SDL和使用lua脚本的OpenGL在C ++游戏引擎上工作,我需要使用这个lua来获取模拟棒的角度以确定2d枪的方向代码

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

logic:controllerAxisForce(int AXIS)返回

SDL_JoystickGetAxis(Joystick, AXIS);

问题是我的枪只会指向左侧而不是左侧和右侧。

2 个答案:

答案 0 :(得分:1)

你应该使用math.atan2为你做这个逻辑(http://www.lua.org/manual/5.1/manual.html#pdf-math.atan2):

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

请注意,返回值以弧度为单位(180度= Pi rad),Pi为3.141592,而不是3.1:)

答案 1 :(得分:0)

我真的很蠢,我的问题是我只能在角度0到3.1之间得到一个数字,所以我最终做的是

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

所以如果左模拟杆位于右侧,则只需将角度增加3.1或180度