ConstraintDistance2D.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintDistance2D.h"
  6. #include "../Physics2D/PhysicsUtils2D.h"
  7. #include "../Physics2D/RigidBody2D.h"
  8. #include "../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. extern const char* PHYSICS2D_CATEGORY;
  12. ConstraintDistance2D::ConstraintDistance2D(Context* context) :
  13. Constraint2D(context),
  14. ownerBodyAnchor_(Vector2::ZERO),
  15. otherBodyAnchor_(Vector2::ZERO)
  16. {
  17. }
  18. ConstraintDistance2D::~ConstraintDistance2D() = default;
  19. void ConstraintDistance2D::RegisterObject(Context* context)
  20. {
  21. context->RegisterFactory<ConstraintDistance2D>(PHYSICS2D_CATEGORY);
  22. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  23. URHO3D_ACCESSOR_ATTRIBUTE("Owner Body Anchor", GetOwnerBodyAnchor, SetOwnerBodyAnchor, Vector2::ZERO, AM_DEFAULT);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Other Body Anchor", GetOtherBodyAnchor, SetOtherBodyAnchor, Vector2::ZERO, AM_DEFAULT);
  25. URHO3D_ACCESSOR_ATTRIBUTE("Stiffness", GetStiffness, SetStiffness, 0.0f, AM_DEFAULT);
  26. URHO3D_ACCESSOR_ATTRIBUTE("Damping", GetDamping, SetDamping, 0.0f, AM_DEFAULT);
  27. URHO3D_ACCESSOR_ATTRIBUTE("Length", GetLength, SetLength, 1.0f, AM_DEFAULT);
  28. URHO3D_ACCESSOR_ATTRIBUTE("Min Length", GetMinLength, SetMinLength, 0.0f, AM_DEFAULT);
  29. URHO3D_ACCESSOR_ATTRIBUTE("Max Length", GetMaxLength, SetMaxLength, FLT_MAX, AM_DEFAULT);
  30. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  31. }
  32. void ConstraintDistance2D::SetOwnerBodyAnchor(const Vector2& anchor)
  33. {
  34. if (anchor == ownerBodyAnchor_)
  35. return;
  36. ownerBodyAnchor_ = anchor;
  37. RecreateJoint();
  38. MarkNetworkUpdate();
  39. }
  40. void ConstraintDistance2D::SetOtherBodyAnchor(const Vector2& anchor)
  41. {
  42. if (anchor == otherBodyAnchor_)
  43. return;
  44. otherBodyAnchor_ = anchor;
  45. RecreateJoint();
  46. MarkNetworkUpdate();
  47. }
  48. void ConstraintDistance2D::SetStiffness(float stiffness)
  49. {
  50. if (stiffness == jointDef_.stiffness)
  51. return;
  52. jointDef_.stiffness = stiffness;
  53. if (joint_)
  54. static_cast<b2DistanceJoint*>(joint_)->SetStiffness(stiffness);
  55. else
  56. RecreateJoint();
  57. MarkNetworkUpdate();
  58. }
  59. void ConstraintDistance2D::SetDamping(float damping)
  60. {
  61. if (damping == jointDef_.damping)
  62. return;
  63. jointDef_.damping = damping;
  64. if (joint_)
  65. static_cast<b2DistanceJoint*>(joint_)->SetDamping(damping);
  66. else
  67. RecreateJoint();
  68. MarkNetworkUpdate();
  69. }
  70. void ConstraintDistance2D::SetLength(float length)
  71. {
  72. if (length == jointDef_.length)
  73. return;
  74. jointDef_.length = length;
  75. if (joint_)
  76. static_cast<b2DistanceJoint*>(joint_)->SetLength(length);
  77. else
  78. RecreateJoint();
  79. MarkNetworkUpdate();
  80. }
  81. b2JointDef* ConstraintDistance2D::GetJointDef()
  82. {
  83. if (!ownerBody_ || !otherBody_)
  84. return nullptr;
  85. b2Body* bodyA = ownerBody_->GetBody();
  86. b2Body* bodyB = otherBody_->GetBody();
  87. if (!bodyA || !bodyB)
  88. return nullptr;
  89. jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(ownerBodyAnchor_), ToB2Vec2(otherBodyAnchor_));
  90. return &jointDef_;
  91. }
  92. bool ConstraintDistance2D::SetLinearStiffness(float frequencyHertz, float dampingRatio)
  93. {
  94. if (!ownerBody_ || !otherBody_)
  95. return false;
  96. b2Body* bodyA = ownerBody_->GetBody();
  97. b2Body* bodyB = otherBody_->GetBody();
  98. if (!bodyA || !bodyB)
  99. return false;
  100. float stiffness, damping;
  101. b2LinearStiffness(stiffness, damping, frequencyHertz, dampingRatio, bodyA, bodyB);
  102. if (joint_)
  103. {
  104. static_cast<b2DistanceJoint*>(joint_)->SetDamping(damping);
  105. static_cast<b2DistanceJoint*>(joint_)->SetStiffness(stiffness);
  106. }
  107. else
  108. {
  109. RecreateJoint();
  110. }
  111. MarkNetworkUpdate();
  112. return true;
  113. }
  114. void ConstraintDistance2D::SetMinLength(float minLength)
  115. {
  116. if (minLength == jointDef_.minLength)
  117. return;
  118. jointDef_.minLength = minLength;
  119. if (joint_)
  120. static_cast<b2DistanceJoint*>(joint_)->SetMinLength(minLength);
  121. else
  122. RecreateJoint();
  123. MarkNetworkUpdate();
  124. }
  125. void ConstraintDistance2D::SetMaxLength(float maxLength)
  126. {
  127. if (maxLength == jointDef_.maxLength)
  128. return;
  129. jointDef_.maxLength = maxLength;
  130. if (joint_)
  131. static_cast<b2DistanceJoint*>(joint_)->SetMaxLength(maxLength);
  132. else
  133. RecreateJoint();
  134. MarkNetworkUpdate();
  135. }
  136. }