ChangeShapeTest.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/ChangeShapeTest.h>
  5. #include <Layers.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  9. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  10. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Application/DebugUI.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(ChangeShapeTest)
  14. {
  15. JPH_ADD_BASE_CLASS(ChangeShapeTest, Test)
  16. }
  17. void ChangeShapeTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. // Shapes
  22. mShapes.push_back(new BoxShape(Vec3(0.5f, 1.5f, 0.5f)));
  23. mShapes.push_back(new SphereShape(0.5f));
  24. mShapes.push_back(new CapsuleShape(1.0f, 0.5f));
  25. mShapes.push_back(TaperedCapsuleShapeSettings(1.0f, 0.5f, 0.3f).Create().Get());
  26. // Compound with center of mass shifted (this requires a correction of the position in the body)
  27. StaticCompoundShapeSettings compound_settings;
  28. compound_settings.AddShape(Vec3(0, 1.5f, 0), Quat::sIdentity(), new CapsuleShape(1.5f, 0.5f));
  29. compound_settings.AddShape(Vec3(0, 3, 0), Quat::sIdentity(), new SphereShape(1));
  30. RefConst<Shape> compound = compound_settings.Create().Get();
  31. mShapes.push_back(compound);
  32. // Create dynamic body that changes shape
  33. BodyCreationSettings settings;
  34. settings.SetShape(mShapes[mShapeIdx]);
  35. settings.mPosition = Vec3(0, 10, 0);
  36. settings.mMotionType = EMotionType::Dynamic;
  37. settings.mObjectLayer = Layers::MOVING;
  38. mBodyID = mBodyInterface->CreateBody(settings)->GetID();
  39. mBodyInterface->AddBody(mBodyID, EActivation::Activate);
  40. }
  41. void ChangeShapeTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  42. {
  43. const float cSwitchTime = 3.0f;
  44. // Increment time
  45. mTime += inParams.mDeltaTime;
  46. // Get new shape
  47. int shape_idx = int(mTime / cSwitchTime) % mShapes.size();
  48. // Change shape
  49. if (mShapeIdx != shape_idx)
  50. {
  51. mShapeIdx = shape_idx;
  52. mBodyInterface->SetShape(mBodyID, mShapes[mShapeIdx], true, mActivateAfterSwitch? EActivation::Activate : EActivation::DontActivate);
  53. }
  54. }
  55. void ChangeShapeTest::SaveState(StateRecorder &inStream) const
  56. {
  57. inStream.Write(mTime);
  58. inStream.Write(mShapeIdx);
  59. }
  60. void ChangeShapeTest::RestoreState(StateRecorder &inStream)
  61. {
  62. inStream.Read(mTime);
  63. inStream.Read(mShapeIdx);
  64. // Reset the shape to what was stored
  65. mBodyInterface->SetShape(mBodyID, mShapes[mShapeIdx], true, EActivation::DontActivate);
  66. }
  67. void ChangeShapeTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  68. {
  69. inUI->CreateCheckBox(inSubMenu, "Activate Body After Switch", mActivateAfterSwitch, [this](UICheckBox::EState inState) { mActivateAfterSwitch = inState == UICheckBox::STATE_CHECKED; });
  70. }