SimulationShapeFilterTest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/General/SimulationShapeFilterTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  9. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  10. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  11. #include <Layers.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(SimulationShapeFilterTest)
  13. {
  14. JPH_ADD_BASE_CLASS(SimulationShapeFilterTest, Test)
  15. }
  16. SimulationShapeFilterTest::~SimulationShapeFilterTest()
  17. {
  18. // Unregister shape filter
  19. mPhysicsSystem->SetSimulationShapeFilter(nullptr);
  20. }
  21. void SimulationShapeFilterTest::Initialize()
  22. {
  23. // Register shape filter
  24. mPhysicsSystem->SetSimulationShapeFilter(&mShapeFilter);
  25. // Floor
  26. CreateFloor();
  27. // Platform
  28. mShapeFilter.mPlatformID = mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(5.0f, 0.5f, 5.0f)), RVec3(0, 7.5f, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  29. // Compound shape
  30. Ref<Shape> capsule = new CapsuleShape(2, 0.1f);
  31. capsule->SetUserData(1); // Don't want the capsule to collide with the platform
  32. Ref<Shape> sphere = new SphereShape(0.5f);
  33. sphere->SetUserData(1); // Don't want the sphere to collide with the platform
  34. Ref<Shape> box = new BoxShape(Vec3::sReplicate(0.5f));
  35. Ref<StaticCompoundShapeSettings> compound = new StaticCompoundShapeSettings;
  36. compound->AddShape(Vec3::sZero(), Quat::sIdentity(), capsule);
  37. compound->AddShape(Vec3(0, -2, 0), Quat::sIdentity(), sphere);
  38. compound->AddShape(Vec3(0, 2, 0), Quat::sIdentity(), box);
  39. mShapeFilter.mCompoundID = mBodyInterface->CreateAndAddBody(BodyCreationSettings(compound, RVec3(0, 15, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  40. }
  41. bool SimulationShapeFilterTest::Filter::ShouldCollide(const Shape *inShape1, const SubShapeID &inSubShapeIDOfShape1, const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const
  42. {
  43. // If the platform is colliding with the compound, filter out collisions where the shape has user data 1
  44. if (mBodyID1 == mPlatformID && mBodyID2 == mCompoundID)
  45. return inShape2->GetUserData() != 1;
  46. else if (mBodyID1 == mCompoundID && mBodyID2 == mPlatformID)
  47. return inShape1->GetUserData() != 1;
  48. return true;
  49. }