LoadSnapshotTest.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Tools/LoadSnapshotTest.h>
  5. #include <Jolt/Physics/PhysicsScene.h>
  6. #include <Jolt/Core/StreamWrapper.h>
  7. #include <Application/DebugUI.h>
  8. #include <Utils/Log.h>
  9. #include <Layers.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(LoadSnapshotTest)
  11. {
  12. JPH_ADD_BASE_CLASS(LoadSnapshotTest, Test)
  13. }
  14. void LoadSnapshotTest::Initialize()
  15. {
  16. ifstream stream("snapshot.bin", ifstream::in | ifstream::binary);
  17. if (!stream.is_open())
  18. FatalError("Unable to open 'snapshot.bin'");
  19. StreamInWrapper wrapper(stream);
  20. PhysicsScene::PhysicsSceneResult result = PhysicsScene::sRestoreFromBinaryState(wrapper);
  21. if (result.HasError())
  22. FatalError(result.GetError().c_str());
  23. Ref<PhysicsScene> scene = result.Get();
  24. // Determine quaternion that rotates the world so that up is Y
  25. Quat up_rotation;
  26. switch (sUpAxis)
  27. {
  28. case 0: up_rotation = Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI); break;
  29. case 2: up_rotation = Quat::sRotation(Vec3::sAxisX(), -0.5f * JPH_PI); break;
  30. default: up_rotation = Quat::sIdentity(); break;
  31. }
  32. for (BodyCreationSettings &settings : scene->GetBodies())
  33. {
  34. if (sOverrideLayers)
  35. {
  36. // Override the layer so that all static objects are in the non-moving layer and everything else is in the moving layer
  37. if (settings.mMotionType == EMotionType::Static)
  38. settings.mObjectLayer = Layers::NON_MOVING;
  39. else
  40. settings.mObjectLayer = Layers::MOVING;
  41. }
  42. // Rotate the body so that it matches Y is up
  43. settings.mPosition = up_rotation * settings.mPosition;
  44. settings.mRotation = up_rotation * settings.mRotation;
  45. }
  46. scene->CreateBodies(mPhysicsSystem);
  47. }
  48. void LoadSnapshotTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  49. {
  50. inUI->CreateComboBox(inSubMenu, "Up Axis", { "X", "Y", "Z" }, sUpAxis, [](int inItem) { sUpAxis = inItem; });
  51. inUI->CreateCheckBox(inSubMenu, "Override Object Layers", sOverrideLayers, [](UICheckBox::EState inState) { sOverrideLayers = inState == UICheckBox::STATE_CHECKED; });
  52. inUI->CreateTextButton(inSubMenu, "Accept Changes", [=]() { RestartTest(); });
  53. }