【问题标题】:Xbox360 Analog stick angle SDLXbox360 模拟摇杆角度 SDL
【发布时间】:2014-03-13 23:02:25
【问题描述】:

好的,直截了当,我正在使用带有 lua 脚本的 SDL 和 openGL 在 C++ 中开发游戏引擎,我需要使用此 lua 代码获取模拟摇杆的角度以确定 2d 枪的方向

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

逻辑:controllerAxisForce(int AXIS) 返回

SDL_JoystickGetAxis(Joystick, AXIS);

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

【问题讨论】:

    标签: c++ controller lua joystick angle


    【解决方案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 :)

    【讨论】:

      【解决方案2】:

      我真的很愚蠢,我的问题是我只能得到一个介于 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 度的角度

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-01
        • 1970-01-01
        • 2017-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-28
        • 2018-12-21
        相关资源
        最近更新 更多