RigidBody2D.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. void SetBodyType(BodyType2D type);
  53. /// Set mass.
  54. void SetMass(float mass);
  55. /// Set inertia.
  56. void SetInertia(float inertia);
  57. /// Set mass center.
  58. void SetMassCenter(const Vector2& center);
  59. /// Set whether to automatically calculate mass and inertia from collision shapes. Default true.
  60. void SetUseFixtureMass(bool useFixtureMass);
  61. /// Set linear damping.
  62. void SetLinearDamping(float linearDamping);
  63. /// Set angular damping.
  64. void SetAngularDamping(float angularDamping);
  65. /// Set allow sleep.
  66. void SetAllowSleep(bool allowSleep);
  67. /// Set fixed rotation.
  68. void SetFixedRotation(bool fixedRotation);
  69. /// Set bullet mode.
  70. void SetBullet(bool bullet);
  71. /// Set gravity scale.
  72. void SetGravityScale(float gravityScale);
  73. /// Set awake.
  74. void SetAwake(bool awake);
  75. /// Set linear velocity.
  76. void SetLinearVelocity(const Vector2& linearVelocity);
  77. /// Set angular velocity.
  78. void SetAngularVelocity(float angularVelocity);
  79. /// Apply force.
  80. void ApplyForce(const Vector2& force, const Vector2& point, bool wake);
  81. /// Apply force to center.
  82. void ApplyForceToCenter(const Vector2& force, bool wake);
  83. /// Apply Torque.
  84. void ApplyTorque(float torque, bool wake);
  85. /// Apply linear impulse.
  86. void ApplyLinearImpulse(const Vector2& impulse, const Vector2& point, bool wake);
  87. /// Apply linear impulse to center.
  88. void ApplyLinearImpulseToCenter(const Vector2& impulse, bool wake);
  89. /// Apply angular impulse.
  90. void ApplyAngularImpulse(float impulse, bool wake);
  91. /// Create body.
  92. void CreateBody();
  93. /// Release body.
  94. void ReleaseBody();
  95. /// Apply world transform from the Box2D body. Called by PhysicsWorld2D.
  96. void ApplyWorldTransform();
  97. /// Apply specified world position & rotation. Called by PhysicsWorld2D.
  98. void ApplyWorldTransform(const Vector3& newWorldPosition, const Quaternion& newWorldRotation);
  99. /// Add collision shape.
  100. void AddCollisionShape2D(CollisionShape2D* collisionShape);
  101. /// Remove collision shape.
  102. void RemoveCollisionShape2D(CollisionShape2D* collisionShape);
  103. /// Add constraint.
  104. void AddConstraint2D(Constraint2D* constraint);
  105. /// Remove constraint.
  106. void RemoveConstraint2D(Constraint2D* constraint);
  107. /// Return body type.
  108. BodyType2D GetBodyType() const { return body_ ? (BodyType2D)body_->GetType() : (BodyType2D)bodyDef_.type; }
  109. /// Return mass.
  110. float GetMass() const;
  111. /// Return inertia.
  112. float GetInertia() const;
  113. /// Return mass center.
  114. Vector2 GetMassCenter() const;
  115. /// Return whether to calculate mass and inertia from collision shapes automatically.
  116. bool GetUseFixtureMass() const { return useFixtureMass_; }
  117. /// Return linear damping.
  118. float GetLinearDamping() const { return body_ ? body_->GetLinearDamping() : bodyDef_.linearDamping; }
  119. /// Return angular damping.
  120. float GetAngularDamping() const { return body_ ? body_->GetAngularDamping() : bodyDef_.angularDamping; }
  121. /// Return allow sleep.
  122. bool IsAllowSleep() const { return body_ ? body_->IsSleepingAllowed() : bodyDef_.allowSleep; }
  123. /// Return fixed rotation.
  124. bool IsFixedRotation() const { return body_ ? body_->IsFixedRotation() : bodyDef_.fixedRotation; }
  125. /// Return bullet mode.
  126. bool IsBullet() const { return body_ ? body_->IsBullet() : bodyDef_.bullet; }
  127. /// Return gravity scale.
  128. float GetGravityScale() const { return body_ ? body_->GetGravityScale() : bodyDef_.gravityScale; }
  129. /// Return awake.
  130. bool IsAwake() const;
  131. /// Return linear velocity.
  132. Vector2 GetLinearVelocity() const;
  133. /// Return angular velocity.
  134. float GetAngularVelocity() const;
  135. /// Return Box2D body.
  136. b2Body* GetBody() const { return body_; }
  137. private:
  138. /// Handle node being assigned.
  139. void OnNodeSet(Node* node) override;
  140. /// Handle scene being assigned.
  141. void OnSceneSet(Scene* scene) override;
  142. /// Handle node transform being dirtied.
  143. void OnMarkedDirty(Node* node) override;
  144. /// Physics world.
  145. WeakPtr<PhysicsWorld2D> physicsWorld_;
  146. /// Box2D body define.
  147. b2BodyDef bodyDef_;
  148. /// Box2D mass data.
  149. b2MassData massData_;
  150. /// Use fixture mass (calculate mass & inertia from collision shapes automatically).
  151. bool useFixtureMass_;
  152. /// Box2D body.
  153. b2Body* body_;
  154. /// Collision shapes.
  155. Vector<WeakPtr<CollisionShape2D> > collisionShapes_;
  156. /// Constraints.
  157. Vector<WeakPtr<Constraint2D> > constraints_;
  158. };
  159. }