SoftBodyKinematicTest.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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));
  23. sphere.mObjectLayer = Layers::MOVING;
  24. sphere.mPressure = 2000.0f;
  25. mSphereID = mBodyInterface->CreateAndAddSoftBody(sphere, EActivation::Activate);
  26. }
  27. void SoftBodyKinematicTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  28. {
  29. // Update the velocity of the first vertex
  30. BodyLockWrite body_lock(mPhysicsSystem->GetBodyLockInterface(), mSphereID);
  31. if (body_lock.Succeeded())
  32. {
  33. Body &body = body_lock.GetBody();
  34. SoftBodyMotionProperties *mp = static_cast<SoftBodyMotionProperties *>(body.GetMotionProperties());
  35. RVec3 com = body.GetCenterOfMassPosition();
  36. if (com.GetZ() >= 10.0f)
  37. mp->GetVertex(0).mVelocity = Vec3(0, 0, -5);
  38. else if (com.GetZ() <= -10.0f)
  39. mp->GetVertex(0).mVelocity = Vec3(0, 0, 5);
  40. }
  41. }