BsCSliderJoint.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCSliderJoint.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Components/BsCRigidbody.h"
  6. #include "Private/RTTI/BsCSliderJointRTTI.h"
  7. namespace bs
  8. {
  9. CSliderJoint::CSliderJoint()
  10. : CJoint(mDesc)
  11. {
  12. setName("SliderJoint");
  13. }
  14. CSliderJoint::CSliderJoint(const HSceneObject& parent)
  15. : CJoint(parent, mDesc)
  16. {
  17. setName("SliderJoint");
  18. }
  19. float CSliderJoint::getPosition() const
  20. {
  21. if (mInternal == nullptr)
  22. return 0.0f;
  23. return _getInternal()->getPosition();
  24. }
  25. float CSliderJoint::getSpeed() const
  26. {
  27. if (mInternal == nullptr)
  28. return 0.0f;
  29. return _getInternal()->getSpeed();
  30. }
  31. LimitLinearRange CSliderJoint::getLimit() const
  32. {
  33. return mDesc.limit;
  34. }
  35. void CSliderJoint::setLimit(const LimitLinearRange& limit)
  36. {
  37. if (mDesc.limit == limit)
  38. return;
  39. mDesc.limit = limit;
  40. if (mInternal != nullptr)
  41. _getInternal()->setLimit(limit);
  42. }
  43. void CSliderJoint::setFlag(SliderJointFlag flag, bool enabled)
  44. {
  45. bool isEnabled = ((UINT32)mDesc.flag & (UINT32)flag) != 0;
  46. if (isEnabled == enabled)
  47. return;
  48. if (enabled)
  49. mDesc.flag = (SliderJointFlag)((UINT32)mDesc.flag | (UINT32)flag);
  50. else
  51. mDesc.flag = (SliderJointFlag)((UINT32)mDesc.flag & ~(UINT32)flag);
  52. if (mInternal != nullptr)
  53. _getInternal()->setFlag(flag, enabled);
  54. }
  55. bool CSliderJoint::hasFlag(SliderJointFlag flag) const
  56. {
  57. return ((UINT32)mDesc.flag & (UINT32)flag) != 0;
  58. }
  59. SPtr<Joint> CSliderJoint::createInternal()
  60. {
  61. SPtr<Joint> joint = SliderJoint::create(mDesc);
  62. joint->_setOwner(PhysicsOwnerType::Component, this);
  63. return joint;
  64. }
  65. void CSliderJoint::getLocalTransform(JointBody body, Vector3& position, Quaternion& rotation)
  66. {
  67. position = mPositions[(int)body];
  68. rotation = mRotations[(int)body];
  69. HRigidbody rigidbody = mBodies[(int)body];
  70. const Transform& tfrm = SO()->getTransform();
  71. if (rigidbody == nullptr) // Get world space transform if no relative to any body
  72. {
  73. Quaternion worldRot = tfrm.getRotation();
  74. rotation = worldRot*rotation;
  75. position = worldRot.rotate(position) + tfrm.getPosition();
  76. }
  77. else
  78. {
  79. const Transform& rigidbodyTfrm = rigidbody->SO()->getTransform();
  80. // Use only the offset for positioning, but for rotation use both the offset and target SO rotation.
  81. // (Needed because we need to rotate the joint SO in order to orient the slider direction, so we need an
  82. // additional transform that allows us to orient the object)
  83. position = rotation.rotate(position);
  84. rotation = (rigidbodyTfrm.getRotation()*rotation).inverse()*tfrm.getRotation();
  85. }
  86. }
  87. RTTITypeBase* CSliderJoint::getRTTIStatic()
  88. {
  89. return CSliderJointRTTI::instance();
  90. }
  91. RTTITypeBase* CSliderJoint::getRTTI() const
  92. {
  93. return CSliderJoint::getRTTIStatic();
  94. }
  95. }