SoftBodyCustomUpdateTest.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/SoftBody/SoftBodyCustomUpdateTest.h>
  6. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  7. #include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
  8. #include <Utils/SoftBodyCreator.h>
  9. #include <Layers.h>
  10. #include <Renderer/DebugRendererImp.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyCustomUpdateTest)
  12. {
  13. JPH_ADD_BASE_CLASS(SoftBodyCustomUpdateTest, Test)
  14. }
  15. void SoftBodyCustomUpdateTest::Initialize()
  16. {
  17. // Floor
  18. CreateFloor();
  19. // Create a body but do not add it to the physics system (we're updating it ourselves)
  20. SoftBodyCreationSettings sphere(SoftBodyCreator::CreateSphere(), RVec3(0, 5, 0), Quat::sIdentity(), Layers::MOVING);
  21. sphere.mPressure = 2000.0f;
  22. mBody = mBodyInterface->CreateSoftBody(sphere);
  23. }
  24. void SoftBodyCustomUpdateTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  25. {
  26. // Note that passing a variable delta time results in differences in behavior, usually you want to have a fixed time step.
  27. // For this demo we'll just clamp the delta time to 1/60th of a second and allow behavioral changes due to frame rate fluctuations.
  28. float dt = min(inParams.mDeltaTime, 1.0f / 60.0f);
  29. // Call the update now
  30. SoftBodyMotionProperties *mp = static_cast<SoftBodyMotionProperties *>(mBody->GetMotionProperties());
  31. mp->CustomUpdate(dt, *mBody, *mPhysicsSystem);
  32. #ifdef JPH_DEBUG_RENDERER
  33. // Draw it as well since it's not added to the world
  34. mBody->GetShape()->Draw(mDebugRenderer, mBody->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f), Color::sWhite, false, false);
  35. #else
  36. // Draw the vertices
  37. RMat44 com = mBody->GetCenterOfMassTransform();
  38. for (const SoftBodyVertex &v : mp->GetVertices())
  39. mDebugRenderer->DrawMarker(com * v.mPosition, Color::sRed, 0.1f);
  40. #endif // JPH_DEBUG_RENDERER
  41. }