CollisionDispatch.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Collision/CollisionDispatch.h>
  5. #include <Physics/Collision/Shape/StaticCompoundShape.h>
  6. #include <Physics/Collision/Shape/MutableCompoundShape.h>
  7. #include <Physics/Collision/Shape/ConvexShape.h>
  8. #include <Physics/Collision/Shape/MeshShape.h>
  9. #include <Physics/Collision/Shape/HeightFieldShape.h>
  10. #include <Physics/Collision/Shape/ScaledShape.h>
  11. #include <Physics/Collision/Shape/RotatedTranslatedShape.h>
  12. #include <Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  13. #include <Physics/Collision/RayCast.h>
  14. #include <Physics/Collision/ShapeCast.h>
  15. #include <Physics/Collision/CastResult.h>
  16. #include <Physics/Collision/ShapeFilter.h>
  17. namespace JPH {
  18. void CollisionDispatch::sCollideShapeVsShape(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector)
  19. {
  20. switch (inShape1->GetType())
  21. {
  22. case EShapeType::Convex:
  23. switch (inShape2->GetType())
  24. {
  25. case EShapeType::Convex:
  26. ConvexShape::sCollideConvexVsConvex(static_cast<const ConvexShape *>(inShape1), static_cast<const ConvexShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  27. break;
  28. case EShapeType::Mesh:
  29. MeshShape::sCollideConvexVsMesh(static_cast<const ConvexShape *>(inShape1), static_cast<const MeshShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  30. break;
  31. case EShapeType::HeightField:
  32. HeightFieldShape::sCollideConvexVsHeightField(static_cast<const ConvexShape *>(inShape1), static_cast<const HeightFieldShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  33. break;
  34. case EShapeType::StaticCompound:
  35. StaticCompoundShape::sCollideShapeVsCompound(inShape1, static_cast<const StaticCompoundShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  36. break;
  37. case EShapeType::MutableCompound:
  38. MutableCompoundShape::sCollideShapeVsCompound(inShape1, static_cast<const MutableCompoundShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  39. break;
  40. case EShapeType::Scaled:
  41. {
  42. const ScaledShape *scaled_shape2 = static_cast<const ScaledShape *>(inShape2);
  43. CollisionDispatch::sCollideShapeVsShape(inShape1, scaled_shape2->GetInnerShape(), inScale1, inScale2 * scaled_shape2->GetScale(), inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  44. }
  45. break;
  46. case EShapeType::RotatedTranslated:
  47. RotatedTranslatedShape::sCollideShapeVsRotatedTranslated(inShape1, static_cast<const RotatedTranslatedShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  48. break;
  49. case EShapeType::OffsetCenterOfMass:
  50. OffsetCenterOfMassShape::sCollideShapeVsOffsetCenterOfMassShape(inShape1, static_cast<const OffsetCenterOfMassShape *>(inShape2), inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  51. break;
  52. }
  53. break;
  54. case EShapeType::Mesh:
  55. case EShapeType::HeightField:
  56. JPH_ASSERT(false, "Mesh / height field shapes cannot be dynamic, ignoring!");
  57. break;
  58. case EShapeType::StaticCompound:
  59. StaticCompoundShape::sCollideCompoundVsShape(static_cast<const StaticCompoundShape *>(inShape1), inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  60. break;
  61. case EShapeType::MutableCompound:
  62. MutableCompoundShape::sCollideCompoundVsShape(static_cast<const MutableCompoundShape *>(inShape1), inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  63. break;
  64. case EShapeType::Scaled:
  65. {
  66. const ScaledShape *scaled_shape1 = static_cast<const ScaledShape *>(inShape1);
  67. CollisionDispatch::sCollideShapeVsShape(scaled_shape1->GetInnerShape(), inShape2, inScale1 * scaled_shape1->GetScale(), inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  68. }
  69. break;
  70. case EShapeType::RotatedTranslated:
  71. RotatedTranslatedShape::sCollideRotatedTranslatedVsShape(static_cast<const RotatedTranslatedShape *>(inShape1), inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  72. break;
  73. case EShapeType::OffsetCenterOfMass:
  74. OffsetCenterOfMassShape::sCollideOffsetCenterOfMassShapeVsShape(static_cast<const OffsetCenterOfMassShape *>(inShape1), inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  75. break;
  76. default:
  77. JPH_ASSERT(false);
  78. break;
  79. }
  80. }
  81. void CollisionDispatch::sCastShapeVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  82. {
  83. // Only test shape if it passes the shape filter
  84. if (!inShapeFilter.ShouldCollide(inSubShapeIDCreator1.GetID(), inSubShapeIDCreator2.GetID()))
  85. return;
  86. switch (inShapeCast.mShape->GetType())
  87. {
  88. case EShapeType::StaticCompound:
  89. case EShapeType::MutableCompound:
  90. CompoundShape::sCastCompoundShapeVsShape(inShapeCast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  91. break;
  92. case EShapeType::Convex:
  93. inShape->CastShape(inShapeCast, inShapeCastSettings, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  94. break;
  95. case EShapeType::Mesh:
  96. case EShapeType::HeightField:
  97. JPH_ASSERT(false, "Cannot cast a mesh / height field, ignoring!");
  98. break;
  99. case EShapeType::Scaled:
  100. {
  101. const ScaledShape *scaled_shape = static_cast<const ScaledShape *>(inShapeCast.mShape.GetPtr());
  102. ShapeCast scaled_cast(scaled_shape->GetInnerShape(), inShapeCast.mScale * scaled_shape->GetScale(), inShapeCast.mCenterOfMassStart, inShapeCast.mDirection);
  103. CollisionDispatch::sCastShapeVsShape(scaled_cast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  104. }
  105. break;
  106. case EShapeType::RotatedTranslated:
  107. RotatedTranslatedShape::sCastRotatedTranslatedShapeVsShape(inShapeCast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  108. break;
  109. case EShapeType::OffsetCenterOfMass:
  110. OffsetCenterOfMassShape::sCastOffsetCenterOfMassShapeVsShape(inShapeCast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  111. break;
  112. default:
  113. JPH_ASSERT(false);
  114. break;
  115. }
  116. }
  117. } // JPH