BsCJoint.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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().get();
  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. // This should release the last reference and destroy the internal joint
  102. mInternal = nullptr;
  103. }
  104. void CJoint::onDisabled()
  105. {
  106. // This should release the last reference and destroy the internal joint
  107. mInternal = nullptr;
  108. }
  109. void CJoint::onEnabled()
  110. {
  111. restoreInternal();
  112. }
  113. void CJoint::onTransformChanged(TransformChangedFlags flags)
  114. {
  115. if (!SO()->getActive())
  116. return;
  117. // We're ignoring this during physics update because it would cause problems if the joint itself was moved by physics
  118. // Note: This isn't particularily correct because if the joint is being moved by physics but the rigidbodies
  119. // themselves are not parented to the joint, the transform will need updating. However I'm leaving it up to the
  120. // user to ensure rigidbodies are always parented to the joint in such a case (It's an unlikely situation that
  121. // I can't think of an use for - joint transform will almost always be set as an initialization step and not a
  122. // physics response).
  123. if (gPhysics()._isUpdateInProgress())
  124. return;
  125. updateTransform(JointBody::A);
  126. updateTransform(JointBody::B);
  127. }
  128. void CJoint::restoreInternal()
  129. {
  130. if (mInternal == nullptr)
  131. {
  132. mInternal = createInternal();
  133. mInternal->onJointBreak.connect(std::bind(&CJoint::triggerOnJointBroken, this));
  134. }
  135. // Note: Merge into one call to avoid many virtual function calls
  136. Rigidbody* bodies[2];
  137. if (mBodies[0] != nullptr)
  138. bodies[0] = mBodies[0]->_getInternal().get();
  139. else
  140. bodies[0] = nullptr;
  141. if (mBodies[1] != nullptr)
  142. bodies[1] = mBodies[1]->_getInternal().get();
  143. else
  144. bodies[1] = nullptr;
  145. mInternal->setBody(JointBody::A, bodies[0]);
  146. mInternal->setBody(JointBody::B, bodies[1]);
  147. mInternal->setBreakForce(mBreakForce);
  148. mInternal->setBreakTorque(mBreakTorque);
  149. mInternal->setEnableCollision(mEnableCollision);
  150. updateTransform(JointBody::A);
  151. updateTransform(JointBody::B);
  152. }
  153. void CJoint::notifyRigidbodyMoved(const HRigidbody& body)
  154. {
  155. // If physics update is in progress do nothing, as its the joint itself that's probably moving the body
  156. if (gPhysics()._isUpdateInProgress())
  157. return;
  158. if (mBodies[0] == body)
  159. updateTransform(JointBody::A);
  160. else if (mBodies[1] == body)
  161. updateTransform(JointBody::B);
  162. else
  163. assert(false); // Not allowed to happen
  164. }
  165. void CJoint::updateTransform(JointBody body)
  166. {
  167. Vector3 parentPos = SO()->getWorldPosition();
  168. Quaternion parentRot = SO()->getWorldRotation();
  169. // Add local position/rotation offset to the joint's transform
  170. Vector3 worldPos = parentPos + parentRot.rotate(mPositions[(int)body]);
  171. Quaternion worldRot = parentRot * mRotations[(int)body];
  172. // Transform body's world position/rotation into space local to the joint + offset space
  173. Vector3 bodyPos;
  174. Quaternion bodyRot;
  175. HRigidbody rigidbody = mBodies[(int)body];
  176. if(rigidbody != nullptr)
  177. {
  178. bodyPos = rigidbody->SO()->getWorldPosition();
  179. bodyRot = rigidbody->SO()->getWorldRotation();
  180. }
  181. Quaternion invRotation = worldRot.inverse();
  182. bodyPos = invRotation.rotate(bodyPos - worldPos);
  183. bodyRot = invRotation * bodyRot;
  184. mInternal->setTransform(body, bodyPos, bodyRot);
  185. }
  186. void CJoint::triggerOnJointBroken()
  187. {
  188. onJointBreak();
  189. }
  190. RTTITypeBase* CJoint::getRTTIStatic()
  191. {
  192. return CJointRTTI::instance();
  193. }
  194. RTTITypeBase* CJoint::getRTTI() const
  195. {
  196. return CJoint::getRTTIStatic();
  197. }
  198. }