GearConstraintTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Constraints/GearConstraintTest.h>
  5. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  6. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  7. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  8. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  9. #include <Jolt/Physics/Constraints/HingeConstraint.h>
  10. #include <Jolt/Physics/Constraints/GearConstraint.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Layers.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(GearConstraintTest)
  14. {
  15. JPH_ADD_BASE_CLASS(GearConstraintTest, Test)
  16. }
  17. void GearConstraintTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. constexpr float cGearHalfWidth = 0.05f;
  22. constexpr float cGear1Radius = 0.5f;
  23. constexpr int cGear1NumTeeth = 100;
  24. constexpr float cGear2Radius = 2.0f;
  25. constexpr int cGear2NumTeeth = int(cGear1NumTeeth * cGear2Radius / cGear1Radius);
  26. constexpr float cToothThicknessBottom = 0.01f;
  27. constexpr float cToothThicknessTop = 0.005f;
  28. constexpr float cToothHeight = 0.02f;
  29. // Create a tooth
  30. Array<Vec3> tooth_points = {
  31. Vec3(0, cGearHalfWidth, cToothThicknessBottom),
  32. Vec3(0, -cGearHalfWidth, cToothThicknessBottom),
  33. Vec3(0, cGearHalfWidth, -cToothThicknessBottom),
  34. Vec3(0, -cGearHalfWidth, -cToothThicknessBottom),
  35. Vec3(cToothHeight, -cGearHalfWidth, cToothThicknessTop),
  36. Vec3(cToothHeight, cGearHalfWidth, cToothThicknessTop),
  37. Vec3(cToothHeight, -cGearHalfWidth, -cToothThicknessTop),
  38. Vec3(cToothHeight, cGearHalfWidth, -cToothThicknessTop),
  39. };
  40. ConvexHullShapeSettings tooth_settings(tooth_points);
  41. tooth_settings.SetEmbedded();
  42. // Create gear 1
  43. CylinderShapeSettings gear1_cylinder(cGearHalfWidth, cGear1Radius);
  44. gear1_cylinder.SetEmbedded();
  45. StaticCompoundShapeSettings gear1_settings;
  46. gear1_settings.SetEmbedded();
  47. gear1_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), &gear1_cylinder);
  48. for (int i = 0; i < cGear1NumTeeth; ++i)
  49. {
  50. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 2.0f * JPH_PI * i / cGear1NumTeeth);
  51. gear1_settings.AddShape(rotation * Vec3(cGear1Radius, 0, 0), rotation, &tooth_settings);
  52. }
  53. Vec3 gear1_initial_p(0, 3.0f, 0);
  54. Quat gear1_initial_r = Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI);
  55. Body *gear1 = mBodyInterface->CreateBody(BodyCreationSettings(&gear1_settings, gear1_initial_p, gear1_initial_r, EMotionType::Dynamic, Layers::MOVING));
  56. mBodyInterface->AddBody(gear1->GetID(), EActivation::Activate);
  57. // Create gear 2
  58. CylinderShapeSettings gear2_cylinder(cGearHalfWidth, cGear2Radius);
  59. gear2_cylinder.SetEmbedded();
  60. StaticCompoundShapeSettings gear2_settings;
  61. gear2_settings.SetEmbedded();
  62. gear2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), &gear2_cylinder);
  63. for (int i = 0; i < cGear2NumTeeth; ++i)
  64. {
  65. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 2.0f * JPH_PI * (i + 0.5f) / cGear2NumTeeth);
  66. gear2_settings.AddShape(rotation * Vec3(cGear2Radius, 0, 0), rotation, &tooth_settings);
  67. }
  68. Vec3 gear2_initial_p = gear1_initial_p + Vec3(cGear1Radius + cGear2Radius + cToothHeight, 0, 0);
  69. Quat gear2_initial_r = gear1_initial_r;
  70. Body *gear2 = mBodyInterface->CreateBody(BodyCreationSettings(&gear2_settings, gear2_initial_p, gear2_initial_r, EMotionType::Dynamic, Layers::MOVING));
  71. mBodyInterface->AddBody(gear2->GetID(), EActivation::Activate);
  72. // Create a hinge for gear 1
  73. HingeConstraintSettings hinge1;
  74. hinge1.mPoint1 = hinge1.mPoint2 = gear1_initial_p;
  75. hinge1.mHingeAxis1 = hinge1.mHingeAxis2 = Vec3::sAxisZ();
  76. hinge1.mNormalAxis1 = hinge1.mNormalAxis2 = Vec3::sAxisX();
  77. Constraint *hinge1_constraint = hinge1.Create(Body::sFixedToWorld, *gear1);
  78. mPhysicsSystem->AddConstraint(hinge1_constraint);
  79. // Create a hinge for gear 1
  80. HingeConstraintSettings hinge2;
  81. hinge2.mPoint1 = hinge2.mPoint2 = gear2_initial_p;
  82. hinge2.mHingeAxis1 = hinge2.mHingeAxis2 = Vec3::sAxisZ();
  83. hinge2.mNormalAxis1 = hinge2.mNormalAxis2 = Vec3::sAxisX();
  84. Constraint *hinge2_constraint = hinge2.Create(Body::sFixedToWorld, *gear2);
  85. mPhysicsSystem->AddConstraint(hinge2_constraint);
  86. // Disable collision between gears
  87. Ref<GroupFilterTable> group_filter = new GroupFilterTable(2);
  88. group_filter->DisableCollision(0, 1);
  89. gear1->SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  90. gear2->SetCollisionGroup(CollisionGroup(group_filter, 0, 1));
  91. // Create gear constraint to constrain the two bodies
  92. GearConstraintSettings gear;
  93. gear.mHingeAxis1 = hinge1.mHingeAxis1;
  94. gear.mHingeAxis2 = hinge2.mHingeAxis1;
  95. gear.SetRatio(cGear1NumTeeth, cGear2NumTeeth);
  96. GearConstraint *gear_constraint = static_cast<GearConstraint *>(gear.Create(*gear1, *gear2));
  97. gear_constraint->SetConstraints(hinge1_constraint, hinge2_constraint);
  98. mPhysicsSystem->AddConstraint(gear_constraint);
  99. // Give the gear a spin
  100. gear2->SetAngularVelocity(Vec3(0, 0, 3.0f));
  101. }