FixedConstraintTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Constraints/FixedConstraintTest.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  7. #include <Jolt/Physics/Constraints/FixedConstraint.h>
  8. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  9. #include <Layers.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(FixedConstraintTest)
  11. {
  12. JPH_ADD_BASE_CLASS(FixedConstraintTest, Test)
  13. }
  14. void FixedConstraintTest::Initialize()
  15. {
  16. // Floor
  17. CreateFloor();
  18. float box_size = 4.0f;
  19. RefConst<Shape> box = new BoxShape(Vec3::sReplicate(0.5f * box_size));
  20. const int num_bodies = 10;
  21. // Build a collision group filter that disables collision between adjacent bodies
  22. Ref<GroupFilterTable> group_filter = new GroupFilterTable(num_bodies);
  23. for (CollisionGroup::SubGroupID i = 0; i < num_bodies - 1; ++i)
  24. group_filter->DisableCollision(i, i + 1);
  25. // Bodies attached through fixed constraints
  26. for (int randomness = 0; randomness < 2; ++randomness)
  27. {
  28. CollisionGroup::GroupID group_id = CollisionGroup::GroupID(randomness);
  29. Vec3 position(0, 25.0f, -randomness * 20.0f);
  30. Body &top = *mBodyInterface->CreateBody(BodyCreationSettings(box, position, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  31. top.SetCollisionGroup(CollisionGroup(group_filter, group_id, 0));
  32. mBodyInterface->AddBody(top.GetID(), EActivation::DontActivate);
  33. default_random_engine random;
  34. uniform_real_distribution<float> displacement(-1.0f, 1.0f);
  35. Body *prev = &top;
  36. for (int i = 1; i < num_bodies; ++i)
  37. {
  38. Quat rotation;
  39. if (randomness == 0)
  40. {
  41. position += Vec3(box_size, 0, 0);
  42. rotation = Quat::sIdentity();
  43. }
  44. else
  45. {
  46. position += Vec3(box_size + abs(displacement(random)), displacement(random), displacement(random));
  47. rotation = Quat::sRandom(random);
  48. }
  49. Body &segment = *mBodyInterface->CreateBody(BodyCreationSettings(box, position, rotation, EMotionType::Dynamic, Layers::MOVING));
  50. segment.SetCollisionGroup(CollisionGroup(group_filter, group_id, CollisionGroup::SubGroupID(i)));
  51. mBodyInterface->AddBody(segment.GetID(), EActivation::Activate);
  52. FixedConstraintSettings settings;
  53. settings.SetPoint(*prev, segment);
  54. Ref<Constraint> c = settings.Create(*prev, segment);
  55. mPhysicsSystem->AddConstraint(c);
  56. prev = &segment;
  57. }
  58. }
  59. {
  60. // Two light bodies attached to a heavy body (gives issues if the wrong anchor point is chosen)
  61. Body *light1 = mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.1f)), Vec3(-5.0f, 7.0f, -5.2f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  62. mBodyInterface->AddBody(light1->GetID(), EActivation::Activate);
  63. Body *heavy = mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(5.0f)), Vec3(-5.0f, 7.0f, 0.0f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  64. mBodyInterface->AddBody(heavy->GetID(), EActivation::Activate);
  65. Body *light2 = mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(0.1f)), Vec3(-5.0f, 7.0f, 5.2f), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  66. mBodyInterface->AddBody(light2->GetID(), EActivation::Activate);
  67. FixedConstraintSettings light1_heavy;
  68. light1_heavy.SetPoint(*light1, *heavy);
  69. mPhysicsSystem->AddConstraint(light1_heavy.Create(*light1, *heavy));
  70. FixedConstraintSettings heavy_light2;
  71. heavy_light2.SetPoint(*heavy, *light2);
  72. mPhysicsSystem->AddConstraint(heavy_light2.Create(*heavy, *light2));
  73. }
  74. {
  75. // A tower of beams and crossbeams (note that it is not recommended to make constructs with this many fixed constraints, this is not always stable)
  76. Vec3 base_position(0, 25, -40.0f);
  77. Quat base_rotation = Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI);
  78. Ref<BoxShape> pillar = new BoxShape(Vec3(0.1f, 1.0f, 0.1f), 0.0f);
  79. Ref<BoxShape> beam = new BoxShape(Vec3(0.01f, 1.5f, 0.1f), 0.0f);
  80. Body *prev_pillars[4] = { &Body::sFixedToWorld, &Body::sFixedToWorld, &Body::sFixedToWorld, &Body::sFixedToWorld };
  81. Vec3 center = Vec3::sZero();
  82. for (int y = 0; y < 10; ++y)
  83. {
  84. // Create pillars
  85. Body *pillars[4];
  86. for (int i = 0; i < 4; ++i)
  87. {
  88. Quat rotation = Quat::sRotation(Vec3::sAxisY(), i * 0.5f * JPH_PI);
  89. pillars[i] = mBodyInterface->CreateBody(BodyCreationSettings(pillar, base_position + base_rotation * (center + rotation * Vec3(1.0f, 1.0f, 1.0f)), base_rotation, EMotionType::Dynamic, Layers::MOVING));
  90. pillars[i]->SetCollisionGroup(CollisionGroup(group_filter, 0, 0)); // For convenience, we disable collisions between all objects in the tower
  91. mBodyInterface->AddBody(pillars[i]->GetID(), EActivation::Activate);
  92. }
  93. for (int i = 0; i < 4; ++i)
  94. {
  95. Quat rotation = Quat::sRotation(Vec3::sAxisY(), i * 0.5f * JPH_PI);
  96. // Create cross beam
  97. Body *cross = mBodyInterface->CreateBody(BodyCreationSettings(beam, base_position + base_rotation * (center + rotation * Vec3(1.105f, 1.0f, 0.0f)), base_rotation * rotation * Quat::sRotation(Vec3::sAxisX(), 0.3f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  98. cross->SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  99. mBodyInterface->AddBody(cross->GetID(), EActivation::Activate);
  100. // Attach cross beam to pillars
  101. for (int j = 0; j < 2; ++j)
  102. {
  103. FixedConstraintSettings constraint;
  104. constraint.SetPoint(*pillars[(i + j) % 4], *cross);
  105. mPhysicsSystem->AddConstraint(constraint.Create(*pillars[(i + j) % 4], *cross));
  106. }
  107. // Attach to previous pillar
  108. if (prev_pillars[i] != nullptr)
  109. {
  110. FixedConstraintSettings constraint;
  111. constraint.SetPoint(*prev_pillars[i], *pillars[i]);
  112. mPhysicsSystem->AddConstraint(constraint.Create(*prev_pillars[i], *pillars[i]));
  113. }
  114. prev_pillars[i] = pillars[i];
  115. }
  116. center += Vec3(0.0f, 2.0f, 0.0f);
  117. }
  118. // Create top
  119. Body *top = mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(1.2f, 0.1f, 1.2f)), base_position + base_rotation * (center + Vec3(0.0f, 0.1f, 0.0f)), base_rotation, EMotionType::Dynamic, Layers::MOVING));
  120. top->SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  121. mBodyInterface->AddBody(top->GetID(), EActivation::Activate);
  122. // Attach top to pillars
  123. for (int i = 0; i < 4; ++i)
  124. {
  125. FixedConstraintSettings constraint;
  126. constraint.SetPoint(*prev_pillars[i], *top);
  127. mPhysicsSystem->AddConstraint(constraint.Create(*prev_pillars[i], *top));
  128. }
  129. }
  130. }