BsCSliderJoint.cpp 2.8 KB

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