CSceneNodeAnimatorCameraFPS.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CSceneNodeAnimatorCameraFPS.h"
  5. #include "IVideoDriver.h"
  6. #include "ISceneManager.h"
  7. #include "Keycodes.h"
  8. #include "ICursorControl.h"
  9. #include "ICameraSceneNode.h"
  10. #include "ISceneNodeAnimatorCollisionResponse.h"
  11. namespace irr
  12. {
  13. namespace scene
  14. {
  15. //! constructor
  16. CSceneNodeAnimatorCameraFPS::CSceneNodeAnimatorCameraFPS(gui::ICursorControl* cursorControl,
  17. f32 rotateSpeed, f32 moveSpeed, f32 jumpSpeed,
  18. SKeyMap* keyMapArray, u32 keyMapSize, bool noVerticalMovement, bool invertY)
  19. : CursorControl(cursorControl), MaxVerticalAngle(88.0f),
  20. MoveSpeed(moveSpeed), RotateSpeed(rotateSpeed), JumpSpeed(jumpSpeed),
  21. MouseYDirection(invertY ? -1.0f : 1.0f),
  22. LastAnimationTime(0), firstUpdate(true), firstInput(true), NoVerticalMovement(noVerticalMovement)
  23. {
  24. #ifdef _DEBUG
  25. setDebugName("CCameraSceneNodeAnimatorFPS");
  26. #endif
  27. if (CursorControl)
  28. CursorControl->grab();
  29. allKeysUp();
  30. // create key map
  31. if (!keyMapArray || !keyMapSize)
  32. {
  33. // create default key map
  34. KeyMap.push_back(SKeyMap(EKA_MOVE_FORWARD, irr::KEY_UP));
  35. KeyMap.push_back(SKeyMap(EKA_MOVE_BACKWARD, irr::KEY_DOWN));
  36. KeyMap.push_back(SKeyMap(EKA_STRAFE_LEFT, irr::KEY_LEFT));
  37. KeyMap.push_back(SKeyMap(EKA_STRAFE_RIGHT, irr::KEY_RIGHT));
  38. KeyMap.push_back(SKeyMap(EKA_JUMP_UP, irr::KEY_KEY_J));
  39. }
  40. else
  41. {
  42. // create custom key map
  43. setKeyMap(keyMapArray, keyMapSize);
  44. }
  45. }
  46. //! destructor
  47. CSceneNodeAnimatorCameraFPS::~CSceneNodeAnimatorCameraFPS()
  48. {
  49. if (CursorControl)
  50. CursorControl->drop();
  51. }
  52. //! It is possible to send mouse and key events to the camera. Most cameras
  53. //! may ignore this input, but camera scene nodes which are created for
  54. //! example with scene::ISceneManager::addMayaCameraSceneNode or
  55. //! scene::ISceneManager::addFPSCameraSceneNode, may want to get this input
  56. //! for changing their position, look at target or whatever.
  57. bool CSceneNodeAnimatorCameraFPS::OnEvent(const SEvent& evt)
  58. {
  59. switch(evt.EventType)
  60. {
  61. case EET_KEY_INPUT_EVENT:
  62. for (u32 i=0; i<KeyMap.size(); ++i)
  63. {
  64. if (KeyMap[i].KeyCode == evt.KeyInput.Key)
  65. {
  66. CursorKeys[KeyMap[i].Action] = evt.KeyInput.PressedDown;
  67. return true;
  68. }
  69. }
  70. break;
  71. case EET_MOUSE_INPUT_EVENT:
  72. if (evt.MouseInput.Event == EMIE_MOUSE_MOVED)
  73. {
  74. CursorPos = CursorControl->getRelativePosition();
  75. return true;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. return false;
  82. }
  83. void CSceneNodeAnimatorCameraFPS::animateNode(ISceneNode* node, u32 timeMs)
  84. {
  85. if (!node || node->getType() != ESNT_CAMERA)
  86. return;
  87. ICameraSceneNode* camera = static_cast<ICameraSceneNode*>(node);
  88. if (firstUpdate)
  89. {
  90. camera->updateAbsolutePosition();
  91. if (CursorControl )
  92. {
  93. CursorControl->setPosition(0.5f, 0.5f);
  94. CursorPos = CenterCursor = CursorControl->getRelativePosition();
  95. }
  96. LastAnimationTime = timeMs;
  97. firstUpdate = false;
  98. }
  99. // If the camera isn't the active camera, and receiving input, then don't process it.
  100. if(!camera->isInputReceiverEnabled())
  101. {
  102. firstInput = true;
  103. return;
  104. }
  105. if ( firstInput )
  106. {
  107. allKeysUp();
  108. firstInput = false;
  109. }
  110. scene::ISceneManager * smgr = camera->getSceneManager();
  111. if(smgr && smgr->getActiveCamera() != camera)
  112. return;
  113. // get time
  114. f32 timeDiff = (f32) ( timeMs - LastAnimationTime );
  115. LastAnimationTime = timeMs;
  116. // update position
  117. core::vector3df pos = camera->getPosition();
  118. // Update rotation
  119. core::vector3df target = (camera->getTarget() - camera->getAbsolutePosition());
  120. core::vector3df relativeRotation = target.getHorizontalAngle();
  121. if (CursorControl)
  122. {
  123. if (CursorPos != CenterCursor)
  124. {
  125. relativeRotation.Y -= (0.5f - CursorPos.X) * RotateSpeed;
  126. relativeRotation.X -= (0.5f - CursorPos.Y) * RotateSpeed * MouseYDirection;
  127. // X < MaxVerticalAngle or X > 360-MaxVerticalAngle
  128. if (relativeRotation.X > MaxVerticalAngle*2 &&
  129. relativeRotation.X < 360.0f-MaxVerticalAngle)
  130. {
  131. relativeRotation.X = 360.0f-MaxVerticalAngle;
  132. }
  133. else
  134. if (relativeRotation.X > MaxVerticalAngle &&
  135. relativeRotation.X < 360.0f-MaxVerticalAngle)
  136. {
  137. relativeRotation.X = MaxVerticalAngle;
  138. }
  139. // Do the fix as normal, special case below
  140. // reset cursor position to the centre of the window.
  141. CursorControl->setPosition(0.5f, 0.5f);
  142. CenterCursor = CursorControl->getRelativePosition();
  143. // needed to avoid problems when the event receiver is disabled
  144. CursorPos = CenterCursor;
  145. }
  146. // Special case, mouse is whipped outside of window before it can update.
  147. video::IVideoDriver* driver = smgr->getVideoDriver();
  148. core::vector2d<u32> mousepos(u32(CursorControl->getPosition().X), u32(CursorControl->getPosition().Y));
  149. core::rect<u32> screenRect(0, 0, driver->getScreenSize().Width, driver->getScreenSize().Height);
  150. // Only if we are moving outside quickly.
  151. bool reset = !screenRect.isPointInside(mousepos);
  152. if(reset)
  153. {
  154. // Force a reset.
  155. CursorControl->setPosition(0.5f, 0.5f);
  156. CenterCursor = CursorControl->getRelativePosition();
  157. CursorPos = CenterCursor;
  158. }
  159. }
  160. // set target
  161. target.set(0,0, core::max_(1.f, pos.getLength()));
  162. core::vector3df movedir = target;
  163. core::matrix4 mat;
  164. mat.setRotationDegrees(core::vector3df(relativeRotation.X, relativeRotation.Y, 0));
  165. mat.transformVect(target);
  166. if (NoVerticalMovement)
  167. {
  168. mat.setRotationDegrees(core::vector3df(0, relativeRotation.Y, 0));
  169. mat.transformVect(movedir);
  170. }
  171. else
  172. {
  173. movedir = target;
  174. }
  175. movedir.normalize();
  176. if (CursorKeys[EKA_MOVE_FORWARD])
  177. pos += movedir * timeDiff * MoveSpeed;
  178. if (CursorKeys[EKA_MOVE_BACKWARD])
  179. pos -= movedir * timeDiff * MoveSpeed;
  180. // strafing
  181. core::vector3df strafevect = target;
  182. strafevect = strafevect.crossProduct(camera->getUpVector());
  183. if (NoVerticalMovement)
  184. strafevect.Y = 0.0f;
  185. strafevect.normalize();
  186. if (CursorKeys[EKA_STRAFE_LEFT])
  187. pos += strafevect * timeDiff * MoveSpeed;
  188. if (CursorKeys[EKA_STRAFE_RIGHT])
  189. pos -= strafevect * timeDiff * MoveSpeed;
  190. // For jumping, we find the collision response animator attached to our camera
  191. // and if it's not falling, we tell it to jump.
  192. if (CursorKeys[EKA_JUMP_UP])
  193. {
  194. const ISceneNodeAnimatorList& animators = camera->getAnimators();
  195. ISceneNodeAnimatorList::ConstIterator it = animators.begin();
  196. while(it != animators.end())
  197. {
  198. if(ESNAT_COLLISION_RESPONSE == (*it)->getType())
  199. {
  200. ISceneNodeAnimatorCollisionResponse * collisionResponse =
  201. static_cast<ISceneNodeAnimatorCollisionResponse *>(*it);
  202. if(!collisionResponse->isFalling())
  203. collisionResponse->jump(JumpSpeed);
  204. }
  205. it++;
  206. }
  207. }
  208. // write translation
  209. camera->setPosition(pos);
  210. // write right target
  211. target += pos;
  212. camera->setTarget(target);
  213. }
  214. void CSceneNodeAnimatorCameraFPS::allKeysUp()
  215. {
  216. for (u32 i=0; i<EKA_COUNT; ++i)
  217. CursorKeys[i] = false;
  218. }
  219. //! Sets the rotation speed
  220. void CSceneNodeAnimatorCameraFPS::setRotateSpeed(f32 speed)
  221. {
  222. RotateSpeed = speed;
  223. }
  224. //! Sets the movement speed
  225. void CSceneNodeAnimatorCameraFPS::setMoveSpeed(f32 speed)
  226. {
  227. MoveSpeed = speed;
  228. }
  229. //! Gets the rotation speed
  230. f32 CSceneNodeAnimatorCameraFPS::getRotateSpeed() const
  231. {
  232. return RotateSpeed;
  233. }
  234. // Gets the movement speed
  235. f32 CSceneNodeAnimatorCameraFPS::getMoveSpeed() const
  236. {
  237. return MoveSpeed;
  238. }
  239. //! Sets the keyboard mapping for this animator
  240. void CSceneNodeAnimatorCameraFPS::setKeyMap(SKeyMap *map, u32 count)
  241. {
  242. // clear the keymap
  243. KeyMap.clear();
  244. // add actions
  245. for (u32 i=0; i<count; ++i)
  246. {
  247. KeyMap.push_back(map[i]);
  248. }
  249. }
  250. void CSceneNodeAnimatorCameraFPS::setKeyMap(const core::array<SKeyMap>& keymap)
  251. {
  252. KeyMap=keymap;
  253. }
  254. const core::array<SKeyMap>& CSceneNodeAnimatorCameraFPS::getKeyMap() const
  255. {
  256. return KeyMap;
  257. }
  258. //! Sets whether vertical movement should be allowed.
  259. void CSceneNodeAnimatorCameraFPS::setVerticalMovement(bool allow)
  260. {
  261. NoVerticalMovement = !allow;
  262. }
  263. //! Sets whether the Y axis of the mouse should be inverted.
  264. void CSceneNodeAnimatorCameraFPS::setInvertMouse(bool invert)
  265. {
  266. if (invert)
  267. MouseYDirection = -1.0f;
  268. else
  269. MouseYDirection = 1.0f;
  270. }
  271. ISceneNodeAnimator* CSceneNodeAnimatorCameraFPS::createClone(ISceneNode* node, ISceneManager* newManager)
  272. {
  273. CSceneNodeAnimatorCameraFPS * newAnimator =
  274. new CSceneNodeAnimatorCameraFPS(CursorControl, RotateSpeed, MoveSpeed, JumpSpeed,
  275. 0, 0, NoVerticalMovement);
  276. newAnimator->setKeyMap(KeyMap);
  277. return newAnimator;
  278. }
  279. } // namespace scene
  280. } // namespace irr