Constraint2D.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright (c) 2008-2016 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. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/Log.h"
  25. #include "../Scene/Node.h"
  26. #include "../Scene/Scene.h"
  27. #include "../Atomic2D/Constraint2D.h"
  28. #include "../Atomic2D/PhysicsUtils2D.h"
  29. #include "../Atomic2D/RigidBody2D.h"
  30. #include "../Atomic2D/PhysicsWorld2D.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  33. {
  34. extern const char* ATOMIC2D_CATEGORY;
  35. Constraint2D::Constraint2D(Context* context) :
  36. Component(context),
  37. joint_(0),
  38. collideConnected_(false)
  39. {
  40. }
  41. Constraint2D::~Constraint2D()
  42. {
  43. ReleaseJoint();
  44. }
  45. void Constraint2D::RegisterObject(Context* context)
  46. {
  47. ATOMIC_ACCESSOR_ATTRIBUTE("Collide Connected", GetCollideConnected, SetCollideConnected, bool, false, AM_DEFAULT);
  48. }
  49. void Constraint2D::OnSetEnabled()
  50. {
  51. if (IsEnabledEffective())
  52. CreateJoint();
  53. else
  54. ReleaseJoint();
  55. }
  56. void Constraint2D::CreateJoint()
  57. {
  58. if (joint_)
  59. return;
  60. b2JointDef* jointDef = GetJointDef();
  61. if (jointDef)
  62. {
  63. joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
  64. joint_->SetUserData(this);
  65. if (ownerBody_)
  66. ownerBody_->AddConstraint2D(this);
  67. if (otherBody_)
  68. otherBody_->AddConstraint2D(this);
  69. }
  70. }
  71. void Constraint2D::ReleaseJoint()
  72. {
  73. if (!joint_)
  74. return;
  75. if (ownerBody_)
  76. ownerBody_->RemoveConstraint2D(this);
  77. if (otherBody_)
  78. otherBody_->RemoveConstraint2D(this);
  79. if (physicsWorld_)
  80. physicsWorld_->GetWorld()->DestroyJoint(joint_);
  81. joint_ = 0;
  82. }
  83. void Constraint2D::SetOtherBody(RigidBody2D* body)
  84. {
  85. if (body == otherBody_)
  86. return;
  87. otherBody_ = body;
  88. RecreateJoint();
  89. MarkNetworkUpdate();
  90. }
  91. void Constraint2D::SetCollideConnected(bool collideConnected)
  92. {
  93. if (collideConnected == collideConnected_)
  94. return;
  95. collideConnected_ = collideConnected;
  96. RecreateJoint();
  97. MarkNetworkUpdate();
  98. }
  99. void Constraint2D::SetAttachedConstraint(Constraint2D* constraint)
  100. {
  101. attachedConstraint_ = constraint;
  102. }
  103. void Constraint2D::OnNodeSet(Node* node)
  104. {
  105. Component::OnNodeSet(node);
  106. if (node)
  107. {
  108. ownerBody_ = node->GetComponent<RigidBody2D>();
  109. if (!ownerBody_)
  110. {
  111. ATOMIC_LOGERROR("No right body component in node, can not create constraint");
  112. return;
  113. }
  114. }
  115. }
  116. void Constraint2D::OnSceneSet(Scene* scene)
  117. {
  118. if (scene)
  119. physicsWorld_ = scene->GetOrCreateComponent<PhysicsWorld2D>();
  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. }