CCNavMeshObstacle.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "navmesh/CCNavMeshObstacle.h"
  22. #if CC_USE_NAVMESH
  23. #include "navmesh/CCNavMesh.h"
  24. #include "2d/CCNode.h"
  25. #include "2d/CCScene.h"
  26. #include "recast/DetourTileCache/DetourTileCache.h"
  27. NS_CC_BEGIN
  28. NavMeshObstacle* NavMeshObstacle::create(float radius, float height)
  29. {
  30. auto ref = new (std::nothrow) NavMeshObstacle();
  31. if (ref && ref->initWith(radius, height))
  32. {
  33. ref->autorelease();
  34. return ref;
  35. }
  36. CC_SAFE_DELETE(ref);
  37. return nullptr;
  38. }
  39. const std::string& NavMeshObstacle::getNavMeshObstacleComponentName()
  40. {
  41. static std::string comName = "___NavMeshObstacleComponent___";
  42. return comName;
  43. }
  44. NavMeshObstacle::NavMeshObstacle()
  45. : _radius(0.0f)
  46. , _height(0.0f)
  47. , _syncFlag(NODE_AND_NODE)
  48. , _obstacleID(-1)
  49. , _tileCache(nullptr)
  50. {
  51. }
  52. cocos2d::NavMeshObstacle::~NavMeshObstacle()
  53. {
  54. }
  55. bool NavMeshObstacle::initWith(float radius, float height)
  56. {
  57. _radius = radius;
  58. _height = height;
  59. setName(getNavMeshObstacleComponentName());
  60. return true;
  61. }
  62. void cocos2d::NavMeshObstacle::removeFrom(dtTileCache* /*tileCache*/)
  63. {
  64. _tileCache->removeObstacle(_obstacleID);
  65. _tileCache = nullptr;
  66. _obstacleID = -1;
  67. }
  68. void cocos2d::NavMeshObstacle::addTo(dtTileCache *tileCache)
  69. {
  70. _tileCache = tileCache;
  71. Mat4 mat = _owner->getNodeToWorldTransform();
  72. _tileCache->addObstacle(&mat.m[12], _radius, _height, &_obstacleID);
  73. }
  74. void cocos2d::NavMeshObstacle::onExit()
  75. {
  76. if (_obstacleID == -1) return;
  77. Component::onExit();
  78. auto scene = _owner->getScene();
  79. if (scene && scene->getNavMesh()){
  80. scene->getNavMesh()->removeNavMeshObstacle(this);
  81. }
  82. }
  83. void cocos2d::NavMeshObstacle::onEnter()
  84. {
  85. if (_obstacleID != -1) return;
  86. Component::onEnter();
  87. auto scene = _owner->getScene();
  88. if (scene && scene->getNavMesh()){
  89. scene->getNavMesh()->addNavMeshObstacle(this);
  90. }
  91. }
  92. void cocos2d::NavMeshObstacle::postUpdate(float /*delta*/)
  93. {
  94. if ((_syncFlag & OBSTACLE_TO_NODE) != 0)
  95. syncToNode();
  96. }
  97. void cocos2d::NavMeshObstacle::preUpdate(float /*delta*/)
  98. {
  99. if ((_syncFlag & NODE_TO_OBSTACLE) != 0)
  100. syncToObstacle();
  101. }
  102. void NavMeshObstacle::syncToNode()
  103. {
  104. if (_tileCache){
  105. auto obstacle = _tileCache->getObstacleByRef(_obstacleID);
  106. if (obstacle){
  107. Vec3 localPos = Vec3(obstacle->pos[0], obstacle->pos[1], obstacle->pos[2]);
  108. if (_owner->getParent())
  109. _owner->getParent()->getWorldToNodeTransform().transformPoint(localPos, &localPos);
  110. _owner->setPosition3D(localPos);
  111. _radius = obstacle->radius;
  112. _height = obstacle->height;
  113. }
  114. }
  115. }
  116. void cocos2d::NavMeshObstacle::setRadius(float radius)
  117. {
  118. _radius = radius;
  119. }
  120. void cocos2d::NavMeshObstacle::setHeight(float height)
  121. {
  122. _height = height;
  123. }
  124. void NavMeshObstacle::syncToObstacle()
  125. {
  126. if (_tileCache){
  127. auto obstacle = _tileCache->getObstacleByRef(_obstacleID);
  128. if (obstacle){
  129. Vec3 worldPos = Vec3(obstacle->pos[0], obstacle->pos[1], obstacle->pos[2]);
  130. Mat4 mat = _owner->getNodeToWorldTransform();
  131. if ((mat.m[12] != obstacle->pos[0] && mat.m[13] != obstacle->pos[1] && mat.m[14] != obstacle->pos[2])
  132. || obstacle->radius != _radius
  133. || obstacle->height != _height){
  134. _tileCache->removeObstacle(_obstacleID);
  135. _tileCache->addObstacle(&mat.m[12], _radius, _height, &_obstacleID);
  136. }
  137. }
  138. }
  139. }
  140. NS_CC_END
  141. #endif //CC_USE_NAVMESH