RigidBody2D.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Copyright (c) 2008-2017 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 "../Scene/Component.h"
  24. #include <Box2D/Box2D.h>
  25. namespace Atomic
  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_KINEMATIC = b2_kinematicBody,
  35. BT_DYNAMIC = b2_dynamicBody
  36. };
  37. /// 2D rigid body component.
  38. class ATOMIC_API RigidBody2D : public Component
  39. {
  40. ATOMIC_OBJECT(RigidBody2D, Component);
  41. public:
  42. /// Construct.
  43. RigidBody2D(Context* context);
  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. /// Set whether to automatically calculate mass and inertia from collision shapes. Default 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 mode.
  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 linear impulse to center.
  87. void ApplyLinearImpulseToCenter(const Vector2& impulse, bool wake);
  88. /// Apply angular impulse.
  89. void ApplyAngularImpulse(float impulse, bool wake);
  90. /// Create body.
  91. void CreateBody();
  92. /// Release body.
  93. void ReleaseBody();
  94. /// Apply world transform from the Box2D body. Called by PhysicsWorld2D.
  95. void ApplyWorldTransform();
  96. /// Apply specified world position & rotation. Called by PhysicsWorld2D.
  97. void ApplyWorldTransform(const Vector3& newWorldPosition, const Quaternion& newWorldRotation);
  98. /// Add collision shape.
  99. void AddCollisionShape2D(CollisionShape2D* collisionShape);
  100. /// Remove collision shape.
  101. void RemoveCollisionShape2D(CollisionShape2D* collisionShape);
  102. /// Add constraint.
  103. void AddConstraint2D(Constraint2D* constraint);
  104. /// Remove constraint.
  105. void RemoveConstraint2D(Constraint2D* constraint);
  106. /// Return body type.
  107. BodyType2D GetBodyType() const { return body_ ? (BodyType2D)body_->GetType() : (BodyType2D)bodyDef_.type; }
  108. /// Return mass.
  109. float GetMass() const;
  110. /// Return inertia.
  111. float GetInertia() const;
  112. /// Return mass center.
  113. Vector2 GetMassCenter() const;
  114. /// Return whether to calculate mass and inertia from collision shapes automatically.
  115. bool GetUseFixtureMass() const { return useFixtureMass_; }
  116. /// Return linear damping.
  117. float GetLinearDamping() const { return body_ ? body_->GetLinearDamping() : bodyDef_.linearDamping; }
  118. /// Return angular damping.
  119. float GetAngularDamping() const { return body_ ? body_->GetAngularDamping() : bodyDef_.angularDamping; }
  120. /// Return allow sleep.
  121. bool IsAllowSleep() const { return body_ ? body_->IsSleepingAllowed() : bodyDef_.allowSleep; }
  122. /// Return fixed rotation.
  123. bool IsFixedRotation() const { return body_ ? body_->IsFixedRotation() : bodyDef_.fixedRotation; }
  124. /// Return bullet mode.
  125. bool IsBullet() const { return body_ ? body_->IsBullet() : bodyDef_.bullet; }
  126. /// Return gravity scale.
  127. float GetGravityScale() const { return body_ ? body_->GetGravityScale() : bodyDef_.gravityScale; }
  128. /// Return awake.
  129. bool IsAwake() const;
  130. /// Return linear velocity.
  131. Vector2 GetLinearVelocity() const;
  132. /// Return angular velocity.
  133. float GetAngularVelocity() const;
  134. /// Return Box2D body.
  135. b2Body* GetBody() const { return body_; }
  136. // ATOMIC BEGIN
  137. bool GetCastShadows() const { return castShadows_; }
  138. void SetCastShadows(bool castShadows) { castShadows_ = castShadows; }
  139. // ATOMIC END
  140. private:
  141. /// Handle node being assigned.
  142. virtual void OnNodeSet(Node* node);
  143. /// Handle scene being assigned.
  144. virtual void OnSceneSet(Scene* scene);
  145. /// Handle node transform being dirtied.
  146. virtual void OnMarkedDirty(Node* node);
  147. /// Physics world.
  148. WeakPtr<PhysicsWorld2D> physicsWorld_;
  149. /// Box2D body define.
  150. b2BodyDef bodyDef_;
  151. /// Box2D mass data.
  152. b2MassData massData_;
  153. /// Use fixture mass (calculate mass & inertia from collision shapes automatically.)
  154. bool useFixtureMass_;
  155. /// Box2D body.
  156. b2Body* body_;
  157. /// Collision shapes.
  158. Vector<WeakPtr<CollisionShape2D> > collisionShapes_;
  159. /// Constraints.
  160. Vector<WeakPtr<Constraint2D> > constraints_;
  161. // ATOMIC BEGIN
  162. bool castShadows_;
  163. // ATOMIC END
  164. };
  165. }