BsCJoint.cpp 6.8 KB

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