ConstraintWheel2D.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintWheel2D.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. ConstraintWheel2D::ConstraintWheel2D(Context* context) :
  13. Constraint2D(context),
  14. anchor_(Vector2::ZERO),
  15. axis_(Vector2::RIGHT)
  16. {
  17. }
  18. ConstraintWheel2D::~ConstraintWheel2D() = default;
  19. void ConstraintWheel2D::RegisterObject(Context* context)
  20. {
  21. context->RegisterFactory<ConstraintWheel2D>(PHYSICS2D_CATEGORY);
  22. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, true, AM_DEFAULT);
  23. URHO3D_ACCESSOR_ATTRIBUTE("Anchor", GetAnchor, SetAnchor, Vector2::ZERO, AM_DEFAULT);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Axis", GetAxis, SetAxis, Vector2::RIGHT, AM_DEFAULT);
  25. URHO3D_ACCESSOR_ATTRIBUTE("Enable Motor", GetEnableMotor, SetEnableMotor, false, AM_DEFAULT);
  26. URHO3D_ACCESSOR_ATTRIBUTE("Max Motor Torque", GetMaxMotorTorque, SetMaxMotorTorque, 0.0f, AM_DEFAULT);
  27. URHO3D_ACCESSOR_ATTRIBUTE("Motor Speed", GetMotorSpeed, SetMotorSpeed, 0.0f, AM_DEFAULT);
  28. URHO3D_ACCESSOR_ATTRIBUTE("Stiffness", GetStiffness, SetStiffness, 0.0f, AM_DEFAULT);
  29. URHO3D_ACCESSOR_ATTRIBUTE("Damping", GetDamping, SetDamping, 0.0f, AM_DEFAULT);
  30. URHO3D_ACCESSOR_ATTRIBUTE("Enable Limit", GetEnableLimit, SetEnableLimit, false, AM_DEFAULT);
  31. URHO3D_ACCESSOR_ATTRIBUTE("Lower Translation", GetLowerTranslation, SetLowerTranslation, 0.0f, AM_DEFAULT);
  32. URHO3D_ACCESSOR_ATTRIBUTE("Upper Translation", GetUpperTranslation, SetUpperTranslation, 0.0f, AM_DEFAULT);
  33. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  34. }
  35. void ConstraintWheel2D::SetAnchor(const Vector2& anchor)
  36. {
  37. if (anchor == anchor_)
  38. return;
  39. anchor_ = anchor;
  40. RecreateJoint();
  41. MarkNetworkUpdate();
  42. }
  43. void ConstraintWheel2D::SetAxis(const Vector2& axis)
  44. {
  45. if (axis == axis_)
  46. return;
  47. axis_ = axis;
  48. RecreateJoint();
  49. MarkNetworkUpdate();
  50. }
  51. void ConstraintWheel2D::SetEnableMotor(bool enableMotor)
  52. {
  53. if (enableMotor == jointDef_.enableMotor)
  54. return;
  55. jointDef_.enableMotor = enableMotor;
  56. if (joint_)
  57. static_cast<b2WheelJoint*>(joint_)->EnableMotor(enableMotor);
  58. else
  59. RecreateJoint();
  60. MarkNetworkUpdate();
  61. }
  62. void ConstraintWheel2D::SetMaxMotorTorque(float maxMotorTorque)
  63. {
  64. if (maxMotorTorque == jointDef_.maxMotorTorque)
  65. return;
  66. jointDef_.maxMotorTorque = maxMotorTorque;
  67. if (joint_)
  68. static_cast<b2WheelJoint*>(joint_)->SetMaxMotorTorque(maxMotorTorque);
  69. else
  70. RecreateJoint();
  71. MarkNetworkUpdate();
  72. }
  73. void ConstraintWheel2D::SetMotorSpeed(float motorSpeed)
  74. {
  75. if (motorSpeed == jointDef_.motorSpeed)
  76. return;
  77. jointDef_.motorSpeed = motorSpeed;
  78. if (joint_)
  79. static_cast<b2WheelJoint*>(joint_)->SetMotorSpeed(motorSpeed);
  80. else
  81. RecreateJoint();
  82. MarkNetworkUpdate();
  83. }
  84. void ConstraintWheel2D::SetStiffness(float stiffness)
  85. {
  86. if (stiffness == jointDef_.stiffness)
  87. return;
  88. jointDef_.stiffness = stiffness;
  89. if (joint_)
  90. static_cast<b2WheelJoint*>(joint_)->SetStiffness(stiffness);
  91. else
  92. RecreateJoint();
  93. MarkNetworkUpdate();
  94. }
  95. void ConstraintWheel2D::SetDamping(float damping)
  96. {
  97. if (damping == jointDef_.damping)
  98. return;
  99. jointDef_.damping = damping;
  100. if (joint_)
  101. static_cast<b2WheelJoint*>(joint_)->SetDamping(damping);
  102. else
  103. RecreateJoint();
  104. MarkNetworkUpdate();
  105. }
  106. b2JointDef* ConstraintWheel2D::GetJointDef()
  107. {
  108. if (!ownerBody_ || !otherBody_)
  109. return nullptr;
  110. b2Body* bodyA = ownerBody_->GetBody();
  111. b2Body* bodyB = otherBody_->GetBody();
  112. if (!bodyA || !bodyB)
  113. return nullptr;
  114. jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_), ToB2Vec2(axis_));
  115. return &jointDef_;
  116. }
  117. bool ConstraintWheel2D::SetLinearStiffness(float frequencyHertz, float dampingRatio)
  118. {
  119. if (!ownerBody_ || !otherBody_)
  120. return false;
  121. b2Body* bodyA = ownerBody_->GetBody();
  122. b2Body* bodyB = otherBody_->GetBody();
  123. if (!bodyA || !bodyB)
  124. return false;
  125. float stiffness, damping;
  126. b2LinearStiffness(stiffness, damping, frequencyHertz, dampingRatio, bodyA, bodyB);
  127. if (joint_)
  128. {
  129. static_cast<b2WheelJoint*>(joint_)->SetDamping(damping);
  130. static_cast<b2WheelJoint*>(joint_)->SetStiffness(stiffness);
  131. }
  132. else
  133. {
  134. RecreateJoint();
  135. }
  136. MarkNetworkUpdate();
  137. return true;
  138. }
  139. void ConstraintWheel2D::SetLowerTranslation(float lowerTranslation)
  140. {
  141. if (lowerTranslation == jointDef_.lowerTranslation)
  142. return;
  143. jointDef_.lowerTranslation = lowerTranslation;
  144. if (joint_)
  145. static_cast<b2WheelJoint*>(joint_)->SetLimits(lowerTranslation, jointDef_.upperTranslation);
  146. else
  147. RecreateJoint();
  148. MarkNetworkUpdate();
  149. }
  150. void ConstraintWheel2D::SetUpperTranslation(float upperTranslation)
  151. {
  152. if (upperTranslation == jointDef_.upperTranslation)
  153. return;
  154. jointDef_.upperTranslation = upperTranslation;
  155. if (joint_)
  156. static_cast<b2WheelJoint*>(joint_)->SetLimits(jointDef_.lowerTranslation, upperTranslation);
  157. else
  158. RecreateJoint();
  159. MarkNetworkUpdate();
  160. }
  161. void ConstraintWheel2D::SetEnableLimit(bool enableLimit)
  162. {
  163. if (enableLimit == jointDef_.enableLimit)
  164. return;
  165. jointDef_.enableLimit = enableLimit;
  166. if (joint_)
  167. static_cast<b2WheelJoint*>(joint_)->EnableLimit(enableLimit);
  168. else
  169. RecreateJoint();
  170. MarkNetworkUpdate();
  171. }
  172. }