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#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "Physics/BsJoint.h"
7#include "Scene/BsComponent.h"
8
9namespace bs
10{
11 /** @addtogroup Components-Core
12 * @{
13 */
14
15 /**
16 * @copydoc Joint
17 *
18 * @note Wraps Joint as a Component.
19 */
20 class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Physics,n:Joint) CJoint : public Component
21 {
22 public:
23 CJoint(const HSceneObject& parent, JOINT_DESC& desc);
24 virtual ~CJoint() = default;
25
26 /** @copydoc Joint::getBody */
27 BS_SCRIPT_EXPORT(n:GetBody)
28 HRigidbody getBody(JointBody body) const;
29
30 /** @copydoc Joint::setBody */
31 BS_SCRIPT_EXPORT(n:SetBody)
32 void setBody(JointBody body, const HRigidbody& value);
33
34 /** @copydoc Joint::getPosition */
35 BS_SCRIPT_EXPORT(n:GetPosition)
36 Vector3 getPosition(JointBody body) const;
37
38 /** @copydoc Joint::getRotation */
39 BS_SCRIPT_EXPORT(n:GetRotation)
40 Quaternion getRotation(JointBody body) const;
41
42 /** @copydoc Joint::setTransform */
43 BS_SCRIPT_EXPORT(n:SetTransform)
44 void setTransform(JointBody body, const Vector3& position, const Quaternion& rotation);
45
46 /** @copydoc Joint::getBreakForce */
47 BS_SCRIPT_EXPORT(n:BreakForce,pr:getter)
48 float getBreakForce() const;
49
50 /** @copydoc Joint::setBreakForce */
51 BS_SCRIPT_EXPORT(n:BreakForce,pr:setter)
52 void setBreakForce(float force);
53
54 /** @copydoc Joint::getBreakTorque */
55 BS_SCRIPT_EXPORT(n:BreakTorque,pr:getter)
56 float getBreakTorque() const;
57
58 /** @copydoc Joint::setBreakTorque */
59 BS_SCRIPT_EXPORT(n:BreakTorque,pr:setter)
60 void setBreakTorque(float torque);
61
62 /** @copydoc Joint::getEnableCollision */
63 BS_SCRIPT_EXPORT(n:EnableCollision,pr:getter)
64 bool getEnableCollision() const;
65
66 /** @copydoc Joint::setEnableCollision */
67 BS_SCRIPT_EXPORT(n:EnableCollision,pr:setter)
68 void setEnableCollision(bool value);
69
70 /** @copydoc Joint::onJointBreak */
71 BS_SCRIPT_EXPORT(n:OnJointBreak)
72 Event<void()> onJointBreak;
73
74 /** @name Internal
75 * @{
76 */
77
78 /** Returns the Joint implementation wrapped by this component. */
79 Joint* _getInternal() const { return mInternal.get(); }
80
81 /** @} */
82
83 /************************************************************************/
84 /* COMPONENT OVERRIDES */
85 /************************************************************************/
86 protected:
87 friend class SceneObject;
88
89 /** @copydoc Component::onInitialized() */
90 void onInitialized() override;
91
92 /** @copydoc Component::onDestroyed() */
93 void onDestroyed() override;
94
95 /** @copydoc Component::onDisabled() */
96 void onDisabled() override;
97
98 /** @copydoc Component::onEnabled() */
99 void onEnabled() override;
100
101 /** @copydoc Component::onTransformChanged() */
102 void onTransformChanged(TransformChangedFlags flags) override;
103
104 protected:
105 friend class CRigidbody;
106 using Component::destroyInternal;
107
108 /** Creates the internal representation of the Joint for use by the component. */
109 virtual SPtr<Joint> createInternal() = 0;
110
111 /** Creates the internal representation of the Joint and restores the values saved by the Component. */
112 virtual void restoreInternal();
113
114 /** Calculates the local position/rotation that needs to be applied to the particular joint body. */
115 virtual void getLocalTransform(JointBody body, Vector3& position, Quaternion& rotation);
116
117 /** Destroys the internal joint representation. */
118 void destroyInternal();
119
120 /** Notifies the joint that one of the attached rigidbodies moved and that its transform needs updating. */
121 void notifyRigidbodyMoved(const HRigidbody& body);
122
123 /** Checks can the provided rigidbody be used for initializing the joint. */
124 bool isBodyValid(const HRigidbody& body);
125
126 /** Updates the local transform for the specified body attached to the joint. */
127 void updateTransform(JointBody body);
128
129 /** Triggered when the joint constraint gets broken. */
130 void triggerOnJointBroken();
131
132 SPtr<Joint> mInternal;
133
134 HRigidbody mBodies[2];
135 Vector3 mPositions[2];
136 Quaternion mRotations[2];
137
138 private:
139 JOINT_DESC& mDesc;
140
141 /************************************************************************/
142 /* RTTI */
143 /************************************************************************/
144 public:
145 friend class CJointRTTI;
146 static RTTITypeBase* getRTTIStatic();
147 RTTITypeBase* getRTTI() const override;
148
149 CJoint(JOINT_DESC& desc); // Serialization only
150 };
151
152 /** @} */
153}