LoadSnapshotTest.cpp 2.1 KB

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