PulleyConstraintTest.cpp 2.0 KB

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