ConstraintPrismatic2D.cpp 4.3 KB

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