RigidBody.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 "HashSet.h"
  25. #include "Node.h"
  26. #include "PhysicsDefs.h"
  27. class DebugRenderer;
  28. class PhysicsWorld;
  29. /// Physics rigid body component.
  30. class RigidBody : public Component
  31. {
  32. OBJECT(RigidBody);
  33. friend class PhysicsWorld;
  34. public:
  35. /// Construct.
  36. RigidBody(Context* context);
  37. /// Destruct. Free the rigid body and geometries.
  38. virtual ~RigidBody();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// %Set mass. Zero mass (or the lack of collision shapes) makes the body kinematic.
  42. void SetMass(float mass);
  43. /// %Set mass axis for the cylinder and capsule shapes. By default 1 (Y-axis.)
  44. void SetMassAxis(int axis);
  45. /// %Set physics position.
  46. void SetPosition(const Vector3& position);
  47. /// %Set physics rotation.
  48. void SetRotation(const Quaternion& rotation);
  49. /// %Set physics transform.
  50. void SetTransform(const Vector3& position, const Quaternion& rotation);
  51. /// %Set linear velocity.
  52. void SetLinearVelocity(const Vector3& velocity);
  53. /// %Set linear velocity deactivation threshold.
  54. void SetLinearRestThreshold(float threshold);
  55. /// %Set linear velocity damping threshold.
  56. void SetLinearDampingThreshold(float threshold);
  57. /// %Set linear velocity damping scale.
  58. void SetLinearDampingScale(float scale);
  59. /// %Set angular velocity.
  60. void SetAngularVelocity(const Vector3& angularVelocity);
  61. /// %Set angular velocity deactivation threshold.
  62. void SetAngularRestThreshold(float threshold);
  63. /// %Set angular velocity damping threshold.
  64. void SetAngularDampingThreshold(float threshold);
  65. /// %Set angular velocity damping scale.
  66. void SetAngularDampingScale(float scale);
  67. /// %Set maximum angular velocity. Set to 0 to disable rotation.
  68. void SetAngularMaxVelocity(float velocity);
  69. /// %Set rigid body active/inactive state.
  70. void SetActive(bool active);
  71. /// Apply force to center of mass.
  72. void ApplyForce(const Vector3& force);
  73. /// Apply force at position.
  74. void ApplyForceAtPosition(const Vector3& force, const Vector3& position);
  75. /// Apply torque.
  76. void ApplyTorque(const Vector3& torque);
  77. /// Reset accumulated forces.
  78. void ResetForces();
  79. /// Return physics world.
  80. PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  81. /// Return mass.
  82. float GetMass() const { return mass_; }
  83. /// Return mass axis for cylinder and capsule shapes.
  84. int GetMassAxis() const { return massAxis_; }
  85. /// Return ODE body ID.
  86. dBodyID GetBody() const { return body_; }
  87. /// Return physics position.
  88. const Vector3& GetPosition() const;
  89. /// Return physics rotation.
  90. const Quaternion& GetRotation() const;
  91. /// Return linear velocity.
  92. const Vector3& GetLinearVelocity() const;
  93. /// Return linear velocity deactivation threshold.
  94. float GetLinearRestThreshold() const;
  95. /// Return linear velocity damping threshold.
  96. float GetLinearDampingThreshold() const;
  97. /// Return linear velocity damping scale.
  98. float GetLinearDampingScale() const;
  99. /// Return angular velocity.
  100. const Vector3& GetAngularVelocity() const;
  101. /// Return angular velocity deactivation threshold.
  102. float GetAngularRestThreshold() const;
  103. /// Return angular velocity damping threshold.
  104. float GetAngularDampingThreshold() const;
  105. /// Return angular velocity damping scale.
  106. float GetAngularDampingScale() const;
  107. /// Return maximum angular velocity.
  108. float GetAngularMaxVelocity() const;
  109. /// Return whether body is active.
  110. bool IsActive() const;
  111. /// Recalculate mass.
  112. void UpdateMass();
  113. /// %Set network angular velocity attribute.
  114. void SetNetAngularVelocityAttr(const PODVector<unsigned char>& value);
  115. /// Return network angular velocity attribute.
  116. const PODVector<unsigned char>& GetNetAngularVelocityAttr() const;
  117. protected:
  118. /// Handle node being assigned.
  119. virtual void OnNodeSet(Node* node);
  120. /// Handle node transform being dirtied.
  121. virtual void OnMarkedDirty(Node* node);
  122. private:
  123. /// Store previous transform for rendering interpolation.
  124. void PreStep();
  125. /// Interpolate between previous and current transform and store as rendering transform.
  126. void PostStep(float t, HashSet<RigidBody*>& processedBodies);
  127. /// Create the body.
  128. void CreateBody();
  129. /// Remove the body.
  130. void ReleaseBody();
  131. /// Physics world.
  132. WeakPtr<PhysicsWorld> physicsWorld_;
  133. /// Mass.
  134. float mass_;
  135. /// Mass axis for cylinder and capsule shapes.
  136. int massAxis_;
  137. /// ODE body ID.
  138. dBodyID body_;
  139. /// Previous position for rendering interpolation.
  140. Vector3 previousPosition_;
  141. /// Previous rotation for rendering interpolation.
  142. Quaternion previousRotation_;
  143. /// Attribute buffer for network replication.
  144. mutable VectorBuffer attrBuffer_;
  145. /// Poststep flag.
  146. bool inPostStep_;
  147. };