ConstraintPriorityTest.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Constraints/ConstraintPriorityTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Layers.h>
  9. #include <Renderer/DebugRendererImp.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(ConstraintPriorityTest)
  11. {
  12. JPH_ADD_BASE_CLASS(ConstraintPriorityTest, Test)
  13. }
  14. void ConstraintPriorityTest::Initialize()
  15. {
  16. float box_size = 1.0f;
  17. RefConst<Shape> box = new BoxShape(Vec3(0.5f * box_size, 0.2f, 0.2f));
  18. const int num_bodies = 20;
  19. // Bodies attached through fixed constraints
  20. for (int priority = 0; priority < 2; ++priority)
  21. {
  22. RVec3 position(0, 10.0f, 0.2f * priority);
  23. Body &top = *mBodyInterface->CreateBody(BodyCreationSettings(box, position, Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  24. mBodyInterface->AddBody(top.GetID(), EActivation::DontActivate);
  25. Body *prev = &top;
  26. for (int i = 1; i < num_bodies; ++i)
  27. {
  28. position += Vec3(box_size, 0, 0);
  29. Body &segment = *mBodyInterface->CreateBody(BodyCreationSettings(box, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::NON_MOVING)); // Putting all bodies in the NON_MOVING layer so they won't collide
  30. mBodyInterface->AddBody(segment.GetID(), EActivation::Activate);
  31. FixedConstraintSettings settings;
  32. settings.mAutoDetectPoint = true;
  33. settings.mConstraintPriority = priority == 0? i : num_bodies - i; // Priority is reversed for one chain compared to the other
  34. Ref<Constraint> c = settings.Create(*prev, segment);
  35. mPhysicsSystem->AddConstraint(c);
  36. mConstraints.push_back(static_cast<FixedConstraint *>(c.GetPtr()));
  37. prev = &segment;
  38. }
  39. }
  40. }
  41. void ConstraintPriorityTest::PostPhysicsUpdate(float inDeltaTime)
  42. {
  43. for (FixedConstraint *c : mConstraints)
  44. mDebugRenderer->DrawText3D(0.5f * (c->GetBody1()->GetCenterOfMassPosition() + c->GetBody2()->GetCenterOfMassPosition()), StringFormat("Priority: %d", c->GetConstraintPriority()), Color::sWhite, 0.2f);
  45. }