Constraint2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Copyright (c) 2008-2015 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. if (ownerBody_)
  44. ownerBody_->RemoveConstraint2D(this);
  45. if (otherBody_)
  46. otherBody_->RemoveConstraint2D(this);
  47. ReleaseJoint();
  48. }
  49. void Constraint2D::RegisterObject(Context* context)
  50. {
  51. ACCESSOR_ATTRIBUTE("Collide Connected", GetCollideConnected, SetCollideConnected, bool, false, AM_DEFAULT);
  52. }
  53. void Constraint2D::OnSetEnabled()
  54. {
  55. if (IsEnabledEffective())
  56. CreateJoint();
  57. else
  58. ReleaseJoint();
  59. }
  60. void Constraint2D::CreateJoint()
  61. {
  62. if (joint_)
  63. return;
  64. b2JointDef* jointDef = GetJointDef();
  65. if (jointDef)
  66. {
  67. joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
  68. joint_->SetUserData(this);
  69. }
  70. }
  71. void Constraint2D::ReleaseJoint()
  72. {
  73. if (!joint_)
  74. return;
  75. if (physicsWorld_)
  76. physicsWorld_->GetWorld()->DestroyJoint(joint_);
  77. joint_ = 0;
  78. }
  79. void Constraint2D::SetOtherBody(RigidBody2D* body)
  80. {
  81. if (body == otherBody_)
  82. return;
  83. otherBody_ = body;
  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. LOGERROR("No rigid body component in node, can not create constraint");
  108. return;
  109. }
  110. }
  111. }
  112. void Constraint2D::OnSceneSet(Scene* scene)
  113. {
  114. if (scene)
  115. physicsWorld_ = scene->GetOrCreateComponent<PhysicsWorld2D>();
  116. }
  117. void Constraint2D::InitializeJointDef(b2JointDef* jointDef)
  118. {
  119. jointDef->bodyA = ownerBody_->GetBody();
  120. jointDef->bodyB = otherBody_->GetBody();
  121. jointDef->collideConnected = collideConnected_;
  122. }
  123. void Constraint2D::RecreateJoint()
  124. {
  125. if (attachedConstraint_)
  126. attachedConstraint_->ReleaseJoint();
  127. ReleaseJoint();
  128. CreateJoint();
  129. if (attachedConstraint_)
  130. attachedConstraint_->CreateJoint();
  131. }
  132. }