Constraint2D.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // Copyright (c) 2008-2017 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. otherBodyNodeIDDirty_(false)
  40. {
  41. }
  42. Constraint2D::~Constraint2D()
  43. {
  44. ReleaseJoint();
  45. }
  46. void Constraint2D::RegisterObject(Context* context)
  47. {
  48. ATOMIC_ACCESSOR_ATTRIBUTE("Collide Connected", GetCollideConnected, SetCollideConnected, bool, false, AM_DEFAULT);
  49. ATOMIC_ATTRIBUTE("Other Body NodeID", unsigned, otherBodyNodeID_, 0, AM_DEFAULT | AM_NODEID);
  50. }
  51. void Constraint2D::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  52. {
  53. Serializable::OnSetAttribute(attr, src);
  54. if (!attr.accessor_ && attr.offset_ == offsetof(Constraint2D, otherBodyNodeID_))
  55. otherBodyNodeIDDirty_ = true;
  56. }
  57. void Constraint2D::ApplyAttributes()
  58. {
  59. // If other body node ID dirty, try to find it now and apply
  60. if (otherBodyNodeIDDirty_)
  61. {
  62. Scene* scene = GetScene();
  63. if (scene)
  64. {
  65. Node* otherNode = scene->GetNode(otherBodyNodeID_);
  66. if (otherNode)
  67. SetOtherBody(otherNode->GetComponent<RigidBody2D>());
  68. }
  69. otherBodyNodeIDDirty_ = false;
  70. }
  71. }
  72. void Constraint2D::OnSetEnabled()
  73. {
  74. if (IsEnabledEffective())
  75. CreateJoint();
  76. else
  77. ReleaseJoint();
  78. }
  79. void Constraint2D::CreateJoint()
  80. {
  81. if (joint_)
  82. return;
  83. b2JointDef* jointDef = GetJointDef();
  84. if (jointDef)
  85. {
  86. joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
  87. joint_->SetUserData(this);
  88. if (ownerBody_)
  89. ownerBody_->AddConstraint2D(this);
  90. if (otherBody_)
  91. otherBody_->AddConstraint2D(this);
  92. }
  93. }
  94. void Constraint2D::ReleaseJoint()
  95. {
  96. if (!joint_)
  97. return;
  98. if (ownerBody_)
  99. ownerBody_->RemoveConstraint2D(this);
  100. if (otherBody_)
  101. otherBody_->RemoveConstraint2D(this);
  102. if (physicsWorld_)
  103. physicsWorld_->GetWorld()->DestroyJoint(joint_);
  104. joint_ = 0;
  105. }
  106. void Constraint2D::SetOtherBody(RigidBody2D* body)
  107. {
  108. if (body == otherBody_)
  109. return;
  110. otherBody_ = body;
  111. Node* otherNode = body ? body->GetNode() : (Node*)0;
  112. otherBodyNodeID_ = otherNode ? otherNode->GetID() : 0;
  113. RecreateJoint();
  114. MarkNetworkUpdate();
  115. }
  116. void Constraint2D::SetCollideConnected(bool collideConnected)
  117. {
  118. if (collideConnected == collideConnected_)
  119. return;
  120. collideConnected_ = collideConnected;
  121. RecreateJoint();
  122. MarkNetworkUpdate();
  123. }
  124. void Constraint2D::SetAttachedConstraint(Constraint2D* constraint)
  125. {
  126. attachedConstraint_ = constraint;
  127. }
  128. void Constraint2D::OnNodeSet(Node* node)
  129. {
  130. Component::OnNodeSet(node);
  131. if (node)
  132. {
  133. ownerBody_ = node->GetComponent<RigidBody2D>();
  134. if (!ownerBody_)
  135. {
  136. ATOMIC_LOGERROR("No right body component in node, can not create constraint");
  137. return;
  138. }
  139. }
  140. }
  141. void Constraint2D::OnSceneSet(Scene* scene)
  142. {
  143. if (scene)
  144. {
  145. physicsWorld_ = scene->GetDerivedComponent<PhysicsWorld2D>();
  146. if (!physicsWorld_)
  147. physicsWorld_ = scene->CreateComponent<PhysicsWorld2D>();
  148. }
  149. }
  150. void Constraint2D::InitializeJointDef(b2JointDef* jointDef)
  151. {
  152. jointDef->bodyA = ownerBody_->GetBody();
  153. jointDef->bodyB = otherBody_->GetBody();
  154. jointDef->collideConnected = collideConnected_;
  155. }
  156. void Constraint2D::RecreateJoint()
  157. {
  158. if (attachedConstraint_)
  159. attachedConstraint_->ReleaseJoint();
  160. ReleaseJoint();
  161. CreateJoint();
  162. if (attachedConstraint_)
  163. attachedConstraint_->CreateJoint();
  164. }
  165. }