SoftBodyKinematicTest.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/SoftBody/SoftBodyKinematicTest.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. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodyKinematicTest)
  11. {
  12. JPH_ADD_BASE_CLASS(SoftBodyKinematicTest, Test)
  13. }
  14. void SoftBodyKinematicTest::Initialize()
  15. {
  16. // Floor
  17. CreateFloor();
  18. // A sphere
  19. Ref<SoftBodySharedSettings> sphere_settings = SoftBodyCreator::CreateSphere();
  20. sphere_settings->mVertices[0].mInvMass = 0.0f;
  21. sphere_settings->mVertices[0].mVelocity = Float3(0, 0, 5);
  22. SoftBodyCreationSettings sphere(sphere_settings, RVec3(0, 5, 0), Quat::sIdentity(), Layers::MOVING);
  23. sphere.mPressure = 2000.0f;
  24. mSphereID = mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  25. }
  26. void SoftBodyKinematicTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  27. {
  28. // Update the velocity of the first vertex
  29. BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), mSphereID);
  30. if (body_lock.Succeeded())
  31. {
  32. Body &body = body_lock.GetBody();
  33. SoftBodyMotionProperties *mp = static_cast<SoftBodyMotionProperties *>(body.GetMotionProperties());
  34. RVec3 com = body.GetCenterOfMassPosition();
  35. if (com.GetZ() >= 10.0f)
  36. mp->GetVertex(0).mVelocity = Vec3(0, 0, -5);
  37. else if (com.GetZ() <= -10.0f)
  38. mp->GetVertex(0).mVelocity = Vec3(0, 0, 5);
  39. }
  40. }