ChangeShapeTest.cpp 2.8 KB

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