Constraint2D.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Copyright (c) 2008-2014 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 "Constraint2D.h"
  24. #include "Context.h"
  25. #include "Log.h"
  26. #include "Node.h"
  27. #include "PhysicsUtils2D.h"
  28. #include "RigidBody2D.h"
  29. #include "PhysicsWorld2D.h"
  30. #include "Scene.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. extern const char* URHO2D_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. COPY_BASE_ATTRIBUTES(Component);
  53. }
  54. void Constraint2D::OnSetEnabled()
  55. {
  56. if (IsEnabledEffective())
  57. CreateJoint();
  58. else
  59. ReleaseJoint();
  60. }
  61. void Constraint2D::CreateJoint()
  62. {
  63. if (joint_)
  64. return;
  65. b2JointDef* jointDef = GetJointDef();
  66. if (jointDef)
  67. {
  68. joint_ = physicsWorld_->GetWorld()->CreateJoint(jointDef);
  69. joint_->SetUserData(this);
  70. }
  71. }
  72. void Constraint2D::ReleaseJoint()
  73. {
  74. if (!joint_)
  75. return;
  76. if (physicsWorld_)
  77. physicsWorld_->GetWorld()->DestroyJoint(joint_);
  78. joint_ = 0;
  79. }
  80. void Constraint2D::SetOtherBody(RigidBody2D* body)
  81. {
  82. if (body == otherBody_)
  83. return;
  84. otherBody_ = body;
  85. RecreateJoint();
  86. MarkNetworkUpdate();
  87. }
  88. void Constraint2D::SetCollideConnected(bool collideConnected)
  89. {
  90. if (collideConnected == collideConnected_)
  91. return;
  92. collideConnected_ = collideConnected;
  93. RecreateJoint();
  94. MarkNetworkUpdate();
  95. }
  96. void Constraint2D::SetAttachedConstraint(Constraint2D* constraint)
  97. {
  98. attachedConstraint_ = constraint;
  99. }
  100. void Constraint2D::OnNodeSet(Node* node)
  101. {
  102. Component::OnNodeSet(node);
  103. if (node)
  104. {
  105. Scene* scene = GetScene();
  106. physicsWorld_ = scene->GetOrCreateComponent<PhysicsWorld2D>();
  107. ownerBody_ = node->GetComponent<RigidBody2D>();
  108. if (!ownerBody_)
  109. {
  110. LOGERROR("No right body component in node, can not create constraint");
  111. return;
  112. }
  113. }
  114. }
  115. void Constraint2D::InitializeJointDef(b2JointDef* jointDef)
  116. {
  117. jointDef->bodyA = ownerBody_->GetBody();
  118. jointDef->bodyB = otherBody_->GetBody();
  119. jointDef->collideConnected = collideConnected_;
  120. }
  121. void Constraint2D::RecreateJoint()
  122. {
  123. if (attachedConstraint_)
  124. attachedConstraint_->ReleaseJoint();
  125. ReleaseJoint();
  126. CreateJoint();
  127. if (attachedConstraint_)
  128. attachedConstraint_->CreateJoint();
  129. }
  130. }