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