ConstraintWeld2D.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintWeld2D.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. ConstraintWeld2D::ConstraintWeld2D(Context* context) :
  13. Constraint2D(context),
  14. anchor_(Vector2::ZERO)
  15. {
  16. }
  17. ConstraintWeld2D::~ConstraintWeld2D() = default;
  18. void ConstraintWeld2D::RegisterObject(Context* context)
  19. {
  20. context->RegisterFactory<ConstraintWeld2D>(PHYSICS2D_CATEGORY);
  21. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  22. URHO3D_ACCESSOR_ATTRIBUTE("Anchor", GetAnchor, SetAnchor, Vector2::ZERO, AM_DEFAULT);
  23. URHO3D_ACCESSOR_ATTRIBUTE("Stiffness", GetStiffness, SetStiffness, 0.0f, AM_DEFAULT);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Damping", GetDamping, SetDamping, 0.0f, AM_DEFAULT);
  25. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  26. }
  27. void ConstraintWeld2D::SetAnchor(const Vector2& anchor)
  28. {
  29. if (anchor == anchor_)
  30. return;
  31. anchor_ = anchor;
  32. RecreateJoint();
  33. MarkNetworkUpdate();
  34. }
  35. void ConstraintWeld2D::SetStiffness(float stiffness)
  36. {
  37. if (stiffness == jointDef_.stiffness)
  38. return;
  39. jointDef_.stiffness = stiffness;
  40. if (joint_)
  41. static_cast<b2WeldJoint*>(joint_)->SetStiffness(stiffness);
  42. else
  43. RecreateJoint();
  44. MarkNetworkUpdate();
  45. }
  46. void ConstraintWeld2D::SetDamping(float damping)
  47. {
  48. if (damping == jointDef_.damping)
  49. return;
  50. jointDef_.damping = damping;
  51. if (joint_)
  52. static_cast<b2WeldJoint*>(joint_)->SetDamping(damping);
  53. else
  54. RecreateJoint();
  55. MarkNetworkUpdate();
  56. }
  57. b2JointDef* ConstraintWeld2D::GetJointDef()
  58. {
  59. if (!ownerBody_ || !otherBody_)
  60. return nullptr;
  61. b2Body* bodyA = ownerBody_->GetBody();
  62. b2Body* bodyB = otherBody_->GetBody();
  63. if (!bodyA || !bodyB)
  64. return nullptr;
  65. jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
  66. return &jointDef_;
  67. }
  68. bool ConstraintWeld2D::SetAngularStiffness(float frequencyHertz, float dampingRatio)
  69. {
  70. if (!ownerBody_ || !otherBody_)
  71. return false;
  72. b2Body* bodyA = ownerBody_->GetBody();
  73. b2Body* bodyB = otherBody_->GetBody();
  74. if (!bodyA || !bodyB)
  75. return false;
  76. float stiffness, damping;
  77. b2AngularStiffness(stiffness, damping, frequencyHertz, dampingRatio, bodyA, bodyB);
  78. if (joint_)
  79. {
  80. static_cast<b2WeldJoint*>(joint_)->SetDamping(damping);
  81. static_cast<b2WeldJoint*>(joint_)->SetStiffness(stiffness);
  82. }
  83. else
  84. {
  85. RecreateJoint();
  86. }
  87. MarkNetworkUpdate();
  88. return true;
  89. }
  90. }