ShapeFilterTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <TestFramework.h>
  2. #include <Tests/General/ShapeFilterTest.h>
  3. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  4. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  8. #include <Jolt/Physics/Collision/ShapeCast.h>
  9. #include <Renderer/DebugRendererImp.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(ShapeFilterTest)
  12. {
  13. JPH_ADD_BASE_CLASS(ShapeFilterTest, Test)
  14. }
  15. void ShapeFilterTest::Initialize()
  16. {
  17. // Create geometry to cast against
  18. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(20, 1, 3)), RVec3(0, -1, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::Activate);
  19. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3::sReplicate(3)), RVec3(0, 3, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::Activate);
  20. // Create shape to cast
  21. Ref box_shape = new BoxShapeSettings(Vec3::sReplicate(1));
  22. box_shape->mUserData = (uint64)ShapeIdentifier::Box;
  23. Ref sphere_shape = new SphereShapeSettings(1);
  24. sphere_shape->mUserData = (uint64)ShapeIdentifier::Sphere;
  25. StaticCompoundShapeSettings cast_shape;
  26. cast_shape.AddShape(Vec3(3, 2, 0), Quat::sIdentity(), box_shape);
  27. cast_shape.AddShape(Vec3(0, 0, 0), Quat::sIdentity(), sphere_shape);
  28. cast_shape.mUserData = (uint64)ShapeIdentifier::Compound;
  29. mCastShape = cast_shape.Create().Get();
  30. }
  31. void ShapeFilterTest::PostPhysicsUpdate(float inDeltaTime)
  32. {
  33. mElapsedTime += inDeltaTime;
  34. float phase = mElapsedTime;
  35. const RVec3 cast_origin = RVec3(Cos(phase) * 10, 10, 0);
  36. const Vec3 cast_motion = Vec3(0, -15, 0);
  37. ClosestHitCollisionCollector<CastShapeCollector> cast_shape_collector;
  38. class MyShapeFilter : public ShapeFilter
  39. {
  40. public:
  41. virtual bool ShouldCollide(const Shape *inShape1, const SubShapeID &inSubShapeID1, const Shape *inShape2, const SubShapeID &inSubShapeID2) const override
  42. {
  43. return inShape1->GetUserData() != mUserDataOfShapeToIgnore;
  44. }
  45. // We're not interested in the other overload as it is not used by ray casts
  46. using ShapeFilter::ShouldCollide;
  47. uint64 mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Sphere;
  48. };
  49. MyShapeFilter shape_filter;
  50. // Select which shape to ignore
  51. float shape_select = fmod(phase, 6.0f * JPH_PI);
  52. const char *text;
  53. if (shape_select < 2.0f * JPH_PI)
  54. {
  55. shape_filter.mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Box;
  56. text = "Box";
  57. }
  58. else if (shape_select < 4.0f * JPH_PI)
  59. {
  60. shape_filter.mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Sphere;
  61. text = "Sphere";
  62. }
  63. else
  64. {
  65. shape_filter.mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Compound;
  66. text = "Compound";
  67. }
  68. mDebugRenderer->DrawText3D(cast_origin, StringFormat("Ignoring shape: %s", text), Color::sWhite);
  69. // Do the cast
  70. mPhysicsSystem->GetNarrowPhaseQuery().CastShape(
  71. RShapeCast(mCastShape, Vec3::sReplicate(1), RMat44::sTranslation(cast_origin), cast_motion),
  72. ShapeCastSettings(),
  73. RVec3::sZero(),
  74. cast_shape_collector,
  75. { },
  76. { },
  77. { },
  78. shape_filter
  79. );
  80. // Show the result
  81. RVec3 cast_point;
  82. Color color;
  83. if (cast_shape_collector.HadHit())
  84. {
  85. cast_point = cast_origin + cast_motion * cast_shape_collector.mHit.mFraction;
  86. color = Color::sGreen;
  87. }
  88. else
  89. {
  90. cast_point = cast_origin + cast_motion;
  91. color = Color::sRed;
  92. }
  93. mDebugRenderer->DrawArrow(cast_origin, cast_point, Color::sOrange, 0.1f);
  94. JPH_IF_DEBUG_RENDERER(mCastShape->Draw(mDebugRenderer, RMat44::sTranslation(RVec3(cast_point)), Vec3::sReplicate(1.0f), color, false, true);)
  95. }