Xbox360模拟杆角度SDL

好的,直截了当地说,我正在使用SDL和openGL以及lua脚本编写C++游戏引擎,我需要获取模拟摇杆的角度,以确定2d枪的方向,使用以下lua代码:

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

logic:controllerAxisForce(int AXIS)返回

SDL_JoystickGetAxis(Joystick, AXIS);

问题是我的枪只会指向左边,而不是左右两边。

点赞
用户88888888
用户88888888

我真的很傻,我的问题是我只能拿到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度添加到角度中。

2014-03-13 23:17:58
用户869951
用户869951

你应该使用 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度等于π弧度),而不是3.1 :)

2014-03-14 02:26:24