PulleyConstraintTest.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Constraints/PulleyConstraintTest.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Constraints/PulleyConstraint.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Layers.h>
  9. JPH_IMPLEMENT_RTTI_VIRTUAL(PulleyConstraintTest)
  10. {
  11. JPH_ADD_BASE_CLASS(PulleyConstraintTest, Test)
  12. }
  13. void PulleyConstraintTest::Initialize()
  14. {
  15. // Floor
  16. CreateFloor();
  17. // Variation 0: Max length (rope)
  18. // Variation 1: Fixed length (rigid rod)
  19. // Variation 2: Min/max length
  20. // Variation 3: With ratio (block and tackle)
  21. for (int variation = 0; variation < 4; ++variation)
  22. {
  23. Vec3 position1(-10, 10, -10.0f * variation);
  24. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.5f)), position1, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  25. mBodyInterface->AddBody(body1.GetID(), EActivation::Activate);
  26. Vec3 position2(10, 10, -10.0f * variation);
  27. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.5f)), position2, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  28. mBodyInterface->AddBody(body2.GetID(), EActivation::Activate);
  29. PulleyConstraintSettings settings;
  30. settings.mBodyPoint1 = position1 + Vec3(0, 0.5f, 0); // Connect at the top of the block
  31. settings.mBodyPoint2 = position2 + Vec3(0, 0.5f, 0);
  32. settings.mFixedPoint1 = settings.mBodyPoint1 + Vec3(0, 10, 0);
  33. settings.mFixedPoint2 = settings.mBodyPoint2 + Vec3(0, 10, 0);
  34. switch (variation)
  35. {
  36. case 0:
  37. // Can't extend but can contract
  38. break;
  39. case 1:
  40. // Fixed size
  41. settings.mMinLength = settings.mMaxLength = -1;
  42. break;
  43. case 2:
  44. // With range
  45. settings.mMinLength = 18.0f;
  46. settings.mMaxLength = 22.0f;
  47. break;
  48. case 3:
  49. // With ratio
  50. settings.mRatio = 4.0f;
  51. break;
  52. }
  53. mPhysicsSystem->AddConstraint(settings.Create(body1, body2));
  54. }
  55. }