RigidBody2D.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Scene/Component.h"
  6. #include <box2d/box2d.h>
  7. namespace Urho3D
  8. {
  9. class CollisionShape2D;
  10. class Constraint2D;
  11. class PhysicsWorld2D;
  12. /// Rigid body type.
  13. enum BodyType2D
  14. {
  15. BT_STATIC = b2_staticBody,
  16. BT_KINEMATIC = b2_kinematicBody,
  17. BT_DYNAMIC = b2_dynamicBody
  18. };
  19. /// 2D rigid body component.
  20. class URHO3D_API RigidBody2D : public Component
  21. {
  22. URHO3D_OBJECT(RigidBody2D, Component);
  23. public:
  24. /// Construct.
  25. explicit RigidBody2D(Context* context);
  26. /// Destruct.
  27. ~RigidBody2D() override;
  28. /// Register object factory.
  29. /// @nobind
  30. static void RegisterObject(Context* context);
  31. /// Handle enabled/disabled state change.
  32. void OnSetEnabled() override;
  33. /// Set body type.
  34. /// @property
  35. void SetBodyType(BodyType2D type);
  36. /// Set mass.
  37. /// @property
  38. void SetMass(float mass);
  39. /// Set inertia.
  40. /// @property
  41. void SetInertia(float inertia);
  42. /// Set mass center.
  43. /// @property
  44. void SetMassCenter(const Vector2& center);
  45. /// Set whether to automatically calculate mass and inertia from collision shapes. Default true.
  46. /// @property
  47. void SetUseFixtureMass(bool useFixtureMass);
  48. /// Set linear damping.
  49. /// @property
  50. void SetLinearDamping(float linearDamping);
  51. /// Set angular damping.
  52. /// @property
  53. void SetAngularDamping(float angularDamping);
  54. /// Set allow sleep.
  55. /// @property
  56. void SetAllowSleep(bool allowSleep);
  57. /// Set fixed rotation.
  58. /// @property
  59. void SetFixedRotation(bool fixedRotation);
  60. /// Set bullet mode.
  61. /// @property
  62. void SetBullet(bool bullet);
  63. /// Set gravity scale.
  64. /// @property
  65. void SetGravityScale(float gravityScale);
  66. /// Set awake.
  67. /// @property
  68. void SetAwake(bool awake);
  69. /// Set linear velocity.
  70. /// @property
  71. void SetLinearVelocity(const Vector2& linearVelocity);
  72. /// Set angular velocity.
  73. void SetAngularVelocity(float angularVelocity);
  74. /// Apply force.
  75. void ApplyForce(const Vector2& force, const Vector2& point, bool wake);
  76. /// Apply force to center.
  77. void ApplyForceToCenter(const Vector2& force, bool wake);
  78. /// Apply Torque.
  79. void ApplyTorque(float torque, bool wake);
  80. /// Apply linear impulse.
  81. void ApplyLinearImpulse(const Vector2& impulse, const Vector2& point, bool wake);
  82. /// Apply linear impulse to center.
  83. void ApplyLinearImpulseToCenter(const Vector2& impulse, bool wake);
  84. /// Apply angular impulse.
  85. void ApplyAngularImpulse(float impulse, bool wake);
  86. /// Create body.
  87. void CreateBody();
  88. /// Release body.
  89. void ReleaseBody();
  90. /// Apply world transform from the Box2D body. Called by PhysicsWorld2D.
  91. void ApplyWorldTransform();
  92. /// Apply specified world position & rotation. Called by PhysicsWorld2D.
  93. void ApplyWorldTransform(const Vector3& newWorldPosition, const Quaternion& newWorldRotation);
  94. /// Add collision shape.
  95. void AddCollisionShape2D(CollisionShape2D* collisionShape);
  96. /// Remove collision shape.
  97. void RemoveCollisionShape2D(CollisionShape2D* collisionShape);
  98. /// Add constraint.
  99. void AddConstraint2D(Constraint2D* constraint);
  100. /// Remove constraint.
  101. void RemoveConstraint2D(Constraint2D* constraint);
  102. /// Return body type.
  103. /// @property
  104. BodyType2D GetBodyType() const { return body_ ? (BodyType2D)body_->GetType() : (BodyType2D)bodyDef_.type; }
  105. /// Return mass.
  106. /// @property
  107. float GetMass() const;
  108. /// Return inertia.
  109. /// @property
  110. float GetInertia() const;
  111. /// Return mass center.
  112. /// @property
  113. Vector2 GetMassCenter() const;
  114. /// Return whether to calculate mass and inertia from collision shapes automatically.
  115. /// @property
  116. bool GetUseFixtureMass() const { return useFixtureMass_; }
  117. /// Return linear damping.
  118. /// @property
  119. float GetLinearDamping() const { return body_ ? body_->GetLinearDamping() : bodyDef_.linearDamping; }
  120. /// Return angular damping.
  121. /// @property
  122. float GetAngularDamping() const { return body_ ? body_->GetAngularDamping() : bodyDef_.angularDamping; }
  123. /// Return allow sleep.
  124. /// @property
  125. bool IsAllowSleep() const { return body_ ? body_->IsSleepingAllowed() : bodyDef_.allowSleep; }
  126. /// Return fixed rotation.
  127. /// @property
  128. bool IsFixedRotation() const { return body_ ? body_->IsFixedRotation() : bodyDef_.fixedRotation; }
  129. /// Return bullet mode.
  130. /// @property
  131. bool IsBullet() const { return body_ ? body_->IsBullet() : bodyDef_.bullet; }
  132. /// Return gravity scale.
  133. /// @property
  134. float GetGravityScale() const { return body_ ? body_->GetGravityScale() : bodyDef_.gravityScale; }
  135. /// Return awake.
  136. /// @property
  137. bool IsAwake() const;
  138. /// Return linear velocity.
  139. /// @property
  140. Vector2 GetLinearVelocity() const;
  141. /// Return angular velocity.
  142. float GetAngularVelocity() const;
  143. /// Return Box2D body.
  144. b2Body* GetBody() const { return body_; }
  145. private:
  146. /// Handle node being assigned.
  147. void OnNodeSet(Node* node) override;
  148. /// Handle scene being assigned.
  149. void OnSceneSet(Scene* scene) override;
  150. /// Handle node transform being dirtied.
  151. void OnMarkedDirty(Node* node) override;
  152. /// Physics world.
  153. WeakPtr<PhysicsWorld2D> physicsWorld_;
  154. /// Box2D body define.
  155. b2BodyDef bodyDef_;
  156. /// Box2D mass data.
  157. b2MassData massData_;
  158. /// Use fixture mass (calculate mass & inertia from collision shapes automatically).
  159. bool useFixtureMass_;
  160. /// Box2D body.
  161. b2Body* body_;
  162. /// Collision shapes.
  163. Vector<WeakPtr<CollisionShape2D>> collisionShapes_;
  164. /// Constraints.
  165. Vector<WeakPtr<Constraint2D>> constraints_;
  166. };
  167. }