ConstraintRevolute2D.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Physics2D/ConstraintRevolute2D.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. ConstraintRevolute2D::ConstraintRevolute2D(Context* context) :
  13. Constraint2D(context),
  14. anchor_(Vector2::ZERO)
  15. {
  16. }
  17. ConstraintRevolute2D::~ConstraintRevolute2D() = default;
  18. void ConstraintRevolute2D::RegisterObject(Context* context)
  19. {
  20. context->RegisterFactory<ConstraintRevolute2D>(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("Enable Limit", GetEnableLimit, SetEnableLimit, false, AM_DEFAULT);
  24. URHO3D_ACCESSOR_ATTRIBUTE("Lower Angle", GetLowerAngle, SetLowerAngle, 0.0f, AM_DEFAULT);
  25. URHO3D_ACCESSOR_ATTRIBUTE("Upper Angle", GetUpperAngle, SetUpperAngle, 0.0f, AM_DEFAULT);
  26. URHO3D_ACCESSOR_ATTRIBUTE("Enable Motor", GetEnableMotor, SetEnableMotor, false, AM_DEFAULT);
  27. URHO3D_ACCESSOR_ATTRIBUTE("Motor Speed", GetMotorSpeed, SetMotorSpeed, 0.0f, AM_DEFAULT);
  28. URHO3D_ACCESSOR_ATTRIBUTE("Max Motor Torque", GetMaxMotorTorque, SetMaxMotorTorque, 0.0f, AM_DEFAULT);
  29. URHO3D_COPY_BASE_ATTRIBUTES(Constraint2D);
  30. }
  31. void ConstraintRevolute2D::SetAnchor(const Vector2& anchor)
  32. {
  33. if (anchor == anchor_)
  34. return;
  35. anchor_ = anchor;
  36. RecreateJoint();
  37. MarkNetworkUpdate();
  38. }
  39. void ConstraintRevolute2D::SetEnableLimit(bool enableLimit)
  40. {
  41. if (enableLimit == jointDef_.enableLimit)
  42. return;
  43. jointDef_.enableLimit = enableLimit;
  44. if (joint_)
  45. static_cast<b2RevoluteJoint*>(joint_)->EnableLimit(enableLimit);
  46. else
  47. RecreateJoint();
  48. MarkNetworkUpdate();
  49. }
  50. void ConstraintRevolute2D::SetLowerAngle(float lowerAngle)
  51. {
  52. if (lowerAngle == jointDef_.lowerAngle)
  53. return;
  54. jointDef_.lowerAngle = lowerAngle;
  55. if (joint_)
  56. static_cast<b2RevoluteJoint*>(joint_)->SetLimits(lowerAngle, jointDef_.upperAngle);
  57. else
  58. RecreateJoint();
  59. MarkNetworkUpdate();
  60. }
  61. void ConstraintRevolute2D::SetUpperAngle(float upperAngle)
  62. {
  63. if (upperAngle == jointDef_.upperAngle)
  64. return;
  65. jointDef_.upperAngle = upperAngle;
  66. if (joint_)
  67. static_cast<b2RevoluteJoint*>(joint_)->SetLimits(jointDef_.lowerAngle, upperAngle);
  68. else
  69. RecreateJoint();
  70. MarkNetworkUpdate();
  71. }
  72. void ConstraintRevolute2D::SetEnableMotor(bool enableMotor)
  73. {
  74. if (enableMotor == jointDef_.enableMotor)
  75. return;
  76. jointDef_.enableMotor = enableMotor;
  77. if (joint_)
  78. static_cast<b2RevoluteJoint*>(joint_)->EnableMotor(enableMotor);
  79. else
  80. RecreateJoint();
  81. MarkNetworkUpdate();
  82. }
  83. void ConstraintRevolute2D::SetMotorSpeed(float motorSpeed)
  84. {
  85. if (motorSpeed == jointDef_.motorSpeed)
  86. return;
  87. jointDef_.motorSpeed = motorSpeed;
  88. if (joint_)
  89. static_cast<b2RevoluteJoint*>(joint_)->SetMotorSpeed(motorSpeed);
  90. else
  91. RecreateJoint();
  92. MarkNetworkUpdate();
  93. }
  94. void ConstraintRevolute2D::SetMaxMotorTorque(float maxMotorTorque)
  95. {
  96. if (maxMotorTorque == jointDef_.maxMotorTorque)
  97. return;
  98. jointDef_.maxMotorTorque = maxMotorTorque;
  99. if (joint_)
  100. static_cast<b2RevoluteJoint*>(joint_)->SetMaxMotorTorque(maxMotorTorque);
  101. else
  102. RecreateJoint();
  103. MarkNetworkUpdate();
  104. }
  105. b2JointDef* ConstraintRevolute2D::GetJointDef()
  106. {
  107. if (!ownerBody_ || !otherBody_)
  108. return nullptr;
  109. b2Body* bodyA = ownerBody_->GetBody();
  110. b2Body* bodyB = otherBody_->GetBody();
  111. if (!bodyA || !bodyB)
  112. return nullptr;
  113. jointDef_.Initialize(bodyA, bodyB, ToB2Vec2(anchor_));
  114. return &jointDef_;
  115. }
  116. }