BsCFixedJoint.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCFixedJoint.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Components/BsCRigidbody.h"
  6. #include "Private/RTTI/BsCFixedJointRTTI.h"
  7. namespace bs
  8. {
  9. CFixedJoint::CFixedJoint()
  10. :CJoint(mDesc)
  11. {
  12. setName("FixedJoint");
  13. }
  14. CFixedJoint::CFixedJoint(const HSceneObject& parent)
  15. : CJoint(parent, mDesc)
  16. {
  17. setName("FixedJoint");
  18. }
  19. SPtr<Joint> CFixedJoint::createInternal()
  20. {
  21. SPtr<Joint> joint = FixedJoint::create(mDesc);
  22. joint->_setOwner(PhysicsOwnerType::Component, this);
  23. return joint;
  24. }
  25. void CFixedJoint::getLocalTransform(JointBody body, Vector3& position, Quaternion& rotation)
  26. {
  27. position = mPositions[(int)body];
  28. rotation = mRotations[(int)body];
  29. HRigidbody rigidbody = mBodies[(int)body];
  30. const Transform& tfrm = SO()->getTransform();
  31. if (rigidbody == nullptr) // Get world space transform if no relative to any body
  32. {
  33. Quaternion worldRot = tfrm.getRotation();
  34. rotation = worldRot*rotation;
  35. position = worldRot.rotate(position) + tfrm.getPosition();
  36. }
  37. else
  38. {
  39. const Transform& rigidbodyTfrm = rigidbody->SO()->getTransform();
  40. // Find world space transform
  41. Quaternion worldRot = rigidbodyTfrm.getRotation();
  42. rotation = worldRot * rotation;
  43. position = worldRot.rotate(position) + rigidbodyTfrm.getPosition();
  44. // Get transform of the joint local to the object
  45. Quaternion invRotation = rotation.inverse();
  46. position = invRotation.rotate(tfrm.getPosition() - position);
  47. rotation = invRotation * tfrm.getRotation();
  48. }
  49. }
  50. RTTITypeBase* CFixedJoint::getRTTIStatic()
  51. {
  52. return CFixedJointRTTI::instance();
  53. }
  54. RTTITypeBase* CFixedJoint::getRTTI() const
  55. {
  56. return CFixedJoint::getRTTIStatic();
  57. }
  58. }