SoftBodyForceTest.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/SoftBodyForceTest.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  8. #include <Utils/SoftBodyCreator.h>
  9. #include <Renderer/DebugRendererImp.h>
  10. #include <Math/Perlin.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyForceTest)
  13. {
  14. JPH_ADD_BASE_CLASS(SoftBodyForceTest, Test)
  15. }
  16. void SoftBodyForceTest::Initialize()
  17. {
  18. CreateFloor();
  19. static constexpr uint cGridSize = 30;
  20. // Create hanging cloth
  21. auto inv_mass = [](uint inX, uint inZ) {
  22. return (inX == 0 && inZ == 0)
  23. || (inX == cGridSize - 1 && inZ == 0)? 0.0f : 1.0f;
  24. };
  25. SoftBodyCreationSettings cloth(SoftBodyCreator::CreateCloth(cGridSize, cGridSize, 0.75f, inv_mass), RVec3(0, 15.0f, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), Layers::MOVING);
  26. mBodyID = mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  27. }
  28. void SoftBodyForceTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  29. {
  30. mTime += inParams.mDeltaTime;
  31. // Apply a fluctuating force
  32. constexpr float cMaxForce = 10000.0f;
  33. constexpr float cMaxAngle = DegreesToRadians(90.0f);
  34. Vec3 force(0, 0, 0.5f * cMaxForce * (1.0f + PerlinNoise3(0, 0, mTime / 2.0f, 256, 256, 256)));
  35. force = Mat44::sRotationY(cMaxAngle * PerlinNoise3(mTime / 10.0f, 0, 0, 256, 256, 256)) * force;
  36. mBodyInterface->AddForce(mBodyID, force);
  37. // Draw the force
  38. RVec3 offset(0, 10, 0);
  39. DebugRenderer::sInstance->DrawArrow(offset, offset + 10.0f * force.Normalized(), Color::sGreen, 0.1f);
  40. }
  41. void SoftBodyForceTest::SaveState(StateRecorder &inStream) const
  42. {
  43. inStream.Write(mTime);
  44. }
  45. void SoftBodyForceTest::RestoreState(StateRecorder &inStream)
  46. {
  47. inStream.Read(mTime);
  48. }