SoftBodySensorTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/SoftBodySensorTest.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/Shape/TaperedCylinderShape.h>
  10. #include <Jolt/Physics/SoftBody/SoftBodyManifold.h>
  11. #include <Utils/SoftBodyCreator.h>
  12. #include <Renderer/DebugRendererImp.h>
  13. #include <Layers.h>
  14. JPH_IMPLEMENT_RTTI_VIRTUAL(SoftBodySensorTest)
  15. {
  16. JPH_ADD_BASE_CLASS(SoftBodySensorTest, Test)
  17. }
  18. void SoftBodySensorTest::Initialize()
  19. {
  20. // Install contact listener for soft bodies
  21. mPhysicsSystem->SetSoftBodyContactListener(this);
  22. // Floor
  23. CreateFloor();
  24. // Create cloth that's fixated at the corners
  25. SoftBodyCreationSettings cloth(SoftBodyCreator::CreateClothWithFixatedCorners(), RVec3(0, 10.0f, 0), Quat::sIdentity(), Layers::MOVING);
  26. mBodyInterface->CreateAndAddSoftBody(cloth, EActivation::Activate);
  27. // Some sensors to detect the cloth
  28. BodyCreationSettings cylinder_sensor(new TaperedCylinderShapeSettings(4.0f, 1.0f, 2.0f), RVec3(0, 6, 0), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Static, Layers::SENSOR);
  29. cylinder_sensor.mIsSensor = true;
  30. mBodyInterface->CreateAndAddBody(cylinder_sensor, EActivation::DontActivate);
  31. BodyCreationSettings sphere_sensor(new SphereShape(4.0f), RVec3(4, 5, 0), Quat::sIdentity(), EMotionType::Static, Layers::SENSOR);
  32. sphere_sensor.mIsSensor = true;
  33. mBodyInterface->CreateAndAddBody(sphere_sensor, EActivation::DontActivate);
  34. // Sphere that falls on the cloth to check that we don't ignore this collision
  35. BodyCreationSettings bcs(new SphereShape(1.0f), RVec3(0, 15, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  36. bcs.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  37. bcs.mMassPropertiesOverride.mMass = 500.0f;
  38. mBodyInterface->CreateAndAddBody(bcs, EActivation::Activate);
  39. }
  40. void SoftBodySensorTest::OnSoftBodyContactAdded(const Body &inSoftBody, const SoftBodyManifold &inManifold)
  41. {
  42. // Draw the vertices that are in contact
  43. RMat44 com = inSoftBody.GetCenterOfMassTransform();
  44. for (const SoftBodyVertex &v : inManifold.GetVertices())
  45. if (inManifold.HasContact(v))
  46. DebugRenderer::sInstance->DrawMarker(com * v.mPosition, Color::sGreen, 0.1f);
  47. // Draw the sensors that are in contact with the soft body
  48. for (uint i = 0; i < inManifold.GetNumSensorContacts(); ++i)
  49. {
  50. BodyID sensor_id = inManifold.GetSensorContactBodyID(i);
  51. BodyLockRead lock(mPhysicsSystem->GetBodyLockInterfaceNoLock(), sensor_id); // Can't lock in a callback
  52. if (lock.SucceededAndIsInBroadPhase())
  53. {
  54. AABox bounds = lock.GetBody().GetWorldSpaceBounds();
  55. DebugRenderer::sInstance->DrawWireBox(bounds, Color::sGreen);
  56. }
  57. }
  58. }