camera.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. -- Move speed (m/s)
  2. MOVE_SPEED_NORMAL = 1
  3. MOVE_SPEED_FAST = 5
  4. -- Move flags
  5. MOVE_FORWARD = 1
  6. MOVE_BACKWARD = 2
  7. MOVE_RIGHT = 3
  8. MOVE_LEFT = 4
  9. _useScriptCamera = false
  10. _forwardSpeed = 0
  11. _sideSpeed = 0
  12. _touch = Vector2.new()
  13. _delta = Vector2.new()
  14. _move = Vector2.new()
  15. _moveFlags = { false, false, false, false }
  16. _yaw = 0
  17. _pitch = 0
  18. _moveFast = false
  19. function camera_setActive(flag)
  20. _useScriptCamera = flag
  21. if _useScriptCamera then
  22. Game.getInstance():setMultiTouch(true)
  23. _scene = Scene.getScene()
  24. _cameraNode = _scene:getActiveCamera():getNode()
  25. -- Set initial camera angles
  26. local eulers = camera_quatToEuler(_cameraNode:getRotation())
  27. _yaw = eulers:y()
  28. _pitch = eulers:x()
  29. else
  30. -- Release scene and camera
  31. _scene = nil
  32. _cameraNode = nil
  33. end
  34. end
  35. function camera_setSpeed(normal, fast)
  36. MOVE_SPEED_NORMAL = normal
  37. MOVE_SPEED_FAST = fast
  38. end
  39. function camera_update(elapsedTime)
  40. if not _useScriptCamera then
  41. return
  42. end
  43. if USE_PHYSICS_CHARACTER then
  44. local char = _scene:findNode("camera"):getCollisionObject():asCharacter()
  45. local speed = MOVE_SPEED_NORMAL
  46. if _moveFast then
  47. speed = MOVE_SPEED_FAST
  48. end
  49. -- Forward motion
  50. if _moveFlags[MOVE_FORWARD] then
  51. char:setForwardVelocity(speed)
  52. elseif _moveFlags[MOVE_BACKWARD] then
  53. char:setForwardVelocity(-speed)
  54. else
  55. char:setForwardVelocity(0)
  56. end
  57. -- Strafing
  58. if _moveFlags[MOVE_LEFT] then
  59. char:setRightVelocity(-speed)
  60. elseif _moveFlags[MOVE_RIGHT] then
  61. char:setRightVelocity(speed)
  62. else
  63. char:setRightVelocity(0)
  64. end
  65. else
  66. -- Manual camera movement
  67. local secs = elapsedTime / 1000.0
  68. _move:set(0,0)
  69. -- Forward motion
  70. if _moveFlags[MOVE_FORWARD] then
  71. _move:y(1)
  72. elseif _moveFlags[MOVE_BACKWARD] then
  73. _move:y(-1)
  74. end
  75. -- Strafing
  76. if _moveFlags[MOVE_LEFT] then
  77. _move:x(-1)
  78. elseif _moveFlags[MOVE_RIGHT] then
  79. _move:x(1)
  80. end
  81. if not _move:isZero() then
  82. local speed = MOVE_SPEED_NORMAL
  83. if _moveFast then
  84. speed = MOVE_SPEED_FAST
  85. end
  86. _move:normalize():scale(secs * speed)
  87. camera_moveForward(_move:y());
  88. camera_moveRight(_move:x());
  89. end
  90. end
  91. end
  92. function camera_keyEvent(evt, key)
  93. if not _useScriptCamera then
  94. return
  95. end
  96. if evt == Keyboard.KEY_PRESS then
  97. if key == Keyboard.KEY_W or key == Keyboard.KEY_CAPITAL_W then
  98. _moveFlags[MOVE_FORWARD] = true
  99. elseif key == Keyboard.KEY_S or key == Keyboard.KEY_CAPITAL_S then
  100. _moveFlags[MOVE_BACKWARD] = true
  101. elseif key == Keyboard.KEY_A or key == Keyboard.KEY_CAPITAL_A then
  102. _moveFlags[MOVE_LEFT] = true
  103. elseif key == Keyboard.KEY_D or key == Keyboard.KEY_CAPITAL_D then
  104. _moveFlags[MOVE_RIGHT] = true
  105. elseif key == Keyboard.KEY_SHIFT then
  106. _moveFast = true
  107. end
  108. elseif evt == Keyboard.KEY_RELEASE then
  109. if key == Keyboard.KEY_W or key == Keyboard.KEY_CAPITAL_W then
  110. _moveFlags[MOVE_FORWARD] = false
  111. elseif key == Keyboard.KEY_S or key == Keyboard.KEY_CAPITAL_S then
  112. _moveFlags[MOVE_BACKWARD] = false
  113. elseif key == Keyboard.KEY_A or key == Keyboard.KEY_CAPITAL_A then
  114. _moveFlags[MOVE_LEFT] = false
  115. elseif key == Keyboard.KEY_D or key == Keyboard.KEY_CAPITAL_D then
  116. _moveFlags[MOVE_RIGHT] = false
  117. elseif key == Keyboard.KEY_SHIFT then
  118. _moveFast = false
  119. end
  120. end
  121. end
  122. function camera_touchEvent(evt, x, y, contactIndex)
  123. if not _useScriptCamera then
  124. return
  125. end
  126. if evt == Touch.TOUCH_PRESS then
  127. if contactIndex == 0 then
  128. _touch:set(x, y)
  129. elseif contactIndex == 1 then
  130. _moveFlags[MOVE_FORWARD] = true
  131. elseif contactIndex == 2 then
  132. _moveFast = true
  133. end
  134. elseif evt == Touch.TOUCH_RELEASE then
  135. if contactIndex == 1 then
  136. _moveFlags[MOVE_FORWARD] = false
  137. elseif contactIndex == 2 then
  138. _moveFast = false
  139. end
  140. elseif evt == Touch.TOUCH_MOVE then
  141. if contactIndex == 0 then
  142. _delta:set(x - _touch:x(), y - _touch:y())
  143. _touch:set(x, y)
  144. _pitch = _pitch - math.rad(_delta:y() * 0.5)
  145. _yaw = _yaw - math.rad(_delta:x() * 0.5)
  146. _cameraNode:setRotation(Quaternion.identity())
  147. _cameraNode:rotateY(_yaw)
  148. _cameraNode:rotateX(_pitch)
  149. end
  150. end
  151. end
  152. function camera_moveForward(by)
  153. local v = _cameraNode:getForwardVector()
  154. v:normalize():scale(by)
  155. _cameraNode:translate(v)
  156. end
  157. function camera_moveRight(by)
  158. local v = _cameraNode:getRightVector()
  159. v:normalize():scale(by)
  160. _cameraNode:translate(v)
  161. end
  162. function camera_quatToEuler(quat)
  163. local qx = quat:x()
  164. local qy = quat:y()
  165. local qz = quat:z()
  166. local qw = quat:w()
  167. local qx2 = qx * qx
  168. local qy2 = qy * qy
  169. local qz2 = qz * qz
  170. local qw2 = qw * qw
  171. local rotx = 0
  172. local roty = 0
  173. local rotz = 0
  174. if (qx*qy + qz*qw) == 0.5 then
  175. rotx = 0
  176. roty = 2 * math.atan2(qx, qw)
  177. elseif (qx*qy + qz*qw) == -0.5 then
  178. rotx = 0
  179. roty = -2 * math.atan2(qx, qw)
  180. else
  181. rotx = math.atan2(2*qx*qw-2*qy*qz , 1 - 2*qx2 - 2*qz2)
  182. roty = math.atan2(2*qy*qw-2*qx*qz , 1 - 2*qy2 - 2*qz2)
  183. end
  184. rotz = math.asin(2*qx*qy + 2*qz*qw)
  185. return Vector3.new(rotx, roty, rotz)
  186. end