BsCJoint.cpp 6.0 KB

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