Constraint2D.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Scene/Component.h"
  5. #include <box2d/box2d.h>
  6. namespace Urho3D
  7. {
  8. class RigidBody2D;
  9. class PhysicsWorld2D;
  10. /// 2D physics constraint component.
  11. class URHO3D_API Constraint2D : public Component
  12. {
  13. URHO3D_OBJECT(Constraint2D, Component);
  14. public:
  15. /// Construct.
  16. explicit Constraint2D(Context* context);
  17. /// Destruct.
  18. ~Constraint2D() override;
  19. /// Register object factory.
  20. /// @nobind
  21. static void RegisterObject(Context* context);
  22. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  23. void ApplyAttributes() override;
  24. /// Handle enabled/disabled state change.
  25. void OnSetEnabled() override;
  26. /// Create joint.
  27. void CreateJoint();
  28. /// Release joint.
  29. void ReleaseJoint();
  30. /// Set other rigid body.
  31. /// @property
  32. void SetOtherBody(RigidBody2D* body);
  33. /// Set collide connected.
  34. /// @property
  35. void SetCollideConnected(bool collideConnected);
  36. /// Set attached constriant (for gear).
  37. void SetAttachedConstraint(Constraint2D* constraint);
  38. /// Return owner body.
  39. /// @property
  40. RigidBody2D* GetOwnerBody() const { return ownerBody_; }
  41. /// Return other body.
  42. /// @property
  43. RigidBody2D* GetOtherBody() const { return otherBody_; }
  44. /// Return collide connected.
  45. /// @property
  46. bool GetCollideConnected() const { return collideConnected_; }
  47. /// Return attached constraint (for gear).
  48. Constraint2D* GetAttachedConstraint() const { return attachedConstraint_; }
  49. /// Return Box2D joint.
  50. b2Joint* GetJoint() const { return joint_; }
  51. protected:
  52. /// Handle node being assigned.
  53. void OnNodeSet(Node* node) override;
  54. /// Handle scene being assigned.
  55. void OnSceneSet(Scene* scene) override;
  56. /// Return joint def.
  57. virtual b2JointDef* GetJointDef() { return nullptr; };
  58. /// Recreate joint.
  59. void RecreateJoint();
  60. /// Initialize joint def.
  61. void InitializeJointDef(b2JointDef* jointDef);
  62. /// Mark other body node ID dirty.
  63. void MarkOtherBodyNodeIDDirty() { otherBodyNodeIDDirty_ = true; }
  64. /// Physics world.
  65. WeakPtr<PhysicsWorld2D> physicsWorld_;
  66. /// Box2D joint.
  67. b2Joint* joint_{};
  68. /// Owner body.
  69. WeakPtr<RigidBody2D> ownerBody_;
  70. /// Other body.
  71. WeakPtr<RigidBody2D> otherBody_;
  72. /// Other body node ID for serialization.
  73. unsigned otherBodyNodeID_{};
  74. /// Collide connected flag.
  75. bool collideConnected_{};
  76. /// Other body node ID dirty flag.
  77. bool otherBodyNodeIDDirty_{};
  78. /// Attached constraint.
  79. WeakPtr<Constraint2D> attachedConstraint_;
  80. };
  81. }