Constraint2D.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../IO/Log.h"
  6. #include "../Physics2D/Constraint2D.h"
  7. #include "../Physics2D/PhysicsUtils2D.h"
  8. #include "../Physics2D/PhysicsWorld2D.h"
  9. #include "../Physics2D/RigidBody2D.h"
  10. #include "../Scene/Node.h"
  11. #include "../Scene/Scene.h"
  12. #include "../DebugNew.h"
  13. namespace Urho3D
  14. {
  15. Constraint2D::Constraint2D(Context* context) :
  16. Component(context)
  17. {
  18. }
  19. Constraint2D::~Constraint2D()
  20. {
  21. ReleaseJoint();
  22. }
  23. void Constraint2D::RegisterObject(Context* context)
  24. {
  25. URHO3D_ACCESSOR_ATTRIBUTE("Collide Connected", GetCollideConnected, SetCollideConnected, false, AM_DEFAULT);
  26. URHO3D_ATTRIBUTE_EX("Other Body NodeID", otherBodyNodeID_, MarkOtherBodyNodeIDDirty, 0, AM_DEFAULT | AM_NODEID);
  27. }
  28. void Constraint2D::ApplyAttributes()
  29. {
  30. // If other body node ID dirty, try to find it now and apply
  31. if (otherBodyNodeIDDirty_)
  32. {
  33. Scene* scene = GetScene();
  34. if (scene)
  35. {
  36. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  37. if (otherNode)
  38. SetOtherBody(otherNode->GetComponent<RigidBody2D>());
  39. }
  40. otherBodyNodeIDDirty_ = false;
  41. }
  42. }
  43. void Constraint2D::OnSetEnabled()
  44. {
  45. if (IsEnabledEffective())
  46. CreateJoint();
  47. else
  48. ReleaseJoint();
  49. }
  50. void Constraint2D::CreateJoint()
  51. {
  52. if (joint_)
  53. return;
  54. b2JointDef* jointDef = GetJointDef();
  55. if (jointDef)
  56. {
  57. joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
  58. joint_->GetUserData().pointer = (uintptr_t)this;
  59. if (ownerBody_)
  60. ownerBody_->AddConstraint2D(this);
  61. if (otherBody_)
  62. otherBody_->AddConstraint2D(this);
  63. }
  64. }
  65. void Constraint2D::ReleaseJoint()
  66. {
  67. if (!joint_)
  68. return;
  69. if (ownerBody_)
  70. ownerBody_->RemoveConstraint2D(this);
  71. if (otherBody_)
  72. otherBody_->RemoveConstraint2D(this);
  73. if (physicsWorld_)
  74. physicsWorld_->GetWorld()->DestroyJoint(joint_);
  75. joint_ = nullptr;
  76. }
  77. void Constraint2D::SetOtherBody(RigidBody2D* body)
  78. {
  79. if (body == otherBody_)
  80. return;
  81. otherBody_ = body;
  82. Node* otherNode = body ? body->GetNode() : nullptr;
  83. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  84. RecreateJoint();
  85. MarkNetworkUpdate();
  86. }
  87. void Constraint2D::SetCollideConnected(bool collideConnected)
  88. {
  89. if (collideConnected == collideConnected_)
  90. return;
  91. collideConnected_ = collideConnected;
  92. RecreateJoint();
  93. MarkNetworkUpdate();
  94. }
  95. void Constraint2D::SetAttachedConstraint(Constraint2D* constraint)
  96. {
  97. attachedConstraint_ = constraint;
  98. }
  99. void Constraint2D::OnNodeSet(Node* node)
  100. {
  101. Component::OnNodeSet(node);
  102. if (node)
  103. {
  104. ownerBody_ = node->GetComponent<RigidBody2D>();
  105. if (!ownerBody_)
  106. {
  107. URHO3D_LOGERROR("No right body component in node, can not create constraint");
  108. return;
  109. }
  110. }
  111. }
  112. void Constraint2D::OnSceneSet(Scene* scene)
  113. {
  114. if (scene)
  115. {
  116. physicsWorld_ = scene->GetDerivedComponent<PhysicsWorld2D>();
  117. if (!physicsWorld_)
  118. physicsWorld_ = scene->CreateComponent<PhysicsWorld2D>();
  119. }
  120. }
  121. void Constraint2D::InitializeJointDef(b2JointDef* jointDef)
  122. {
  123. jointDef->bodyA = ownerBody_->GetBody();
  124. jointDef->bodyB = otherBody_->GetBody();
  125. jointDef->collideConnected = collideConnected_;
  126. }
  127. void Constraint2D::RecreateJoint()
  128. {
  129. if (attachedConstraint_)
  130. attachedConstraint_->ReleaseJoint();
  131. ReleaseJoint();
  132. CreateJoint();
  133. if (attachedConstraint_)
  134. attachedConstraint_->CreateJoint();
  135. }
  136. }