ShapeFilterTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Not used in this example
  42. virtual bool ShouldCollide(const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  43. {
  44. return true;
  45. }
  46. virtual bool ShouldCollide(const Shape *inShape1, const SubShapeID &inSubShapeID1, const Shape *inShape2, const SubShapeID &inSubShapeID2) const override
  47. {
  48. return inShape1->GetUserData() != mUserDataOfShapeToIgnore;
  49. }
  50. uint64 mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Sphere;
  51. };
  52. MyShapeFilter shape_filter;
  53. // Select which shape to ignore
  54. float shape_select = fmod(phase, 6.0f * JPH_PI);
  55. const char *text;
  56. if (shape_select < 2.0f * JPH_PI)
  57. {
  58. shape_filter.mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Box;
  59. text = "Box";
  60. }
  61. else if (shape_select < 4.0f * JPH_PI)
  62. {
  63. shape_filter.mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Sphere;
  64. text = "Sphere";
  65. }
  66. else
  67. {
  68. shape_filter.mUserDataOfShapeToIgnore = (uint64)ShapeIdentifier::Compound;
  69. text = "Compound";
  70. }
  71. mDebugRenderer->DrawText3D(cast_origin, StringFormat("Ignoring shape: %s", text), Color::sWhite);
  72. // Do the cast
  73. mPhysicsSystem->GetNarrowPhaseQuery().CastShape(
  74. RShapeCast(mCastShape, Vec3::sReplicate(1), RMat44::sTranslation(cast_origin), cast_motion),
  75. ShapeCastSettings(),
  76. RVec3::sZero(),
  77. cast_shape_collector,
  78. { },
  79. { },
  80. { },
  81. shape_filter
  82. );
  83. // Show the result
  84. RVec3 cast_point;
  85. Color color;
  86. if (cast_shape_collector.HadHit())
  87. {
  88. cast_point = cast_origin + cast_motion * cast_shape_collector.mHit.mFraction;
  89. color = Color::sGreen;
  90. }
  91. else
  92. {
  93. cast_point = cast_origin + cast_motion;
  94. color = Color::sRed;
  95. }
  96. mDebugRenderer->DrawArrow(cast_origin, cast_point, Color::sOrange, 0.1f);
  97. JPH_IF_DEBUG_RENDERER(mCastShape->Draw(mDebugRenderer, RMat44::sTranslation(RVec3(cast_point)), Vec3::sReplicate(1.0f), color, false, true);)
  98. }