CCPhysics3DComponent.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "physics3d/CCPhysics3D.h"
  21. #include "2d/CCNode.h"
  22. #include "2d/CCScene.h"
  23. #if CC_USE_3D_PHYSICS
  24. #if (CC_ENABLE_BULLET_INTEGRATION)
  25. NS_CC_BEGIN
  26. Physics3DComponent::~Physics3DComponent()
  27. {
  28. CC_SAFE_RELEASE(_physics3DObj);
  29. }
  30. std::string& Physics3DComponent::getPhysics3DComponentName()
  31. {
  32. static std::string comName = "___Physics3DComponent___";
  33. return comName;
  34. }
  35. bool Physics3DComponent::init()
  36. {
  37. setName(getPhysics3DComponentName());
  38. return Component::init();
  39. }
  40. Physics3DComponent* Physics3DComponent::create(Physics3DObject* physicsObj, const cocos2d::Vec3& translateInPhysics, const cocos2d::Quaternion& rotInPhsyics)
  41. {
  42. auto ret = new (std::nothrow) Physics3DComponent();
  43. if (ret && ret->init())
  44. {
  45. ret->setPhysics3DObject(physicsObj);
  46. ret->setTransformInPhysics(translateInPhysics, rotInPhsyics);
  47. ret->autorelease();
  48. return ret;
  49. }
  50. CC_SAFE_DELETE(ret);
  51. return nullptr;
  52. }
  53. void Physics3DComponent::setPhysics3DObject(Physics3DObject* physicsObj)
  54. {
  55. CC_SAFE_RETAIN(physicsObj);
  56. CC_SAFE_RELEASE(_physics3DObj);
  57. _physics3DObj = physicsObj;
  58. }
  59. Physics3DComponent::Physics3DComponent()
  60. : _physics3DObj(nullptr)
  61. , _syncFlag(Physics3DComponent::PhysicsSyncFlag::NODE_AND_NODE)
  62. {
  63. }
  64. void Physics3DComponent::setEnabled(bool b)
  65. {
  66. bool oldBool = _enabled;
  67. Component::setEnabled(b);
  68. if (_physics3DObj && oldBool != _enabled)
  69. {
  70. _enabled ? _physics3DObj->getPhysicsWorld()->addPhysics3DObject(_physics3DObj) : _physics3DObj->getPhysicsWorld()->removePhysics3DObject(_physics3DObj);
  71. }
  72. }
  73. void Physics3DComponent::addToPhysicsWorld(Physics3DWorld* world)
  74. {
  75. //add component to physics world
  76. if (_physics3DObj)
  77. {
  78. _physics3DObj->setPhysicsWorld(world);
  79. world->addPhysics3DObject(_physics3DObj);
  80. auto& components = world->_physicsComponents;
  81. auto it = std::find(components.begin(), components.end(), this);
  82. if (it == components.end())
  83. {
  84. auto parent = _owner->getParent();
  85. while (parent) {
  86. for (size_t i = 0; i < components.size(); i++) {
  87. if (parent == components[i]->getOwner())
  88. {
  89. //insert it here
  90. components.insert(components.begin() + i, this);
  91. return;
  92. }
  93. }
  94. parent = parent->getParent();
  95. }
  96. components.insert(components.begin(), this);
  97. }
  98. }
  99. }
  100. void Physics3DComponent::onEnter()
  101. {
  102. Component::onEnter();
  103. if (_physics3DObj->getPhysicsWorld() == nullptr && _owner)
  104. {
  105. auto scene = _owner->getScene();
  106. if (scene)
  107. addToPhysicsWorld(scene->getPhysics3DWorld());
  108. }
  109. }
  110. void Physics3DComponent::onExit()
  111. {
  112. Component::onExit();
  113. setEnabled(false);
  114. //remove component from physics world
  115. if (_physics3DObj)
  116. {
  117. auto& components = _physics3DObj->getPhysicsWorld()->_physicsComponents;
  118. auto it = std::find(components.begin(), components.end(), this);
  119. if (it != components.end())
  120. components.erase(it);
  121. }
  122. }
  123. void Physics3DComponent::preSimulate()
  124. {
  125. if (((int)_syncFlag & (int)Physics3DComponent::PhysicsSyncFlag::NODE_TO_PHYSICS) && _physics3DObj && _owner)
  126. {
  127. syncNodeToPhysics();
  128. }
  129. }
  130. void Physics3DComponent::postSimulate()
  131. {
  132. if (((int)_syncFlag & (int)Physics3DComponent::PhysicsSyncFlag::PHYSICS_TO_NODE) && _physics3DObj && _owner)
  133. {
  134. syncPhysicsToNode();
  135. }
  136. }
  137. void Physics3DComponent::setTransformInPhysics(const cocos2d::Vec3& translateInPhysics, const cocos2d::Quaternion& rotInPhsyics)
  138. {
  139. Mat4::createRotation(rotInPhsyics, &_transformInPhysics);
  140. _transformInPhysics.m[12] = translateInPhysics.x;
  141. _transformInPhysics.m[13] = translateInPhysics.y;
  142. _transformInPhysics.m[14] = translateInPhysics.z;
  143. _invTransformInPhysics = _transformInPhysics.getInversed();
  144. }
  145. void Physics3DComponent::setSyncFlag(PhysicsSyncFlag syncFlag)
  146. {
  147. _syncFlag = syncFlag;
  148. }
  149. void Physics3DComponent::syncPhysicsToNode()
  150. {
  151. if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY
  152. || _physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
  153. {
  154. Mat4 parentMat;
  155. if (_owner->getParent())
  156. parentMat = _owner->getParent()->getNodeToWorldTransform();
  157. auto mat = parentMat.getInversed() * _physics3DObj->getWorldTransform();
  158. //remove scale, no scale support for physics
  159. float oneOverLen = 1.f / sqrtf(mat.m[0] * mat.m[0] + mat.m[1] * mat.m[1] + mat.m[2] * mat.m[2]);
  160. mat.m[0] *= oneOverLen;
  161. mat.m[1] *= oneOverLen;
  162. mat.m[2] *= oneOverLen;
  163. oneOverLen = 1.f / sqrtf(mat.m[4] * mat.m[4] + mat.m[5] * mat.m[5] + mat.m[6] * mat.m[6]);
  164. mat.m[4] *= oneOverLen;
  165. mat.m[5] *= oneOverLen;
  166. mat.m[6] *= oneOverLen;
  167. oneOverLen = 1.f / sqrtf(mat.m[8] * mat.m[8] + mat.m[9] * mat.m[9] + mat.m[10] * mat.m[10]);
  168. mat.m[8] *= oneOverLen;
  169. mat.m[9] *= oneOverLen;
  170. mat.m[10] *= oneOverLen;
  171. mat *= _transformInPhysics;
  172. static Vec3 scale, translation;
  173. static Quaternion quat;
  174. mat.decompose(&scale, &quat, &translation);
  175. _owner->setPosition3D(translation);
  176. quat.normalize();
  177. _owner->setRotationQuat(quat);
  178. }
  179. }
  180. void Physics3DComponent::syncNodeToPhysics()
  181. {
  182. if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY
  183. || _physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
  184. {
  185. auto mat = _owner->getNodeToWorldTransform();
  186. //remove scale, no scale support for physics
  187. float oneOverLen = 1.f / sqrtf(mat.m[0] * mat.m[0] + mat.m[1] * mat.m[1] + mat.m[2] * mat.m[2]);
  188. mat.m[0] *= oneOverLen;
  189. mat.m[1] *= oneOverLen;
  190. mat.m[2] *= oneOverLen;
  191. oneOverLen = 1.f / sqrtf(mat.m[4] * mat.m[4] + mat.m[5] * mat.m[5] + mat.m[6] * mat.m[6]);
  192. mat.m[4] *= oneOverLen;
  193. mat.m[5] *= oneOverLen;
  194. mat.m[6] *= oneOverLen;
  195. oneOverLen = 1.f / sqrtf(mat.m[8] * mat.m[8] + mat.m[9] * mat.m[9] + mat.m[10] * mat.m[10]);
  196. mat.m[8] *= oneOverLen;
  197. mat.m[9] *= oneOverLen;
  198. mat.m[10] *= oneOverLen;
  199. mat *= _invTransformInPhysics;
  200. if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::RIGID_BODY)
  201. {
  202. auto body = static_cast<Physics3DRigidBody*>(_physics3DObj)->getRigidBody();
  203. auto motionState = body->getMotionState();
  204. motionState->setWorldTransform(convertMat4TobtTransform(mat));
  205. body->setMotionState(motionState);
  206. }
  207. else if (_physics3DObj->getObjType() == Physics3DObject::PhysicsObjType::COLLIDER)
  208. {
  209. auto object = static_cast<Physics3DCollider*>(_physics3DObj)->getGhostObject();
  210. object->setWorldTransform(convertMat4TobtTransform(mat));
  211. }
  212. }
  213. }
  214. NS_CC_END
  215. #endif // CC_ENABLE_BULLET_INTEGRATION
  216. #endif //CC_USE_3D_PHYSICS