好的,我正在用SDL开发一个游戏引擎,用lua脚本编写openGL,我需要得到模拟棒的角度,用这个lua代码来确定2d炮的方向。
playerLookArrow.rotation = math.atan(logic:controllerAxisForce(3)/-logic:controllerAxisForce(4))逻辑:控制器AXIS(int轴)返回
SDL_JoystickGetAxis(Joystick, AXIS);问题是我的枪只能指向左边而不是左边和右边。
发布于 2014-03-14 02:26:24
您应该使用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 deg = Pi ),Pi为3.141592,巴黎圣母院3.1 :)
发布于 2014-03-13 23:17:58
我真的很蠢,我的问题是,我只能得到一个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度的角度。
https://stackoverflow.com/questions/22392840
复制相似问题