LoadSnapshotTest.cpp 2.2 KB

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