1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Components/BsCFixedJoint.h"
4#include "Scene/BsSceneObject.h"
5#include "Components/BsCRigidbody.h"
6#include "Private/RTTI/BsCFixedJointRTTI.h"
7#include "Scene/BsSceneManager.h"
8
9namespace bs
10{
11 CFixedJoint::CFixedJoint()
12 :CJoint(mDesc)
13 {
14 setName("FixedJoint");
15 }
16
17 CFixedJoint::CFixedJoint(const HSceneObject& parent)
18 : CJoint(parent, mDesc)
19 {
20 setName("FixedJoint");
21 }
22
23 SPtr<Joint> CFixedJoint::createInternal()
24 {
25 const SPtr<SceneInstance>& scene = SO()->getScene();
26 SPtr<Joint> joint = FixedJoint::create(*scene->getPhysicsScene(), mDesc);
27
28 joint->_setOwner(PhysicsOwnerType::Component, this);
29 return joint;
30 }
31
32 void CFixedJoint::getLocalTransform(JointBody body, Vector3& position, Quaternion& rotation)
33 {
34 position = mPositions[(UINT32)body];
35 rotation = mRotations[(UINT32)body];
36
37 HRigidbody rigidbody = mBodies[(UINT32)body];
38 const Transform& tfrm = SO()->getTransform();
39 if (rigidbody == nullptr) // Get world space transform if no relative to any body
40 {
41 Quaternion worldRot = tfrm.getRotation();
42
43 rotation = worldRot*rotation;
44 position = worldRot.rotate(position) + tfrm.getPosition();
45 }
46 else
47 {
48 const Transform& rigidbodyTfrm = rigidbody->SO()->getTransform();
49
50 // Find world space transform
51 Quaternion worldRot = rigidbodyTfrm.getRotation();
52
53 rotation = worldRot * rotation;
54 position = worldRot.rotate(position) + rigidbodyTfrm.getPosition();
55
56 // Get transform of the joint local to the object
57 Quaternion invRotation = rotation.inverse();
58
59 position = invRotation.rotate(tfrm.getPosition() - position);
60 rotation = invRotation * tfrm.getRotation();
61 }
62 }
63
64 RTTITypeBase* CFixedJoint::getRTTIStatic()
65 {
66 return CFixedJointRTTI::instance();
67 }
68
69 RTTITypeBase* CFixedJoint::getRTTI() const
70 {
71 return CFixedJoint::getRTTIStatic();
72 }
73}
74