BsCRigidbody.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. /** @copydoc Rigidbody::setInterpolationMode */
  89. void setInterpolationMode(Rigidbody::InterpolationMode value);
  90. /** @copydoc Rigidbody::getInterpolationMode */
  91. Rigidbody::InterpolationMode getInterpolationMode(Rigidbody::InterpolationMode value) const
  92. { return mInterpolationMode; }
  93. /** Sets a value that determines which (if any) collision events are reported. */
  94. inline void setCollisionReportMode(CollisionReportMode mode);
  95. /** Gets a value that determines which (if any) collision events are reported. */
  96. CollisionReportMode getCollisionReportMode() const { return mCollisionReportMode; }
  97. /** @copydoc Rigidbody::setFlags */
  98. inline void setFlags(Rigidbody::Flag flags);
  99. /** @copydoc Rigidbody::getFlags */
  100. Rigidbody::Flag getFlags() const { return mFlags; }
  101. /** @copydoc Rigidbody::addForce */
  102. inline void addForce(const Vector3& force, ForceMode mode = ForceMode::Force);
  103. /** @copydoc Rigidbody::addTorque */
  104. inline void addTorque(const Vector3& torque, ForceMode mode = ForceMode::Force);
  105. /** @copydoc Rigidbody::addForceAtPoint */
  106. inline void addForceAtPoint(const Vector3& force, const Vector3& position,
  107. PointForceMode mode = PointForceMode::Force);
  108. /** @copydoc Rigidbody::getVelocityAtPoint */
  109. inline Vector3 getVelocityAtPoint(const Vector3& point) const;
  110. /** @copydoc Rigidbody::onCollisionBegin */
  111. Event<void(const CollisionData&)> onCollisionBegin;
  112. /** @copydoc Rigidbody::onCollisionStay */
  113. Event<void(const CollisionData&)> onCollisionStay;
  114. /** @copydoc Rigidbody::onCollisionEnd */
  115. Event<void(const CollisionData&)> onCollisionEnd;
  116. /** @name Internal
  117. * @{
  118. */
  119. /** Returns the Rigidbody implementation wrapped by this component. */
  120. Rigidbody* _getInternal() const { return mInternal.get(); }
  121. /** Sets that joint that this rigidbody is attached to. Allows the rigidbody to notify the joint when it moves. */
  122. void _setJoint(const HJoint& joint) { mParentJoint = joint; }
  123. /** @copydoc Rigidbody::_updateMassDistribution */
  124. inline void _updateMassDistribution();
  125. /** @} */
  126. protected:
  127. friend class CCollider;
  128. /**
  129. * Searches child scene objects for Collider components and attaches them to the rigidbody. Make sure to call
  130. * clearColliders() if you need to clear old colliders first.
  131. */
  132. void updateColliders();
  133. /** Unregisters all child colliders from the Rigidbody. */
  134. void clearColliders();
  135. /**
  136. * Registers a new collider with the Rigidbody. This collider will then be used to calculate Rigidbody's geometry
  137. * used for collisions, and optionally (depending on set flags) total mass, inertia tensors and center of mass.
  138. */
  139. void addCollider(const HCollider& collider);
  140. /** Unregisters the collider from the Rigidbody. */
  141. void removeCollider(const HCollider& collider);
  142. /** Checks if the rigidbody is nested under another rigidbody, and throws out a warning if so. */
  143. void checkForNestedRigibody();
  144. /** Appends Component referenes for the colliders to the collision data. */
  145. void processCollisionData(CollisionData& data);
  146. /** Destroys the internal rigidbody representation. */
  147. void destroyInternal();
  148. /** Triggered when the internal rigidbody begins touching another object. */
  149. void triggerOnCollisionBegin(const CollisionData& data);
  150. /** Triggered when the internal rigidbody continues touching another object. */
  151. void triggerOnCollisionStay(const CollisionData& data);
  152. /** Triggered when the internal rigidbody ends touching another object. */
  153. void triggerOnCollisionEnd(const CollisionData& data);
  154. /************************************************************************/
  155. /* COMPONENT OVERRIDES */
  156. /************************************************************************/
  157. protected:
  158. friend class SceneObject;
  159. /** @copydoc Component::onInitialized() */
  160. void onInitialized() override;
  161. /** @copydoc Component::onDestroyed() */
  162. void onDestroyed() override;
  163. /** @copydoc Component::onDisabled() */
  164. void onDisabled() override;
  165. /** @copydoc Component::onEnabled() */
  166. void onEnabled() override;
  167. /** @copydoc Component::onTransformChanged() */
  168. void onTransformChanged(TransformChangedFlags flags) override;
  169. SPtr<Rigidbody> mInternal;
  170. Vector<HCollider> mChildren;
  171. HJoint mParentJoint;
  172. UINT32 mPositionSolverCount = 4;
  173. UINT32 mVelocitySolverCount = 1;
  174. Rigidbody::Flag mFlags = (Rigidbody::Flag)((UINT32)Rigidbody::Flag::AutoTensors | (UINT32)Rigidbody::Flag::AutoMass);
  175. Rigidbody::InterpolationMode mInterpolationMode = Rigidbody::InterpolationMode::None;
  176. CollisionReportMode mCollisionReportMode = CollisionReportMode::None;
  177. Vector3 mCMassPosition;
  178. Quaternion mCMassRotation;
  179. Vector3 mInertiaTensor;
  180. float mMass = 0.0f;
  181. float mMaxAngularVelocity = 1.0f;
  182. float mLinearDrag = 0.0f;
  183. float mAngularDrag = 0.0f;
  184. float mSleepThreshold = 0.0f;
  185. bool mUseGravity = true;
  186. bool mIsKinematic = false;
  187. /************************************************************************/
  188. /* RTTI */
  189. /************************************************************************/
  190. public:
  191. friend class CRigidbodyRTTI;
  192. static RTTITypeBase* getRTTIStatic();
  193. RTTITypeBase* getRTTI() const override;
  194. protected:
  195. CRigidbody() {} // Serialization only
  196. };
  197. /** @} */
  198. }