ConstraintMouse2D.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintMouse2D.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. ConstraintMouse2D::ConstraintMouse2D(Context* context) :
  13. Constraint2D(context),
  14. target_(Vector2::ZERO)
  15. {
  16. }
  17. ConstraintMouse2D::~ConstraintMouse2D() = default;
  18. void ConstraintMouse2D::RegisterObject(Context* context)
  19. {
  20. context->RegisterFactory<ConstraintMouse2D>(PHYSICS2D_CATEGORY);
  21. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  22. URHO3D_ACCESSOR_ATTRIBUTE("Target", GetTarget, SetTarget, Vector2::ZERO, AM_DEFAULT);
  23. URHO3D_ACCESSOR_ATTRIBUTE("Max Force", GetMaxForce, SetMaxForce, 0.0f, AM_DEFAULT);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Stiffness", GetStiffness, SetStiffness, 0.0f, AM_DEFAULT);
  25. URHO3D_ACCESSOR_ATTRIBUTE("Damping", GetDamping, SetDamping, 0.0f, AM_DEFAULT);
  26. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  27. }
  28. void ConstraintMouse2D::SetTarget(const Vector2& target)
  29. {
  30. if (target == target_)
  31. return;
  32. target_ = target;
  33. if (joint_)
  34. static_cast<b2MouseJoint*>(joint_)->SetTarget(ToB2Vec2(target));
  35. else
  36. RecreateJoint();
  37. MarkNetworkUpdate();
  38. }
  39. void ConstraintMouse2D::SetMaxForce(float maxForce)
  40. {
  41. if (maxForce == jointDef_.maxForce)
  42. return;
  43. jointDef_.maxForce = maxForce;
  44. if (joint_)
  45. static_cast<b2MouseJoint*>(joint_)->SetMaxForce(maxForce);
  46. else
  47. RecreateJoint();
  48. MarkNetworkUpdate();
  49. }
  50. void ConstraintMouse2D::SetStiffness(float stiffness)
  51. {
  52. if (stiffness == jointDef_.stiffness)
  53. return;
  54. jointDef_.stiffness = stiffness;
  55. if (joint_)
  56. static_cast<b2MouseJoint*>(joint_)->SetStiffness(stiffness);
  57. else
  58. RecreateJoint();
  59. MarkNetworkUpdate();
  60. }
  61. void ConstraintMouse2D::SetDamping(float damping)
  62. {
  63. if (damping == jointDef_.damping)
  64. return;
  65. jointDef_.damping = damping;
  66. if (joint_)
  67. static_cast<b2MouseJoint*>(joint_)->SetDamping(damping);
  68. else
  69. RecreateJoint();
  70. MarkNetworkUpdate();
  71. }
  72. b2JointDef* ConstraintMouse2D::GetJointDef()
  73. {
  74. if (!ownerBody_ || !otherBody_)
  75. return nullptr;
  76. b2Body* bodyA = otherBody_->GetBody();
  77. b2Body* bodyB = ownerBody_->GetBody();
  78. if (!bodyA || !bodyB)
  79. return nullptr;
  80. jointDef_.bodyA = bodyA;
  81. jointDef_.bodyB = bodyB;
  82. jointDef_.collideConnected = collideConnected_;
  83. jointDef_.target = ToB2Vec2(target_);
  84. return &jointDef_;
  85. }
  86. bool ConstraintMouse2D::SetLinearStiffness(float frequencyHertz, float dampingRatio)
  87. {
  88. if (!ownerBody_ || !otherBody_)
  89. return false;
  90. b2Body* bodyA = ownerBody_->GetBody();
  91. b2Body* bodyB = otherBody_->GetBody();
  92. if (!bodyA || !bodyB)
  93. return false;
  94. float stiffness, damping;
  95. b2LinearStiffness(stiffness, damping, frequencyHertz, dampingRatio, bodyA, bodyB);
  96. if (joint_)
  97. {
  98. static_cast<b2MouseJoint*>(joint_)->SetDamping(damping);
  99. static_cast<b2MouseJoint*>(joint_)->SetStiffness(stiffness);
  100. }
  101. else
  102. {
  103. RecreateJoint();
  104. }
  105. MarkNetworkUpdate();
  106. return true;
  107. }
  108. }