RigidBody.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Node.h"
  25. #include <LinearMath/btMotionState.h>
  26. class CollisionShape;
  27. class DebugRenderer;
  28. class PhysicsWorld;
  29. class SmoothedTransform;
  30. class btCompoundShape;
  31. class btRigidBody;
  32. /// Rigid body collision event signaling mode.
  33. enum CollisionEventMode
  34. {
  35. COLLISION_NEVER = 0,
  36. COLLISION_ACTIVE,
  37. COLLISION_ALWAYS
  38. };
  39. /// Physics rigid body component.
  40. class RigidBody : public Component, public btMotionState
  41. {
  42. OBJECT(RigidBody);
  43. friend class PhysicsWorld;
  44. public:
  45. /// Construct.
  46. RigidBody(Context* context);
  47. /// Destruct. Free the rigid body and geometries.
  48. virtual ~RigidBody();
  49. /// Register object factory.
  50. static void RegisterObject(Context* context);
  51. /// Handle attribute write access.
  52. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  53. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  54. virtual void ApplyAttributes();
  55. /// Return initial world transform to Bullet.
  56. virtual void getWorldTransform(btTransform &worldTrans) const;
  57. /// Update world transform from Bullet.
  58. virtual void setWorldTransform(const btTransform &worldTrans);
  59. /// %Set mass. Zero mass makes the body static.
  60. void SetMass(float mass);
  61. /// %Set rigid body world-space position.
  62. void SetPosition(Vector3 position);
  63. /// %Set rigid body world-space rotation.
  64. void SetRotation(Quaternion rotation);
  65. /// %Set rigid body world-space position and rotation.
  66. void SetTransform(const Vector3& position, const Quaternion& rotation);
  67. /// %Set linear velocity.
  68. void SetLinearVelocity(Vector3 velocity);
  69. /// %Set linear degrees of freedom.
  70. void SetLinearFactor(Vector3 factor);
  71. /// %Set linear velocity deactivation threshold.
  72. void SetLinearRestThreshold(float threshold);
  73. /// %Set linear velocity damping factor.
  74. void SetLinearDamping(float damping);
  75. /// %Set angular velocity.
  76. void SetAngularVelocity(Vector3 angularVelocity);
  77. /// %Set angular degrees of freedom.
  78. void SetAngularFactor(Vector3 factor);
  79. /// %Set angular velocity deactivation threshold.
  80. void SetAngularRestThreshold(float threshold);
  81. /// %Set angular velocity damping factor.
  82. void SetAngularDamping(float factor);
  83. /// %Set friction coefficient.
  84. void SetFriction(float friction);
  85. /// %Set restitution coefficient.
  86. void SetRestitution(float restitution);
  87. /// %Set continuous collision detection swept sphere radius.
  88. void SetCcdRadius(float radius);
  89. /// %Set continuous collision detection motion-per-simulation-step threshold. 0 disables, which is the default.
  90. void SetCcdMotionThreshold(float threshold);
  91. /// %Set whether gravity is applied to rigid body.
  92. void SetUseGravity(bool enable);
  93. /// %Set rigid body kinematic mode. In kinematic mode forces are not applied to the rigid body.
  94. void SetKinematic(bool enable);
  95. /// %Set rigid body phantom mode. In phantom mode collisions are reported but do not apply forces.
  96. void SetPhantom(bool enable);
  97. /// %Set collision layer.
  98. void SetCollisionLayer(unsigned layer);
  99. /// %Set collision mask.
  100. void SetCollisionMask(unsigned mask);
  101. /// %Set collision group and mask.
  102. void SetCollisionLayerAndMask(unsigned layer, unsigned mask);
  103. /// %Set collision event signaling mode. Default is to signal when active.
  104. void SetCollisionEventMode(CollisionEventMode mode);
  105. /// Apply force to center of mass.
  106. void ApplyForce(const Vector3& force);
  107. /// Apply force at position.
  108. void ApplyForce(const Vector3& force, const Vector3& position);
  109. /// Apply torque.
  110. void ApplyTorque(const Vector3& torque);
  111. /// Apply impulse to center of mass.
  112. void ApplyImpulse(const Vector3& impulse);
  113. /// Apply impulse at position.
  114. void ApplyImpulse(const Vector3& impulse, const Vector3& position);
  115. /// Apply torque impulse.
  116. void ApplyTorqueImpulse(const Vector3& torque);
  117. /// Reset accumulated forces.
  118. void ResetForces();
  119. /// Activate rigid body if it was resting.
  120. void Activate();
  121. /// Return physics world.
  122. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  123. /// Return Bullet rigid body.
  124. btRigidBody* GetBody() const { return body_; }
  125. /// Return Bullet compound collision shape.
  126. btCompoundShape* GetCompoundShape() const { return compoundShape_; }
  127. /// Return mass.
  128. float GetMass() const { return mass_; }
  129. /// Return rigid body world-space position.
  130. Vector3 GetPosition() const;
  131. /// Return rigid body world-space rotation.
  132. Quaternion GetRotation() const;
  133. /// Return linear velocity.
  134. Vector3 GetLinearVelocity() const;
  135. /// Return linear degrees of freedom.
  136. Vector3 GetLinearFactor() const;
  137. /// Return linear velocity deactivation threshold.
  138. float GetLinearRestThreshold() const;
  139. /// Return linear velocity damping threshold.
  140. float GetLinearDamping() const;
  141. /// Return linear velocity damping scale.
  142. float GetLinearDampingScale() const;
  143. /// Return angular velocity.
  144. Vector3 GetAngularVelocity() const;
  145. /// Return angular degrees of freedom.
  146. Vector3 GetAngularFactor() const;
  147. /// Return angular velocity deactivation threshold.
  148. float GetAngularRestThreshold() const;
  149. /// Return angular velocity damping threshold.
  150. float GetAngularDamping() const;
  151. /// Return angular velocity damping scale.
  152. float GetAngularDampingScale() const;
  153. /// Return friction coefficient.
  154. float GetFriction() const;
  155. /// Return restitution coefficient.
  156. float GetRestitution() const;
  157. /// Return continuous collision detection swept sphere radius.
  158. float GetCcdRadius() const;
  159. /// Return continuous collision detection motion-per-simulation-step threshold.
  160. float GetCcdMotionThreshold() const;
  161. /// Return whether rigid body uses gravity.
  162. bool GetUseGravity() const;
  163. /// Return kinematic mode flag.
  164. bool IsKinematic() const { return kinematic_; }
  165. /// Return phantom mode flag.
  166. bool IsPhantom() const { return phantom_; }
  167. /// Return whether rigid body is active.
  168. bool IsActive() const;
  169. /// Return collision layer.
  170. unsigned GetCollisionLayer() const { return collisionLayer_; }
  171. /// Return collision mask.
  172. unsigned GetCollisionMask() const { return collisionMask_; }
  173. /// Return collision event signaling mode.
  174. CollisionEventMode GetCollisionEventMode() const { return collisionEventMode_; }
  175. /// Apply new world transform after a simulation step. Called internally.
  176. void ApplyWorldTransform(const Vector3& newWorldPosition, const Quaternion& newWorldRotation);
  177. /// Update mass and inertia of rigid body.
  178. void UpdateMass();
  179. /// Add debug geometry to the debug renderer.
  180. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  181. /// %Set network angular velocity attribute.
  182. void SetNetAngularVelocityAttr(const PODVector<unsigned char>& value);
  183. /// Return network angular velocity attribute.
  184. const PODVector<unsigned char>& GetNetAngularVelocityAttr() const;
  185. protected:
  186. /// Handle node being assigned.
  187. virtual void OnNodeSet(Node* node);
  188. /// Handle node transform being dirtied.
  189. virtual void OnMarkedDirty(Node* node);
  190. private:
  191. /// Create the rigid body, or re-add to the physics world with changed flags. Calls UpdateMass().
  192. void AddBodyToWorld();
  193. /// Remove the rigid body.
  194. void ReleaseBody();
  195. /// Handle SmoothedTransform target position update.
  196. void HandleTargetPosition(StringHash eventType, VariantMap& eventData);
  197. /// Handle SmoothedTransform target rotation update.
  198. void HandleTargetRotation(StringHash eventType, VariantMap& eventData);
  199. /// Bullet rigid body.
  200. btRigidBody* body_;
  201. /// Bullet compound collision shape.
  202. btCompoundShape* compoundShape_;
  203. /// Physics world.
  204. WeakPtr<PhysicsWorld> physicsWorld_;
  205. /// Mass.
  206. float mass_;
  207. /// Attribute buffer for network replication.
  208. mutable VectorBuffer attrBuffer_;
  209. /// Collision layer.
  210. unsigned collisionLayer_;
  211. /// Collision mask.
  212. unsigned collisionMask_;
  213. /// Collision event signaling mode.
  214. CollisionEventMode collisionEventMode_;
  215. /// Last interpolated position from the simulation.
  216. mutable Vector3 lastPosition_;
  217. /// Last interpolated rotation from the simulation.
  218. mutable Quaternion lastRotation_;
  219. /// Kinematic flag.
  220. bool kinematic_;
  221. /// Phantom flag.
  222. bool phantom_;
  223. /// Whether is in Bullet's transform update. Node dirtying is ignored at this point to prevent endless recursion.
  224. bool inSetTransform_;
  225. /// Smoothed transform mode.
  226. bool hasSmoothedTransform_;
  227. /// Dirty flag.
  228. bool dirty_;
  229. };