PhysicsWorld2D.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Camera;
  28. class RigidBody2D;
  29. /// 2D Physics raycast hit.
  30. struct URHO3D_API PhysicsRaycastResult2D
  31. {
  32. /// Construct with defaults.
  33. PhysicsRaycastResult2D() : body_(0)
  34. {
  35. }
  36. /// Test for inequality, added to prevent GCC from complaining.
  37. bool operator != (const PhysicsRaycastResult2D& rhs) const { return position_ != rhs.position_ || normal_ != rhs.normal_ || distance_ != rhs.distance_ || body_ != rhs.body_; }
  38. /// Hit worldspace position.
  39. Vector2 position_;
  40. /// Hit worldspace normal.
  41. Vector2 normal_;
  42. /// Hit distance from ray origin.
  43. float distance_;
  44. /// Rigid body that was hit.
  45. RigidBody2D* body_;
  46. };
  47. /// 2D physics simulation world component. Should be added only to the root scene node.
  48. class URHO3D_API PhysicsWorld2D : public Component, public b2ContactListener, public b2Draw
  49. {
  50. OBJECT(PhysicsWorld2D);
  51. public:
  52. /// Construct.
  53. PhysicsWorld2D(Context* scontext);
  54. /// Destruct.
  55. virtual ~PhysicsWorld2D();
  56. /// Register object factory.
  57. static void RegisterObject(Context* context);
  58. /// Visualize the component as debug geometry.
  59. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  60. // Implement b2ContactListener.
  61. /// Called when two fixtures begin to touch.
  62. virtual void BeginContact(b2Contact* contact);
  63. /// Called when two fixtures cease to touch.
  64. virtual void EndContact(b2Contact* contact);
  65. // Implement b2Draw.
  66. /// Draw a closed polygon provided in CCW order.
  67. virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
  68. /// Draw a solid closed polygon provided in CCW order.
  69. virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
  70. /// Draw a circle.
  71. virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
  72. /// Draw a solid circle.
  73. virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
  74. /// Draw a line segment.
  75. virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
  76. /// Draw a transform. Choose your own length scale.
  77. virtual void DrawTransform(const b2Transform& xf);
  78. /// Step the simulation forward.
  79. void Update(float timeStep);
  80. /// Add debug geometry to the debug renderer.
  81. void DrawDebugGeometry();
  82. /// Set draw shape.
  83. void SetDrawShape(bool drawShape);
  84. /// Set draw joint.
  85. void SetDrawJoint(bool drawJoint);
  86. /// Set draw aabb.
  87. void SetDrawAabb(bool drawAabb);
  88. /// Set draw pair.
  89. void SetDrawPair(bool drawPair);
  90. /// Set draw center of mass.
  91. void SetDrawCenterOfMass(bool drawCenterOfMass);
  92. /// Set allow sleeping.
  93. void SetAllowSleeping(bool enable);
  94. /// Set warm starting.
  95. void SetWarmStarting(bool enable);
  96. /// Set continuous physics.
  97. void SetContinuousPhysics(bool enable);
  98. /// Set sub stepping.
  99. void SetSubStepping(bool enable);
  100. /// Set gravity.
  101. void SetGravity(const Vector2& gravity);
  102. /// Set auto clear forces.
  103. void SetAutoClearForces(bool enable);
  104. /// Set velocity iterations.
  105. void SetVelocityIterations(int velocityIterations);
  106. /// Set position iterations.
  107. void SetPositionIterations(int positionIterations);
  108. /// Add rigid body.
  109. void AddRigidBody(RigidBody2D* rigidBody);
  110. /// Remove rigid body.
  111. void RemoveRigidBody(RigidBody2D* rigidBody);
  112. /// Perform a physics world raycast and return all hits.
  113. void Raycast(PODVector<PhysicsRaycastResult2D>& results, const Vector2& startPoint, const Vector2& endPoint, unsigned collisionMask = M_MAX_UNSIGNED);
  114. /// Perform a physics world raycast and return the closest hit.
  115. void RaycastSingle(PhysicsRaycastResult2D& result, const Vector2& startPoint, const Vector2& endPoint, unsigned collisionMask = M_MAX_UNSIGNED);
  116. /// Return rigid body at point.
  117. RigidBody2D* GetRigidBody(const Vector2& point, unsigned collisionMask = M_MAX_UNSIGNED);
  118. /// Return rigid body at screen point.
  119. RigidBody2D* GetRigidBody(int screenX, int screenY, unsigned collisionMask = M_MAX_UNSIGNED);
  120. /// Return rigid bodies by a box query.
  121. void GetRigidBodies(PODVector<RigidBody2D*>& result, const Rect& aabb, unsigned collisionMask = M_MAX_UNSIGNED);
  122. /// Return draw shape.
  123. bool GetDrawShape() const { return (m_drawFlags & e_shapeBit) != 0; }
  124. /// Return draw joint.
  125. bool GetDrawJoint() const { return (m_drawFlags & e_jointBit) != 0; }
  126. /// Return draw aabb.
  127. bool GetDrawAabb() const { return (m_drawFlags & e_aabbBit) != 0; }
  128. /// Return draw pair.
  129. bool GetDrawPair() const { return (m_drawFlags & e_pairBit) != 0; }
  130. /// Return draw center of mass.
  131. bool GetDrawCenterOfMass() const { return (m_drawFlags & e_centerOfMassBit) != 0; }
  132. /// Return allow sleeping.
  133. bool GetAllowSleeping() const;
  134. /// Return warm starting.
  135. bool GetWarmStarting() const;
  136. /// Return continuous physics.
  137. bool GetContinuousPhysics() const;
  138. /// Return sub stepping.
  139. bool GetSubStepping() const;
  140. /// Return auto clear forces.
  141. bool GetAutoClearForces() const;
  142. /// Return gravity.
  143. const Vector2& GetGravity() const { return gravity_; }
  144. /// Return velocity iterations.
  145. int GetVelocityIterations() const { return velocityIterations_; }
  146. /// Return position iterations.
  147. int GetPositionIterations() const { return positionIterations_; }
  148. /// Return the Box2D physics world.
  149. b2World* GetWorld() { return world_; }
  150. /// Set node dirtying to be disregarded.
  151. void SetApplyingTransforms(bool enable) { applyingTransforms_ = enable; }
  152. /// Return whether node dirtying should be disregarded.
  153. bool IsApplyingTransforms() const { return applyingTransforms_; }
  154. protected:
  155. /// Handle node being assigned.
  156. virtual void OnNodeSet(Node* node);
  157. private:
  158. /// Handle the scene subsystem update event, step simulation here.
  159. void HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData);
  160. /// Send begin contact events.
  161. void SendBeginContactEvents();
  162. /// Send end contact events.
  163. void SendEndContactEvents();
  164. /// Box2D physics world.
  165. b2World* world_;
  166. /// Gravity.
  167. Vector2 gravity_;
  168. /// Velocity iterations.
  169. int velocityIterations_;
  170. /// Position iterations.
  171. int positionIterations_;
  172. /// Extra weak pointer to scene to allow for cleanup in case the world is destroyed before other components.
  173. WeakPtr<Scene> scene_;
  174. /// Debug renderer.
  175. DebugRenderer* debugRenderer_;
  176. /// Debug draw depth test mode.
  177. bool debugDepthTest_;
  178. /// Physics steping.
  179. bool physicsSteping_;
  180. /// Applying transforms.
  181. bool applyingTransforms_;
  182. /// Rigid bodies.
  183. Vector<WeakPtr<RigidBody2D> > rigidBodies_;
  184. /// Contact info.
  185. struct ContactInfo
  186. {
  187. /// Construct.
  188. ContactInfo();
  189. /// Construct.
  190. ContactInfo(b2Contact* contract);
  191. /// Copy construct.
  192. ContactInfo(const ContactInfo& other);
  193. /// Rigid body A.
  194. SharedPtr<RigidBody2D> bodyA_;
  195. /// Rigid body B.
  196. SharedPtr<RigidBody2D> bodyB_;
  197. /// Node A.
  198. SharedPtr<Node> nodeA_;
  199. /// Node B.
  200. SharedPtr<Node> nodeB_;
  201. };
  202. /// Begin contact infos.
  203. Vector<ContactInfo> beginContactInfos_;
  204. /// End contact infos.
  205. Vector<ContactInfo> endContactInfos_;
  206. };
  207. }