BsCFixedJoint.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "RTTI/BsCFixedJointRTTI.h"
  7. namespace bs
  8. {
  9. CFixedJoint::CFixedJoint()
  10. :CJoint(mDesc)
  11. { }
  12. CFixedJoint::CFixedJoint(const HSceneObject& parent)
  13. : CJoint(parent, mDesc)
  14. {
  15. setName("FixedJoint");
  16. }
  17. SPtr<Joint> CFixedJoint::createInternal()
  18. {
  19. SPtr<Joint> joint = FixedJoint::create(mDesc);
  20. joint->_setOwner(PhysicsOwnerType::Component, this);
  21. return joint;
  22. }
  23. void CFixedJoint::getLocalTransform(JointBody body, Vector3& position, Quaternion& rotation)
  24. {
  25. position = mPositions[(int)body];
  26. rotation = mRotations[(int)body];
  27. HRigidbody rigidbody = mBodies[(int)body];
  28. const Transform& tfrm = SO()->getTransform();
  29. if (rigidbody == nullptr) // Get world space transform if no relative to any body
  30. {
  31. Quaternion worldRot = tfrm.getRotation();
  32. rotation = worldRot*rotation;
  33. position = worldRot.rotate(position) + tfrm.getPosition();
  34. }
  35. else
  36. {
  37. const Transform& rigidbodyTfrm = rigidbody->SO()->getTransform();
  38. // Find world space transform
  39. Quaternion worldRot = rigidbodyTfrm.getRotation();
  40. rotation = worldRot * rotation;
  41. position = worldRot.rotate(position) + rigidbodyTfrm.getPosition();
  42. // Get transform of the joint local to the object
  43. Quaternion invRotation = rotation.inverse();
  44. position = invRotation.rotate(tfrm.getPosition() - position);
  45. rotation = invRotation * tfrm.getRotation();
  46. }
  47. }
  48. RTTITypeBase* CFixedJoint::getRTTIStatic()
  49. {
  50. return CFixedJointRTTI::instance();
  51. }
  52. RTTITypeBase* CFixedJoint::getRTTI() const
  53. {
  54. return CFixedJoint::getRTTIStatic();
  55. }
  56. }