2
0

PathConstraintTest.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Constraints/PathConstraintTest.h>
  6. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Jolt/Physics/Constraints/PathConstraintPathHermite.h>
  10. #include <Application/DebugUI.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(PathConstraintTest)
  13. {
  14. JPH_ADD_BASE_CLASS(PathConstraintTest, Test)
  15. }
  16. EPathRotationConstraintType PathConstraintTest::sOrientationType = EPathRotationConstraintType::Free;
  17. void PathConstraintTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. {
  22. // Create spiral path
  23. Ref<PathConstraintPathHermite> path = new PathConstraintPathHermite;
  24. Vec3 normal(0, 1, 0);
  25. Array<Vec3> positions;
  26. for (float a = -0.1f * JPH_PI; a < 4.0f * JPH_PI; a += 0.1f * JPH_PI)
  27. positions.push_back(Vec3(5.0f * Cos(a), -a, 5.0f * Sin(a)));
  28. for (int i = 1; i < int(positions.size() - 1); ++i)
  29. {
  30. Vec3 tangent = 0.5f * (positions[i + 1] - positions[i - 1]);
  31. path->AddPoint(positions[i], tangent, normal);
  32. }
  33. mPaths[0] = path;
  34. // Dynamic base plate to which the path attaches
  35. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(5, 0.5f, 5)), RVec3(-10, 1, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  36. mBodyInterface->AddBody(body1.GetID(), EActivation::Activate);
  37. // Dynamic body attached to the path
  38. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(0.5f, 1.0f, 2.0f)), RVec3(-5, 15, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  39. body2.SetAllowSleeping(false);
  40. mBodyInterface->AddBody(body2.GetID(), EActivation::Activate);
  41. // Create path constraint
  42. PathConstraintSettings settings;
  43. settings.mPath = path;
  44. settings.mPathPosition = Vec3(0, 15, 0);
  45. settings.mRotationConstraintType = sOrientationType;
  46. mConstraints[0] = static_cast<PathConstraint *>(settings.Create(body1, body2));
  47. mPhysicsSystem->AddConstraint(mConstraints[0]);
  48. }
  49. {
  50. // Create circular path
  51. Ref<PathConstraintPathHermite> path = new PathConstraintPathHermite;
  52. path->SetIsLooping(true);
  53. Vec3 normal(0, 1, 0);
  54. Array<Vec3> positions;
  55. for (int i = -1; i < 11; ++i)
  56. {
  57. float a = 2.0f * JPH_PI * i / 10.0f;
  58. positions.push_back(Vec3(5.0f * Cos(a), 0.0f, 5.0f * Sin(a)));
  59. }
  60. for (int i = 1; i < int(positions.size() - 1); ++i)
  61. {
  62. Vec3 tangent = 0.5f * (positions[i + 1] - positions[i - 1]);
  63. path->AddPoint(positions[i], tangent, normal);
  64. }
  65. mPaths[1] = path;
  66. // Dynamic base plate to which the path attaches
  67. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(5, 0.5f, 5)), RVec3(10, 1, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  68. mBodyInterface->AddBody(body1.GetID(), EActivation::Activate);
  69. // Dynamic body attached to the path
  70. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(0.5f, 1.0f, 2.0f)), RVec3(15, 5, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  71. body2.SetAllowSleeping(false);
  72. mBodyInterface->AddBody(body2.GetID(), EActivation::Activate);
  73. // Create path constraint
  74. PathConstraintSettings settings;
  75. settings.mPath = path;
  76. settings.mPathPosition = Vec3(0, 5, 0);
  77. settings.mPathRotation = Quat::sRotation(Vec3::sAxisX(), -0.1f * JPH_PI);
  78. settings.mRotationConstraintType = sOrientationType;
  79. mConstraints[1] = static_cast<PathConstraint *>(settings.Create(body1, body2));
  80. mPhysicsSystem->AddConstraint(mConstraints[1]);
  81. }
  82. }
  83. void PathConstraintTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  84. {
  85. for (PathConstraint *c : mConstraints)
  86. {
  87. MotorSettings &motor_settings = c->GetPositionMotorSettings();
  88. motor_settings.SetForceLimit(sMaxMotorAcceleration / c->GetBody2()->GetMotionProperties()->GetInverseMass()); // F = m * a
  89. motor_settings.mSpringSettings.mFrequency = sFrequency;
  90. motor_settings.mSpringSettings.mDamping = sDamping;
  91. c->SetMaxFrictionForce(sMaxFrictionAcceleration / c->GetBody2()->GetMotionProperties()->GetInverseMass());
  92. }
  93. }
  94. void PathConstraintTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  95. {
  96. static Array<String> constraint_types = { "Free", "Tangent", "Normal", "Binormal", "Path", "Full" };
  97. inUI->CreateTextButton(inSubMenu, "Configuration Settings", [=]() {
  98. UIElement *configuration_settings = inUI->CreateMenu();
  99. inUI->CreateComboBox(configuration_settings, "Rotation Constraint", constraint_types, (int)sOrientationType, [=](int inItem) { sOrientationType = (EPathRotationConstraintType)inItem; });
  100. inUI->CreateTextButton(configuration_settings, "Accept Changes", [=]() { RestartTest(); });
  101. inUI->ShowMenu(configuration_settings);
  102. });
  103. inUI->CreateTextButton(inSubMenu, "Runtime Settings", [=]() {
  104. UIElement *runtime_settings = inUI->CreateMenu();
  105. inUI->CreateComboBox(runtime_settings, "Motor", { "Off", "Velocity", "Position" }, (int)mConstraints[0]->GetPositionMotorState(), [this](int inItem) { for (PathConstraint *c : mConstraints) c->SetPositionMotorState((EMotorState)inItem); });
  106. inUI->CreateSlider(runtime_settings, "Target Velocity (m/s)", mConstraints[0]->GetTargetVelocity(), -10.0f, 10.0f, 0.1f, [this](float inValue) { for (PathConstraint *c : mConstraints) c->SetTargetVelocity(inValue); });
  107. inUI->CreateSlider(runtime_settings, "Target Path Fraction", mConstraints[0]->GetTargetPathFraction(), 0, mPaths[0]->GetPathMaxFraction(), 0.1f, [this](float inValue) { for (PathConstraint *c : mConstraints) c->SetTargetPathFraction(inValue); });
  108. inUI->CreateSlider(runtime_settings, "Max Acceleration (m/s^2)", sMaxMotorAcceleration, 0.0f, 100.0f, 1.0f, [](float inValue) { sMaxMotorAcceleration = inValue; });
  109. inUI->CreateSlider(runtime_settings, "Frequency (Hz)", sFrequency, 0.0f, 20.0f, 0.1f, [](float inValue) { sFrequency = inValue; });
  110. inUI->CreateSlider(runtime_settings, "Damping", sDamping, 0.0f, 2.0f, 0.01f, [](float inValue) { sDamping = inValue; });
  111. inUI->CreateSlider(runtime_settings, "Max Friction Acceleration (m/s^2)", sMaxFrictionAcceleration, 0.0f, 10.0f, 0.1f, [](float inValue) { sMaxFrictionAcceleration = inValue; });
  112. inUI->ShowMenu(runtime_settings);
  113. });
  114. inUI->ShowMenu(inSubMenu);
  115. }