Constraint2D.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 RigidBody2D;
  28. class PhysicsWorld2D;
  29. /// 2D physics constraint component.
  30. class URHO3D_API Constraint2D : public Component
  31. {
  32. OBJECT(Constraint2D);
  33. public:
  34. /// Construct.
  35. Constraint2D(Context* scontext);
  36. /// Destruct.
  37. virtual ~Constraint2D();
  38. /// Register object factory.
  39. static void RegisterObject(Context* context);
  40. /// Handle enabled/disabled state change.
  41. virtual void OnSetEnabled();
  42. /// Create Joint.
  43. void CreateJoint();
  44. /// Release Joint.
  45. void ReleaseJoint();
  46. /// Set other rigid body.
  47. void SetOtherBody(RigidBody2D* body);
  48. /// Set collide connected.
  49. void SetCollideConnected(bool collideConnected);
  50. /// Set attached constriant (for gear).
  51. void SetAttachedConstraint(Constraint2D* constraint);
  52. /// Return owner body.
  53. RigidBody2D* GetOwnerBody() const { return ownerBody_; }
  54. /// Return other body.
  55. RigidBody2D* GetOtherBody() const { return otherBody_; }
  56. /// Return collide connected.
  57. bool GetCollideConnected() const { return collideConnected_; }
  58. /// Return attached constraint (for gear).
  59. Constraint2D* GetAttachedConstraint() const { return attachedConstraint_; }
  60. /// Return Box2D joint.
  61. b2Joint* GetJoint() const { return joint_; }
  62. protected:
  63. /// Handle node being assigned.
  64. virtual void OnNodeSet(Node* node);
  65. /// Return joint def.
  66. virtual b2JointDef* GetJointDef() { return 0; };
  67. /// Recreate joint.
  68. void RecreateJoint();
  69. /// Initialize joint def.
  70. void InitializeJointDef(b2JointDef* jointDef);
  71. /// Physics world.
  72. WeakPtr<PhysicsWorld2D> physicsWorld_;
  73. /// Box2D joint.
  74. b2Joint* joint_;
  75. /// Owner body.
  76. WeakPtr<RigidBody2D> ownerBody_;
  77. /// Other body.
  78. WeakPtr<RigidBody2D> otherBody_;
  79. /// Collide connected.
  80. bool collideConnected_;
  81. /// Attached constraint.
  82. WeakPtr<Constraint2D> attachedConstraint_;
  83. };
  84. }