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/BsCDistanceJoint.h"
4#include "Scene/BsSceneObject.h"
5#include "Private/RTTI/BsCDistanceJointRTTI.h"
6#include "Scene/BsSceneManager.h"
7
8namespace bs
9{
10 CDistanceJoint::CDistanceJoint()
11 : CJoint(mDesc)
12 {
13 setName("DistanceJoint");
14 }
15
16 CDistanceJoint::CDistanceJoint(const HSceneObject& parent)
17 : CJoint(parent, mDesc)
18 {
19 setName("DistanceJoint");
20 }
21
22 float CDistanceJoint::getDistance() const
23 {
24 if (mInternal == nullptr)
25 return 0.0f;
26
27 return _getInternal()->getDistance();
28 }
29
30 float CDistanceJoint::getMinDistance() const
31 {
32 return mDesc.minDistance;
33 }
34
35 void CDistanceJoint::setMinDistance(float value)
36 {
37 if (mDesc.minDistance == value)
38 return;
39
40 mDesc.minDistance = value;
41
42 if (mInternal != nullptr)
43 _getInternal()->setMinDistance(value);
44 }
45
46 float CDistanceJoint::getMaxDistance() const
47 {
48 return mDesc.maxDistance;
49 }
50
51 void CDistanceJoint::setMaxDistance(float value)
52 {
53 if (mDesc.maxDistance == value)
54 return;
55
56 mDesc.maxDistance = value;
57
58 if (mInternal != nullptr)
59 _getInternal()->setMaxDistance(value);
60 }
61
62 float CDistanceJoint::getTolerance() const
63 {
64 return mDesc.tolerance;
65 }
66
67 void CDistanceJoint::setTolerance(float value)
68 {
69 if (mDesc.tolerance == value)
70 return;
71
72 mDesc.tolerance = value;
73
74 if (mInternal != nullptr)
75 _getInternal()->setTolerance(value);
76 }
77
78 Spring CDistanceJoint::getSpring() const
79 {
80 return mDesc.spring;
81 }
82
83 void CDistanceJoint::setSpring(const Spring& value)
84 {
85 if (mDesc.spring == value)
86 return;
87
88 mDesc.spring = value;
89
90 if(mInternal != nullptr)
91 _getInternal()->setSpring(value);
92 }
93
94 void CDistanceJoint::setFlag(DistanceJointFlag flag, bool enabled)
95 {
96 bool isEnabled = ((UINT32)mDesc.flag & (UINT32)flag) != 0;
97 if (isEnabled == enabled)
98 return;
99
100 if (enabled)
101 mDesc.flag = (DistanceJointFlag)((UINT32)mDesc.flag | (UINT32)flag);
102 else
103 mDesc.flag = (DistanceJointFlag)((UINT32)mDesc.flag & ~(UINT32)flag);
104
105 if (mInternal != nullptr)
106 _getInternal()->setFlag(flag, enabled);
107 }
108
109 bool CDistanceJoint::hasFlag(DistanceJointFlag flag) const
110 {
111 return ((UINT32)mDesc.flag & (UINT32)flag) != 0;
112 }
113
114 SPtr<Joint> CDistanceJoint::createInternal()
115 {
116 const SPtr<SceneInstance>& scene = SO()->getScene();
117 SPtr<Joint> joint = DistanceJoint::create(*scene->getPhysicsScene(), mDesc);
118
119 joint->_setOwner(PhysicsOwnerType::Component, this);
120 return joint;
121 }
122
123 RTTITypeBase* CDistanceJoint::getRTTIStatic()
124 {
125 return CDistanceJointRTTI::instance();
126 }
127
128 RTTITypeBase* CDistanceJoint::getRTTI() const
129 {
130 return CDistanceJoint::getRTTIStatic();
131 }
132}
133