RigidBody2D.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. /// \file
  23. #pragma once
  24. #include "../Scene/Component.h"
  25. #include <Box2D/Box2D.h>
  26. namespace Urho3D
  27. {
  28. class CollisionShape2D;
  29. class Constraint2D;
  30. class PhysicsWorld2D;
  31. /// Rigid body type.
  32. enum BodyType2D
  33. {
  34. BT_STATIC = b2_staticBody,
  35. BT_KINEMATIC = b2_kinematicBody,
  36. BT_DYNAMIC = b2_dynamicBody
  37. };
  38. /// 2D rigid body component.
  39. class URHO3D_API RigidBody2D : public Component
  40. {
  41. URHO3D_OBJECT(RigidBody2D, Component);
  42. public:
  43. /// Construct.
  44. explicit RigidBody2D(Context* context);
  45. /// Destruct.
  46. ~RigidBody2D() override;
  47. /// Register object factory.
  48. static void RegisterObject(Context* context);
  49. /// Handle enabled/disabled state change.
  50. void OnSetEnabled() override;
  51. /// Set body type.
  52. /// @property
  53. void SetBodyType(BodyType2D type);
  54. /// Set mass.
  55. /// @property
  56. void SetMass(float mass);
  57. /// Set inertia.
  58. /// @property
  59. void SetInertia(float inertia);
  60. /// Set mass center.
  61. /// @property
  62. void SetMassCenter(const Vector2& center);
  63. /// Set whether to automatically calculate mass and inertia from collision shapes. Default true.
  64. /// @property
  65. void SetUseFixtureMass(bool useFixtureMass);
  66. /// Set linear damping.
  67. /// @property
  68. void SetLinearDamping(float linearDamping);
  69. /// Set angular damping.
  70. /// @property
  71. void SetAngularDamping(float angularDamping);
  72. /// Set allow sleep.
  73. /// @property
  74. void SetAllowSleep(bool allowSleep);
  75. /// Set fixed rotation.
  76. /// @property
  77. void SetFixedRotation(bool fixedRotation);
  78. /// Set bullet mode.
  79. /// @property
  80. void SetBullet(bool bullet);
  81. /// Set gravity scale.
  82. /// @property
  83. void SetGravityScale(float gravityScale);
  84. /// Set awake.
  85. /// @property
  86. void SetAwake(bool awake);
  87. /// Set linear velocity.
  88. /// @property
  89. void SetLinearVelocity(const Vector2& linearVelocity);
  90. /// Set angular velocity.
  91. void SetAngularVelocity(float angularVelocity);
  92. /// Apply force.
  93. void ApplyForce(const Vector2& force, const Vector2& point, bool wake);
  94. /// Apply force to center.
  95. void ApplyForceToCenter(const Vector2& force, bool wake);
  96. /// Apply Torque.
  97. void ApplyTorque(float torque, bool wake);
  98. /// Apply linear impulse.
  99. void ApplyLinearImpulse(const Vector2& impulse, const Vector2& point, bool wake);
  100. /// Apply linear impulse to center.
  101. void ApplyLinearImpulseToCenter(const Vector2& impulse, bool wake);
  102. /// Apply angular impulse.
  103. void ApplyAngularImpulse(float impulse, bool wake);
  104. /// Create body.
  105. void CreateBody();
  106. /// Release body.
  107. void ReleaseBody();
  108. /// Apply world transform from the Box2D body. Called by PhysicsWorld2D.
  109. void ApplyWorldTransform();
  110. /// Apply specified world position & rotation. Called by PhysicsWorld2D.
  111. void ApplyWorldTransform(const Vector3& newWorldPosition, const Quaternion& newWorldRotation);
  112. /// Add collision shape.
  113. void AddCollisionShape2D(CollisionShape2D* collisionShape);
  114. /// Remove collision shape.
  115. void RemoveCollisionShape2D(CollisionShape2D* collisionShape);
  116. /// Add constraint.
  117. void AddConstraint2D(Constraint2D* constraint);
  118. /// Remove constraint.
  119. void RemoveConstraint2D(Constraint2D* constraint);
  120. /// Return body type.
  121. /// @property
  122. BodyType2D GetBodyType() const { return body_ ? (BodyType2D)body_->GetType() : (BodyType2D)bodyDef_.type; }
  123. /// Return mass.
  124. /// @property
  125. float GetMass() const;
  126. /// Return inertia.
  127. /// @property
  128. float GetInertia() const;
  129. /// Return mass center.
  130. /// @property
  131. Vector2 GetMassCenter() const;
  132. /// Return whether to calculate mass and inertia from collision shapes automatically.
  133. /// @property
  134. bool GetUseFixtureMass() const { return useFixtureMass_; }
  135. /// Return linear damping.
  136. /// @property
  137. float GetLinearDamping() const { return body_ ? body_->GetLinearDamping() : bodyDef_.linearDamping; }
  138. /// Return angular damping.
  139. /// @property
  140. float GetAngularDamping() const { return body_ ? body_->GetAngularDamping() : bodyDef_.angularDamping; }
  141. /// Return allow sleep.
  142. /// @property
  143. bool IsAllowSleep() const { return body_ ? body_->IsSleepingAllowed() : bodyDef_.allowSleep; }
  144. /// Return fixed rotation.
  145. /// @property
  146. bool IsFixedRotation() const { return body_ ? body_->IsFixedRotation() : bodyDef_.fixedRotation; }
  147. /// Return bullet mode.
  148. /// @property
  149. bool IsBullet() const { return body_ ? body_->IsBullet() : bodyDef_.bullet; }
  150. /// Return gravity scale.
  151. /// @property
  152. float GetGravityScale() const { return body_ ? body_->GetGravityScale() : bodyDef_.gravityScale; }
  153. /// Return awake.
  154. /// @property
  155. bool IsAwake() const;
  156. /// Return linear velocity.
  157. /// @property
  158. Vector2 GetLinearVelocity() const;
  159. /// Return angular velocity.
  160. float GetAngularVelocity() const;
  161. /// Return Box2D body.
  162. b2Body* GetBody() const { return body_; }
  163. private:
  164. /// Handle node being assigned.
  165. void OnNodeSet(Node* node) override;
  166. /// Handle scene being assigned.
  167. void OnSceneSet(Scene* scene) override;
  168. /// Handle node transform being dirtied.
  169. void OnMarkedDirty(Node* node) override;
  170. /// Physics world.
  171. WeakPtr<PhysicsWorld2D> physicsWorld_;
  172. /// Box2D body define.
  173. b2BodyDef bodyDef_;
  174. /// Box2D mass data.
  175. b2MassData massData_;
  176. /// Use fixture mass (calculate mass & inertia from collision shapes automatically).
  177. bool useFixtureMass_;
  178. /// Box2D body.
  179. b2Body* body_;
  180. /// Collision shapes.
  181. Vector<WeakPtr<CollisionShape2D> > collisionShapes_;
  182. /// Constraints.
  183. Vector<WeakPtr<Constraint2D> > constraints_;
  184. };
  185. }