RigidBody2D.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2008-2014 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. #pragma once
  23. #include "Component.h"
  24. #include <Box2D/Box2D.h>
  25. namespace Urho3D
  26. {
  27. class CollisionShape2D;
  28. class Constraint2D;
  29. class PhysicsWorld2D;
  30. /// Rigid body type.
  31. enum BodyType2D
  32. {
  33. BT_STATIC = b2_staticBody,
  34. BT_DYNAMIC = b2_dynamicBody,
  35. BT_KINEMATIC = b2_kinematicBody,
  36. };
  37. /// 2D rigid body component.
  38. class URHO3D_API RigidBody2D : public Component
  39. {
  40. OBJECT(RigidBody2D);
  41. public:
  42. /// Construct.
  43. RigidBody2D(Context* scontext);
  44. /// Destruct.
  45. virtual ~RigidBody2D();
  46. /// Register object factory.
  47. static void RegisterObject(Context* context);
  48. /// Handle enabled/disabled state change.
  49. virtual void OnSetEnabled();
  50. /// Set body type.
  51. void SetBodyType(BodyType2D bodyType);
  52. /// Set Mass.
  53. void SetMass(float mass);
  54. /// Set inertia.
  55. void SetInertia(float inertia);
  56. /// Set mass center.
  57. void SetMassCenter(const Vector2& center);
  58. /// Use fixture mass (default is true).
  59. void SetUseFixtureMass(bool useFixtureMass);
  60. /// Set linear damping.
  61. void SetLinearDamping(float linearDamping);
  62. /// Set angular damping.
  63. void SetAngularDamping(float angularDamping);
  64. /// Set allow sleep.
  65. void SetAllowSleep(bool allowSleep);
  66. /// Set fixed rotation.
  67. void SetFixedRotation(bool fixedRotation);
  68. /// Set bullet.
  69. void SetBullet(bool bullet);
  70. /// Set gravity scale.
  71. void SetGravityScale(float gravityScale);
  72. /// Set awake.
  73. void SetAwake(bool awake);
  74. /// Set linear velocity.
  75. void SetLinearVelocity(const Vector2& linearVelocity);
  76. /// Set angular velocity.
  77. void SetAngularVelocity(float angularVelocity);
  78. /// Apply force.
  79. void ApplyForce(const Vector2& force, const Vector2& point, bool wake);
  80. /// Apply force to center.
  81. void ApplyForceToCenter(const Vector2& force, bool wake);
  82. /// Apply Torque.
  83. void ApplyTorque(float torque, bool wake);
  84. /// Apply linear impulse.
  85. void ApplyLinearImpulse(const Vector2& impulse, const Vector2& point, bool wake);
  86. /// Apply angular impulse.
  87. void ApplyAngularImpulse(float impulse, bool wake);
  88. /// Create body.
  89. void CreateBody();
  90. /// Release body.
  91. void ReleaseBody();
  92. /// Apply world transform.
  93. void ApplyWorldTransform();
  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. BodyType2D GetBodyType() const { return (BodyType2D)bodyDef_.type; }
  104. /// Return Mass.
  105. float GetMass() const;
  106. /// Return inertia.
  107. float GetInertia() const;
  108. /// Return mass center.
  109. Vector2 GetMassCenter() const;
  110. /// Return use fixture mass.
  111. bool GetUseFixtureMass() const { return useFixtureMass_; }
  112. /// Return linear damping.
  113. float GetLinearDamping() const { return bodyDef_.linearDamping; }
  114. /// Return angular damping.
  115. float GetAngularDamping() const { return bodyDef_.angularDamping; }
  116. /// Return allow sleep.
  117. bool IsAllowSleep() const { return bodyDef_.allowSleep; }
  118. /// Return fixed rotation.
  119. bool IsFixedRotation() const { return bodyDef_.fixedRotation; }
  120. /// Return bullet.
  121. bool IsBullet() const { return bodyDef_.bullet; }
  122. /// Return gravity scale.
  123. float GetGravityScale() const { return bodyDef_.gravityScale; }
  124. /// Return awake.
  125. bool IsAwake() const;
  126. /// Return linear velocity.
  127. Vector2 GetLinearVelocity() const;
  128. /// Return angular velocity.
  129. float GetAngularVelocity() const;
  130. /// Return Box2D body.
  131. b2Body* GetBody() const { return body_; }
  132. private:
  133. /// Handle node being assigned.
  134. virtual void OnNodeSet(Node* node);
  135. /// Handle node transform being dirtied.
  136. virtual void OnMarkedDirty(Node* node);
  137. /// Physics world.
  138. WeakPtr<PhysicsWorld2D> physicsWorld_;
  139. /// Box2D body define.
  140. b2BodyDef bodyDef_;
  141. /// Box2D mass data.
  142. b2MassData massData_;
  143. /// Use fixture mass.
  144. bool useFixtureMass_;
  145. /// Box2D body.
  146. b2Body* body_;
  147. /// Collision shapes.
  148. Vector<WeakPtr<CollisionShape2D> > collisionShapes_;
  149. /// Constraints.
  150. Vector<WeakPtr<Constraint2D> > constraints_;
  151. };
  152. }