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/BsCHingeJoint.h"
4#include "Scene/BsSceneObject.h"
5#include "Private/RTTI/BsCHingeJointRTTI.h"
6#include "Scene/BsSceneManager.h"
7
8namespace bs
9{
10 CHingeJoint::CHingeJoint()
11 : CJoint(mDesc)
12 {
13 setName("HingeJoint");
14 }
15
16 CHingeJoint::CHingeJoint(const HSceneObject& parent)
17 : CJoint(parent, mDesc)
18 {
19 setName("HingeJoint");
20 }
21
22 Radian CHingeJoint::getAngle() const
23 {
24 if (mInternal == nullptr)
25 return Radian(0.0f);
26
27 return _getInternal()->getAngle();
28 }
29
30 float CHingeJoint::getSpeed() const
31 {
32 if (mInternal == nullptr)
33 return 0.0f;
34
35 return _getInternal()->getSpeed();
36 }
37
38 LimitAngularRange CHingeJoint::getLimit() const
39 {
40 return mDesc.limit;
41 }
42
43 void CHingeJoint::setLimit(const LimitAngularRange& limit)
44 {
45 if (limit == mDesc.limit)
46 return;
47
48 mDesc.limit = limit;
49
50 if (mInternal != nullptr)
51 _getInternal()->setLimit(limit);
52 }
53
54 HingeJointDrive CHingeJoint::getDrive() const
55 {
56 return mDesc.drive;
57 }
58
59 void CHingeJoint::setDrive(const HingeJointDrive& drive)
60 {
61 if (drive == mDesc.drive)
62 return;
63
64 mDesc.drive = drive;
65
66 if (mInternal != nullptr)
67 _getInternal()->setDrive(drive);
68 }
69
70 void CHingeJoint::setFlag(HingeJointFlag flag, bool enabled)
71 {
72 bool isEnabled = ((UINT32)mDesc.flag & (UINT32)flag) != 0;
73 if (isEnabled == enabled)
74 return;
75
76 if (enabled)
77 mDesc.flag = (HingeJointFlag)((UINT32)mDesc.flag | (UINT32)flag);
78 else
79 mDesc.flag = (HingeJointFlag)((UINT32)mDesc.flag & ~(UINT32)flag);
80
81 if (mInternal != nullptr)
82 _getInternal()->setFlag(flag, enabled);
83 }
84
85 bool CHingeJoint::hasFlag(HingeJointFlag flag) const
86 {
87 return ((UINT32)mDesc.flag & (UINT32)flag) != 0;
88 }
89
90 SPtr<Joint> CHingeJoint::createInternal()
91 {
92 const SPtr<SceneInstance>& scene = SO()->getScene();
93 SPtr<Joint> joint = HingeJoint::create(*scene->getPhysicsScene(), mDesc);
94
95 joint->_setOwner(PhysicsOwnerType::Component, this);
96 return joint;
97 }
98
99 RTTITypeBase* CHingeJoint::getRTTIStatic()
100 {
101 return CHingeJointRTTI::instance();
102 }
103
104 RTTITypeBase* CHingeJoint::getRTTI() const
105 {
106 return CHingeJoint::getRTTIStatic();
107 }
108}
109