AllowedDOFsTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/General/AllowedDOFsTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Constraints/DistanceConstraint.h>
  8. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  9. #include <Renderer/DebugRendererImp.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(AllowedDOFsTest)
  12. {
  13. JPH_ADD_BASE_CLASS(AllowedDOFsTest, Test)
  14. }
  15. void AllowedDOFsTest::Initialize()
  16. {
  17. // Floor
  18. CreateFloor();
  19. Vec3 box_size(0.5f, 1.0f, 2.0f);
  20. RefConst<Shape> box_shape = new BoxShape(box_size);
  21. for (int allowed_dofs = 1; allowed_dofs <= 0b111111; ++allowed_dofs)
  22. {
  23. float x = -35.0f + 10.0f * (allowed_dofs & 0b111);
  24. float z = -35.0f + 10.0f * ((allowed_dofs >> 3) & 0b111);
  25. // Create body
  26. BodyCreationSettings bcs(box_shape, RVec3(x, 10, z), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  27. bcs.mAllowedDOFs = (EAllowedDOFs)allowed_dofs;
  28. BodyID id = mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  29. mBodies.push_back(id);
  30. // Create a constraint
  31. DistanceConstraintSettings dcs;
  32. dcs.mPoint1 = bcs.mPosition + Vec3(5, 5, 5);
  33. dcs.mPoint2 = bcs.mPosition + box_size;
  34. dcs.mMinDistance = 0.0f;
  35. dcs.mMaxDistance = sqrt(3.0f) * 5.0f + 1.0f;
  36. mPhysicsSystem->AddConstraint(mBodyInterface->CreateConstraint(&dcs, BodyID(), id));
  37. }
  38. }
  39. void AllowedDOFsTest::PostPhysicsUpdate(float inDeltaTime)
  40. {
  41. // Draw degrees of freedom
  42. for (BodyID id : mBodies)
  43. {
  44. BodyLockRead body_lock(mPhysicsSystem->GetBodyLockInterface(), id);
  45. if (body_lock.Succeeded())
  46. {
  47. const Body &body = body_lock.GetBody();
  48. String allowed_dofs_str = "";
  49. EAllowedDOFs allowed_dofs = body.GetMotionProperties()->GetAllowedDOFs();
  50. if ((allowed_dofs & EAllowedDOFs::TranslationX) == EAllowedDOFs::TranslationX)
  51. allowed_dofs_str += "X ";
  52. if ((allowed_dofs & EAllowedDOFs::TranslationY) == EAllowedDOFs::TranslationY)
  53. allowed_dofs_str += "Y ";
  54. if ((allowed_dofs & EAllowedDOFs::TranslationZ) == EAllowedDOFs::TranslationZ)
  55. allowed_dofs_str += "Z ";
  56. if ((allowed_dofs & EAllowedDOFs::RotationX) == EAllowedDOFs::RotationX)
  57. allowed_dofs_str += "RX ";
  58. if ((allowed_dofs & EAllowedDOFs::RotationY) == EAllowedDOFs::RotationY)
  59. allowed_dofs_str += "RY ";
  60. if ((allowed_dofs & EAllowedDOFs::RotationZ) == EAllowedDOFs::RotationZ)
  61. allowed_dofs_str += "RZ ";
  62. DebugRenderer::sInstance->DrawText3D(body.GetPosition(), allowed_dofs_str, Color::sWhite);
  63. }
  64. }
  65. }