Constraint2D.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #pragma once
  23. #include "../Scene/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. URHO3D_OBJECT(Constraint2D, Component);
  33. public:
  34. /// Construct.
  35. explicit Constraint2D(Context* context);
  36. /// Destruct.
  37. ~Constraint2D() override;
  38. /// Register object factory.
  39. static void RegisterObject(Context* context);
  40. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  41. void ApplyAttributes() override;
  42. /// Handle enabled/disabled state change.
  43. void OnSetEnabled() override;
  44. /// Create joint.
  45. void CreateJoint();
  46. /// Release joint.
  47. void ReleaseJoint();
  48. /// Set other rigid body.
  49. /// @property
  50. void SetOtherBody(RigidBody2D* body);
  51. /// Set collide connected.
  52. /// @property
  53. void SetCollideConnected(bool collideConnected);
  54. /// Set attached constriant (for gear).
  55. void SetAttachedConstraint(Constraint2D* constraint);
  56. /// Return owner body.
  57. /// @property
  58. RigidBody2D* GetOwnerBody() const { return ownerBody_; }
  59. /// Return other body.
  60. /// @property
  61. RigidBody2D* GetOtherBody() const { return otherBody_; }
  62. /// Return collide connected.
  63. /// @property
  64. bool GetCollideConnected() const { return collideConnected_; }
  65. /// Return attached constraint (for gear).
  66. Constraint2D* GetAttachedConstraint() const { return attachedConstraint_; }
  67. /// Return Box2D joint.
  68. b2Joint* GetJoint() const { return joint_; }
  69. protected:
  70. /// Handle node being assigned.
  71. void OnNodeSet(Node* node) override;
  72. /// Handle scene being assigned.
  73. void OnSceneSet(Scene* scene) override;
  74. /// Return joint def.
  75. virtual b2JointDef* GetJointDef() { return nullptr; };
  76. /// Recreate joint.
  77. void RecreateJoint();
  78. /// Initialize joint def.
  79. void InitializeJointDef(b2JointDef* jointDef);
  80. /// Mark other body node ID dirty.
  81. void MarkOtherBodyNodeIDDirty() { otherBodyNodeIDDirty_ = true; }
  82. /// Physics world.
  83. WeakPtr<PhysicsWorld2D> physicsWorld_;
  84. /// Box2D joint.
  85. b2Joint* joint_{};
  86. /// Owner body.
  87. WeakPtr<RigidBody2D> ownerBody_;
  88. /// Other body.
  89. WeakPtr<RigidBody2D> otherBody_;
  90. /// Other body node ID for serialization.
  91. unsigned otherBodyNodeID_{};
  92. /// Collide connected flag.
  93. bool collideConnected_{};
  94. /// Other body node ID dirty flag.
  95. bool otherBodyNodeIDDirty_{};
  96. /// Attached constraint.
  97. WeakPtr<Constraint2D> attachedConstraint_;
  98. };
  99. }