BsCJoint.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCJoint.h"
  4. #include "Components/BsCRigidbody.h"
  5. #include "Scene/BsSceneObject.h"
  6. #include "Physics/BsPhysics.h"
  7. #include "RTTI/BsCJointRTTI.h"
  8. using namespace std::placeholders;
  9. namespace bs
  10. {
  11. CJoint::CJoint(JOINT_DESC& desc)
  12. :mDesc(desc)
  13. {
  14. mPositions[0] = Vector3::ZERO;
  15. mPositions[1] = Vector3::ZERO;
  16. mRotations[0] = Quaternion::IDENTITY;
  17. mRotations[1] = Quaternion::IDENTITY;
  18. }
  19. CJoint::CJoint(const HSceneObject& parent, JOINT_DESC& desc)
  20. : Component(parent), mDesc(desc)
  21. {
  22. setName("Joint");
  23. mPositions[0] = Vector3::ZERO;
  24. mPositions[1] = Vector3::ZERO;
  25. mRotations[0] = Quaternion::IDENTITY;
  26. mRotations[1] = Quaternion::IDENTITY;
  27. mNotifyFlags = (TransformChangedFlags)(TCF_Parent | TCF_Transform);
  28. }
  29. HRigidbody CJoint::getBody(JointBody body) const
  30. {
  31. return mBodies[(int)body];
  32. }
  33. void CJoint::setBody(JointBody body, const HRigidbody& value)
  34. {
  35. if (mBodies[(int)body] == value)
  36. return;
  37. if (mBodies[(int)body] != nullptr)
  38. mBodies[(int)body]->_setJoint(HJoint());
  39. mBodies[(int)body] = value;
  40. if (value != nullptr)
  41. mBodies[(int)body]->_setJoint(mThisHandle);
  42. // If joint already exists, destroy it if we removed all bodies, otherwise update its transform
  43. if(mInternal != nullptr)
  44. {
  45. if (!isBodyValid(mBodies[0]) && !isBodyValid(mBodies[1]))
  46. destroyInternal();
  47. else
  48. {
  49. Rigidbody* rigidbody = nullptr;
  50. if (value != nullptr)
  51. rigidbody = value->_getInternal();
  52. mInternal->setBody(body, rigidbody);
  53. updateTransform(body);
  54. }
  55. }
  56. else // If joint doesn't exist, check if we can create it
  57. {
  58. // Must be an active component and at least one of the bodies must be non-null
  59. if (SO()->getActive() && (isBodyValid(mBodies[0]) || isBodyValid(mBodies[1])))
  60. {
  61. restoreInternal();
  62. }
  63. }
  64. }
  65. Vector3 CJoint::getPosition(JointBody body) const
  66. {
  67. return mPositions[(int)body];
  68. }
  69. Quaternion CJoint::getRotation(JointBody body) const
  70. {
  71. return mRotations[(int)body];
  72. }
  73. void CJoint::setTransform(JointBody body, const Vector3& position, const Quaternion& rotation)
  74. {
  75. if (mPositions[(int)body] == position && mRotations[(int)body] == rotation)
  76. return;
  77. mPositions[(int)body] = position;
  78. mRotations[(int)body] = rotation;
  79. if (mInternal != nullptr)
  80. updateTransform(body);
  81. }
  82. float CJoint::getBreakForce() const
  83. {
  84. return mDesc.breakForce;
  85. }
  86. void CJoint::setBreakForce(float force)
  87. {
  88. if (mDesc.breakForce == force)
  89. return;
  90. mDesc.breakForce = force;
  91. if (mInternal != nullptr)
  92. mInternal->setBreakForce(force);
  93. }
  94. float CJoint::getBreakTorque() const
  95. {
  96. return mDesc.breakTorque;
  97. }
  98. void CJoint::setBreakTorque(float torque)
  99. {
  100. if (mDesc.breakTorque == torque)
  101. return;
  102. mDesc.breakTorque = torque;
  103. if (mInternal != nullptr)
  104. mInternal->setBreakTorque(torque);
  105. }
  106. bool CJoint::getEnableCollision() const
  107. {
  108. return mDesc.enableCollision;
  109. }
  110. void CJoint::setEnableCollision(bool value)
  111. {
  112. if (mDesc.enableCollision == value)
  113. return;
  114. mDesc.enableCollision = value;
  115. if (mInternal != nullptr)
  116. mInternal->setEnableCollision(value);
  117. }
  118. void CJoint::onInitialized()
  119. {
  120. }
  121. void CJoint::onDestroyed()
  122. {
  123. if (mBodies[0] != nullptr)
  124. mBodies[0]->_setJoint(HJoint());
  125. if (mBodies[1] != nullptr)
  126. mBodies[1]->_setJoint(HJoint());
  127. if(mInternal != nullptr)
  128. destroyInternal();
  129. }
  130. void CJoint::onDisabled()
  131. {
  132. if (mInternal != nullptr)
  133. destroyInternal();
  134. }
  135. void CJoint::onEnabled()
  136. {
  137. if(isBodyValid(mBodies[0]) || isBodyValid(mBodies[1]))
  138. restoreInternal();
  139. }
  140. void CJoint::onTransformChanged(TransformChangedFlags flags)
  141. {
  142. if (mInternal == nullptr)
  143. return;
  144. // We're ignoring this during physics update because it would cause problems if the joint itself was moved by physics
  145. // Note: This isn't particularily correct because if the joint is being moved by physics but the rigidbodies
  146. // themselves are not parented to the joint, the transform will need updating. However I'm leaving it up to the
  147. // user to ensure rigidbodies are always parented to the joint in such a case (It's an unlikely situation that
  148. // I can't think of an use for - joint transform will almost always be set as an initialization step and not a
  149. // physics response).
  150. if (gPhysics()._isUpdateInProgress())
  151. return;
  152. updateTransform(JointBody::Target);
  153. updateTransform(JointBody::Anchor);
  154. }
  155. void CJoint::restoreInternal()
  156. {
  157. if (mBodies[0] != nullptr)
  158. mDesc.bodies[0].body = mBodies[0]->_getInternal();
  159. else
  160. mDesc.bodies[0].body = nullptr;
  161. if (mBodies[1] != nullptr)
  162. mDesc.bodies[1].body = mBodies[1]->_getInternal();
  163. else
  164. mDesc.bodies[1].body = nullptr;
  165. getLocalTransform(JointBody::Target, mDesc.bodies[0].position, mDesc.bodies[0].rotation);
  166. getLocalTransform(JointBody::Anchor, mDesc.bodies[1].position, mDesc.bodies[1].rotation);
  167. mInternal = createInternal();
  168. mInternal->onJointBreak.connect(std::bind(&CJoint::triggerOnJointBroken, this));
  169. }
  170. void CJoint::destroyInternal()
  171. {
  172. // This should release the last reference and destroy the internal joint
  173. mInternal->_setOwner(PhysicsOwnerType::None, nullptr);
  174. mInternal = nullptr;
  175. }
  176. void CJoint::notifyRigidbodyMoved(const HRigidbody& body)
  177. {
  178. if (mInternal == nullptr)
  179. return;
  180. // If physics update is in progress do nothing, as its the joint itself that's probably moving the body
  181. if (gPhysics()._isUpdateInProgress())
  182. return;
  183. if (mBodies[0] == body)
  184. updateTransform(JointBody::Target);
  185. else if (mBodies[1] == body)
  186. updateTransform(JointBody::Anchor);
  187. else
  188. assert(false); // Not allowed to happen
  189. }
  190. bool CJoint::isBodyValid(const HRigidbody& body)
  191. {
  192. if (body == nullptr)
  193. return false;
  194. if (body->_getInternal() == nullptr)
  195. return false;
  196. return true;
  197. }
  198. void CJoint::updateTransform(JointBody body)
  199. {
  200. Vector3 localPos;
  201. Quaternion localRot;
  202. getLocalTransform(body, localPos, localRot);
  203. mInternal->setTransform(body, localPos, localRot);
  204. }
  205. void CJoint::getLocalTransform(JointBody body, Vector3& position, Quaternion& rotation)
  206. {
  207. position = mPositions[(int)body];
  208. rotation = mRotations[(int)body];
  209. HRigidbody rigidbody = mBodies[(int)body];
  210. if (rigidbody == nullptr) // Get world space transform if no relative to any body
  211. {
  212. const Transform& tfrm = SO()->getTransform();
  213. Quaternion worldRot = tfrm.getRotation();
  214. rotation = worldRot*rotation;
  215. position = worldRot.rotate(position) + tfrm.getPosition();
  216. }
  217. else
  218. {
  219. position = rotation.rotate(position);
  220. }
  221. }
  222. void CJoint::triggerOnJointBroken()
  223. {
  224. onJointBreak();
  225. }
  226. RTTITypeBase* CJoint::getRTTIStatic()
  227. {
  228. return CJointRTTI::instance();
  229. }
  230. RTTITypeBase* CJoint::getRTTI() const
  231. {
  232. return CJoint::getRTTIStatic();
  233. }
  234. }