BsCJoint.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "BsCJointRTTI.h"
  7. using namespace std::placeholders;
  8. namespace BansheeEngine
  9. {
  10. CJoint::CJoint(const HSceneObject& parent)
  11. : Component(parent)
  12. {
  13. setName("Joint");
  14. mNotifyFlags = (TransformChangedFlags)(TCF_Parent | TCF_Transform);
  15. }
  16. HRigidbody CJoint::getBody(JointBody body) const
  17. {
  18. return mBodies[(int)body];
  19. }
  20. void CJoint::setBody(JointBody body, const HRigidbody& value)
  21. {
  22. if (mBodies[(int)body] == value)
  23. return;
  24. mBodies[(int)body] = value;
  25. if(mInternal != nullptr)
  26. {
  27. Rigidbody* rigidbody = nullptr;
  28. if (value != nullptr)
  29. rigidbody = value->_getInternal().get();
  30. mInternal->setBody(body, rigidbody);
  31. updateTransform(body);
  32. }
  33. }
  34. Vector3 CJoint::getPosition(JointBody body) const
  35. {
  36. return mPositions[(int)body];
  37. }
  38. Quaternion CJoint::getRotation(JointBody body) const
  39. {
  40. return mRotations[(int)body];
  41. }
  42. void CJoint::setTransform(JointBody body, const Vector3& position, const Quaternion& rotation)
  43. {
  44. if (mPositions[(int)body] == position && mRotations[(int)body] == rotation)
  45. return;
  46. mPositions[(int)body] = position;
  47. mRotations[(int)body] = rotation;
  48. if(mInternal != nullptr)
  49. updateTransform(body);
  50. }
  51. float CJoint::getBreakForce() const
  52. {
  53. return mBreakForce;
  54. }
  55. void CJoint::setBreakForce(float force)
  56. {
  57. if (mBreakForce == force)
  58. return;
  59. mBreakForce = force;
  60. if (mInternal != nullptr)
  61. mInternal->setBreakForce(force);
  62. }
  63. float CJoint::getBreakTorque() const
  64. {
  65. return mBreakTorque;
  66. }
  67. void CJoint::setBreakToque(float torque)
  68. {
  69. if (mBreakTorque == torque)
  70. return;
  71. mBreakTorque = torque;
  72. if (mInternal != nullptr)
  73. mInternal->setBreakTorque(torque);
  74. }
  75. bool CJoint::getEnableCollision() const
  76. {
  77. return mEnableCollision;
  78. }
  79. void CJoint::setEnableCollision(bool value)
  80. {
  81. if (mEnableCollision == value)
  82. return;
  83. mEnableCollision = value;
  84. if (mInternal != nullptr)
  85. mInternal->setEnableCollision(value);
  86. }
  87. void CJoint::onInitialized()
  88. {
  89. }
  90. void CJoint::onDestroyed()
  91. {
  92. // This should release the last reference and destroy the internal joint
  93. mInternal = nullptr;
  94. }
  95. void CJoint::onDisabled()
  96. {
  97. // This should release the last reference and destroy the internal joint
  98. mInternal = nullptr;
  99. }
  100. void CJoint::onEnabled()
  101. {
  102. restoreInternal();
  103. }
  104. void CJoint::onTransformChanged(TransformChangedFlags flags)
  105. {
  106. if (!SO()->getActive())
  107. return;
  108. // TODO - This might get called during physics update, perhaps avoid that?
  109. updateTransform(JointBody::A);
  110. updateTransform(JointBody::B);
  111. }
  112. void CJoint::restoreInternal()
  113. {
  114. if (mInternal == nullptr)
  115. {
  116. mInternal = createInternal();
  117. mInternal->onJointBreak.connect(std::bind(&CJoint::triggerOnJointBroken, this));
  118. }
  119. // Note: Merge into one call to avoid many virtual function calls
  120. Rigidbody* bodies[2];
  121. if (mBodies[0] != nullptr)
  122. bodies[0] = mBodies[0]->_getInternal().get();
  123. else
  124. bodies[0] = nullptr;
  125. if (mBodies[1] != nullptr)
  126. bodies[1] = mBodies[1]->_getInternal().get();
  127. else
  128. bodies[1] = nullptr;
  129. mInternal->setBody(JointBody::A, bodies[0]);
  130. mInternal->setBody(JointBody::B, bodies[1]);
  131. mInternal->setBreakForce(mBreakForce);
  132. mInternal->setBreakTorque(mBreakTorque);
  133. mInternal->setEnableCollision(mEnableCollision);
  134. updateTransform(JointBody::A);
  135. updateTransform(JointBody::B);
  136. }
  137. void CJoint::updateTransform(JointBody body)
  138. {
  139. Vector3 localPos;
  140. Quaternion localRot;
  141. localPos = mPositions[(int)body];
  142. localRot = mRotations[(int)body];
  143. // Transform to world space of the related body
  144. HRigidbody rigidbody = mBodies[(int)body];
  145. if(rigidbody != nullptr)
  146. {
  147. localRot = rigidbody->SO()->getWorldRotation() * localRot;
  148. localPos = localRot.rotate(localPos) + rigidbody->SO()->getWorldPosition();
  149. }
  150. // Transform to space local to the joint
  151. Quaternion invRotation = SO()->getWorldRotation().inverse();
  152. localPos = invRotation.rotate(localPos - SO()->getWorldPosition());
  153. localRot = invRotation * localRot;
  154. mInternal->setTransform(body, localPos, localRot);
  155. }
  156. void CJoint::triggerOnJointBroken()
  157. {
  158. onJointBreak();
  159. }
  160. RTTITypeBase* CJoint::getRTTIStatic()
  161. {
  162. return CJointRTTI::instance();
  163. }
  164. RTTITypeBase* CJoint::getRTTI() const
  165. {
  166. return CJoint::getRTTIStatic();
  167. }
  168. }