ConstraintPulley2D.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintPulley2D.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. ConstraintPulley2D::ConstraintPulley2D(Context* context) :
  13. Constraint2D(context),
  14. ownerBodyGroundAnchor_(-1.0f, 1.0f),
  15. otherBodyGroundAnchor_(1.0f, 1.0f),
  16. ownerBodyAnchor_(-1.0f, 0.0f),
  17. otherBodyAnchor_(1.0f, 0.0f)
  18. {
  19. }
  20. ConstraintPulley2D::~ConstraintPulley2D() = default;
  21. void ConstraintPulley2D::RegisterObject(Context* context)
  22. {
  23. context->RegisterFactory<ConstraintPulley2D>(PHYSICS2D_CATEGORY);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  25. URHO3D_ACCESSOR_ATTRIBUTE("Owner Body Ground Anchor", GetOwnerBodyGroundAnchor, SetOwnerBodyGroundAnchor, Vector2::ZERO,
  26. AM_DEFAULT);
  27. URHO3D_ACCESSOR_ATTRIBUTE("Other Body Ground Anchor", GetOtherBodyGroundAnchor, SetOtherBodyGroundAnchor, Vector2::ZERO,
  28. AM_DEFAULT);
  29. URHO3D_ACCESSOR_ATTRIBUTE("Owner Body Anchor", GetOwnerBodyAnchor, SetOwnerBodyAnchor, Vector2::ZERO, AM_DEFAULT);
  30. URHO3D_ACCESSOR_ATTRIBUTE("Other Body Anchor", GetOtherBodyAnchor, SetOtherBodyAnchor, Vector2::ZERO, AM_DEFAULT);
  31. URHO3D_ACCESSOR_ATTRIBUTE("Ratio", GetRatio, SetRatio, 0.0f, AM_DEFAULT);
  32. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  33. }
  34. void ConstraintPulley2D::SetOwnerBodyGroundAnchor(const Vector2& groundAnchor)
  35. {
  36. if (groundAnchor == ownerBodyGroundAnchor_)
  37. return;
  38. ownerBodyGroundAnchor_ = groundAnchor;
  39. RecreateJoint();
  40. MarkNetworkUpdate();
  41. }
  42. void ConstraintPulley2D::SetOtherBodyGroundAnchor(const Vector2& groundAnchor)
  43. {
  44. if (groundAnchor == otherBodyGroundAnchor_)
  45. return;
  46. otherBodyGroundAnchor_ = groundAnchor;
  47. RecreateJoint();
  48. MarkNetworkUpdate();
  49. }
  50. void ConstraintPulley2D::SetOwnerBodyAnchor(const Vector2& anchor)
  51. {
  52. if (anchor == ownerBodyAnchor_)
  53. return;
  54. ownerBodyAnchor_ = anchor;
  55. RecreateJoint();
  56. MarkNetworkUpdate();
  57. }
  58. void ConstraintPulley2D::SetOtherBodyAnchor(const Vector2& anchor)
  59. {
  60. if (anchor == otherBodyAnchor_)
  61. return;
  62. otherBodyAnchor_ = anchor;
  63. RecreateJoint();
  64. MarkNetworkUpdate();
  65. }
  66. void ConstraintPulley2D::SetRatio(float ratio)
  67. {
  68. if (ratio == jointDef_.ratio)
  69. return;
  70. jointDef_.ratio = ratio;
  71. RecreateJoint();
  72. MarkNetworkUpdate();
  73. }
  74. b2JointDef* ConstraintPulley2D::GetJointDef()
  75. {
  76. if (!ownerBody_ || !otherBody_)
  77. return nullptr;
  78. b2Body* bodyA = ownerBody_->GetBody();
  79. b2Body* bodyB = otherBody_->GetBody();
  80. if (!bodyA || !bodyB)
  81. return nullptr;
  82. jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(ownerBodyGroundAnchor_), ToB2Vec2(otherBodyGroundAnchor_),
  83. ToB2Vec2(ownerBodyAnchor_), ToB2Vec2(otherBodyAnchor_), jointDef_.ratio);
  84. return &jointDef_;
  85. }
  86. }