BsCRigidbody.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsRigidbody.h"
  6. #include "BsComponent.h"
  7. #include "BsVector3.h"
  8. #include "BsQuaternion.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup Components-Core
  12. * @{
  13. */
  14. /**
  15. * @copydoc Rigidbody
  16. *
  17. * Wraps Rigidbody as a Component. Colliders that are on the same scene object as the rigidbody, or on child scene
  18. * objects are automatically considered as part of the rigidbody.
  19. */
  20. class BS_CORE_EXPORT CRigidbody : public Component
  21. {
  22. public:
  23. CRigidbody(const HSceneObject& parent);
  24. /** @copydoc Rigidbody::move */
  25. inline void move(const Vector3& position);
  26. /** @copydoc Rigidbody::rotate */
  27. inline void rotate(const Quaternion& rotation);
  28. /** @copydoc Rigidbody::setMass */
  29. inline void setMass(float mass);
  30. /** @copydoc Rigidbody::getMass */
  31. float getMass() const { return mMass; };
  32. /** @copydoc Rigidbody::setIsKinematic */
  33. inline void setIsKinematic(bool kinematic);
  34. /** @copydoc Rigidbody::getIsKinematic */
  35. bool getIsKinematic() const { return mIsKinematic; }
  36. /** @copydoc Rigidbody::isSleeping */
  37. inline bool isSleeping() const;
  38. /** @copydoc Rigidbody::sleep */
  39. inline void sleep();
  40. /** @copydoc Rigidbody::wakeUp */
  41. inline void wakeUp();
  42. /** @copydoc Rigidbody::setSleepThreshold */
  43. inline void setSleepThreshold(float threshold);
  44. /** @copydoc Rigidbody::getSleepThreshold */
  45. float getSleepThreshold() const { return mSleepThreshold; }
  46. /** @copydoc Rigidbody::setUseGravity */
  47. inline void setUseGravity(bool gravity);
  48. /** @copydoc Rigidbody::getUseGravity */
  49. bool getUseGravity() const { return mUseGravity; }
  50. /** @copydoc Rigidbody::setVelocity */
  51. inline void setVelocity(const Vector3& velocity);
  52. /** @copydoc Rigidbody::getVelocity */
  53. inline Vector3 getVelocity() const;
  54. /** @copydoc Rigidbody::setAngularVelocity */
  55. inline void setAngularVelocity(const Vector3& velocity);
  56. /** @copydoc Rigidbody::getAngularVelocity */
  57. inline Vector3 getAngularVelocity() const;
  58. /** @copydoc Rigidbody::setDrag */
  59. inline void setDrag(float drag);
  60. /** @copydoc Rigidbody::getDrag */
  61. float getDrag() const { return mLinearDrag; }
  62. /** @copydoc Rigidbody::setAngularDrag */
  63. inline void setAngularDrag(float drag);
  64. /** @copydoc Rigidbody::getAngularDrag */
  65. float getAngularDrag() const { return mAngularDrag; }
  66. /** @copydoc Rigidbody::setInertiaTensor */
  67. inline void setInertiaTensor(const Vector3& tensor);
  68. /** @copydoc Rigidbody::getInertiaTensor */
  69. inline Vector3 getInertiaTensor() const;
  70. /** @copydoc Rigidbody::setMaxAngularVelocity */
  71. inline void setMaxAngularVelocity(float maxVelocity);
  72. /** @copydoc Rigidbody::getMaxAngularVelocity */
  73. float getMaxAngularVelocity() const { return mMaxAngularVelocity; }
  74. /** @copydoc Rigidbody::setCenterOfMass */
  75. inline void setCenterOfMass(const Vector3& position, const Quaternion& rotation);
  76. /** @copydoc Rigidbody::getCenterOfMassPosition */
  77. inline Vector3 getCenterOfMassPosition() const;
  78. /** @copydoc Rigidbody::getCenterOfMassRotation */
  79. inline Quaternion getCenterOfMassRotation() const;
  80. /** @copydoc Rigidbody::setPositionSolverCount */
  81. inline void setPositionSolverCount(UINT32 count);
  82. /** @copydoc Rigidbody::getPositionSolverCount */
  83. UINT32 getPositionSolverCount() const { return mPositionSolverCount; }
  84. /** @copydoc Rigidbody::setVelocitySolverCount */
  85. inline void setVelocitySolverCount(UINT32 count);
  86. /** @copydoc Rigidbody::getVelocitySolverCount */
  87. UINT32 getVelocitySolverCount() const { return mVelocitySolverCount; }
  88. /** Sets a value that determines which (if any) collision events are reported. */
  89. inline void setCollisionReportMode(CollisionReportMode mode);
  90. /** Gets a value that determines which (if any) collision events are reported. */
  91. CollisionReportMode getCollisionReportMode() const { return mCollisionReportMode; }
  92. /** @copydoc Rigidbody::setFlags */
  93. inline void setFlags(Rigidbody::Flag flags);
  94. /** @copydoc Rigidbody::getFlags */
  95. Rigidbody::Flag getFlags() const { return mFlags; }
  96. /** @copydoc Rigidbody::addForce */
  97. inline void addForce(const Vector3& force, ForceMode mode = ForceMode::Force);
  98. /** @copydoc Rigidbody::addTorque */
  99. inline void addTorque(const Vector3& torque, ForceMode mode = ForceMode::Force);
  100. /** @copydoc Rigidbody::addForceAtPoint */
  101. inline void addForceAtPoint(const Vector3& force, const Vector3& position,
  102. PointForceMode mode = PointForceMode::Force);
  103. /** @copydoc Rigidbody::getVelocityAtPoint */
  104. inline Vector3 getVelocityAtPoint(const Vector3& point) const;
  105. /** @copydoc Rigidbody::onCollisionBegin */
  106. Event<void(const CollisionData&)> onCollisionBegin;
  107. /** @copydoc Rigidbody::onCollisionStay */
  108. Event<void(const CollisionData&)> onCollisionStay;
  109. /** @copydoc Rigidbody::onCollisionEnd */
  110. Event<void(const CollisionData&)> onCollisionEnd;
  111. /** @name Internal
  112. * @{
  113. */
  114. /** Returns the Rigidbody implementation wrapped by this component. */
  115. Rigidbody* _getInternal() const { return mInternal.get(); }
  116. /** Sets that joint that this rigidbody is attached to. Allows the rigidbody to notify the joint when it moves. */
  117. void _setJoint(const HJoint& joint) { mParentJoint = joint; }
  118. /** @copydoc Rigidbody::updateMassDistribution */
  119. inline void _updateMassDistribution();
  120. /** @} */
  121. protected:
  122. friend class CCollider;
  123. using Component::destroyInternal;
  124. /**
  125. * Searches child scene objects for Collider components and attaches them to the rigidbody. Make sure to call
  126. * clearColliders() if you need to clear old colliders first.
  127. */
  128. void updateColliders();
  129. /** Unregisters all child colliders from the Rigidbody. */
  130. void clearColliders();
  131. /**
  132. * Registers a new collider with the Rigidbody. This collider will then be used to calculate Rigidbody's geometry
  133. * used for collisions, and optionally (depending on set flags) total mass, inertia tensors and center of mass.
  134. */
  135. void addCollider(const HCollider& collider);
  136. /** Unregisters the collider from the Rigidbody. */
  137. void removeCollider(const HCollider& collider);
  138. /** Checks if the rigidbody is nested under another rigidbody, and throws out a warning if so. */
  139. void checkForNestedRigibody();
  140. /** Appends Component referenes for the colliders to the collision data. */
  141. void processCollisionData(CollisionData& data);
  142. /** Destroys the internal rigidbody representation. */
  143. void destroyInternal();
  144. /** Triggered when the internal rigidbody begins touching another object. */
  145. void triggerOnCollisionBegin(const CollisionData& data);
  146. /** Triggered when the internal rigidbody continues touching another object. */
  147. void triggerOnCollisionStay(const CollisionData& data);
  148. /** Triggered when the internal rigidbody ends touching another object. */
  149. void triggerOnCollisionEnd(const CollisionData& data);
  150. /************************************************************************/
  151. /* COMPONENT OVERRIDES */
  152. /************************************************************************/
  153. protected:
  154. friend class SceneObject;
  155. /** @copydoc Component::onInitialized() */
  156. void onInitialized() override;
  157. /** @copydoc Component::onDestroyed() */
  158. void onDestroyed() override;
  159. /** @copydoc Component::onDisabled() */
  160. void onDisabled() override;
  161. /** @copydoc Component::onEnabled() */
  162. void onEnabled() override;
  163. /** @copydoc Component::onTransformChanged() */
  164. void onTransformChanged(TransformChangedFlags flags) override;
  165. SPtr<Rigidbody> mInternal;
  166. Vector<HCollider> mChildren;
  167. HJoint mParentJoint;
  168. UINT32 mPositionSolverCount = 4;
  169. UINT32 mVelocitySolverCount = 1;
  170. Rigidbody::Flag mFlags = (Rigidbody::Flag)((UINT32)Rigidbody::Flag::AutoTensors | (UINT32)Rigidbody::Flag::AutoMass);
  171. CollisionReportMode mCollisionReportMode = CollisionReportMode::None;
  172. Vector3 mCMassPosition;
  173. Quaternion mCMassRotation;
  174. Vector3 mInertiaTensor;
  175. float mMass = 0.0f;
  176. float mMaxAngularVelocity = 1.0f;
  177. float mLinearDrag = 0.0f;
  178. float mAngularDrag = 0.0f;
  179. float mSleepThreshold = 0.0f;
  180. bool mUseGravity = true;
  181. bool mIsKinematic = false;
  182. /************************************************************************/
  183. /* RTTI */
  184. /************************************************************************/
  185. public:
  186. friend class CRigidbodyRTTI;
  187. static RTTITypeBase* getRTTIStatic();
  188. RTTITypeBase* getRTTI() const override;
  189. protected:
  190. CRigidbody() {} // Serialization only
  191. };
  192. /** @} */
  193. }