TransformedShape.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Collision/TransformedShape.h>
  5. #include <Jolt/Physics/Collision/RayCast.h>
  6. #include <Jolt/Physics/Collision/ShapeCast.h>
  7. #include <Jolt/Physics/Collision/CastResult.h>
  8. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  9. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  10. #include <Jolt/Geometry/OrientedBox.h>
  11. JPH_NAMESPACE_BEGIN
  12. bool TransformedShape::CastRay(const RayCast &inRay, RayCastResult &ioHit) const
  13. {
  14. if (mShape != nullptr)
  15. {
  16. // Transform the ray to local space
  17. RayCast ray = inRay.Transformed(GetInverseCenterOfMassTransform());
  18. // Scale the ray
  19. Vec3 inv_scale = GetShapeScale().Reciprocal();
  20. ray.mOrigin *= inv_scale;
  21. ray.mDirection *= inv_scale;
  22. // Cast the ray on the shape
  23. SubShapeIDCreator sub_shape_id(mSubShapeIDCreator);
  24. if (mShape->CastRay(ray, sub_shape_id, ioHit))
  25. {
  26. // Set body ID on the hit result
  27. ioHit.mBodyID = mBodyID;
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. void TransformedShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  34. {
  35. if (mShape != nullptr)
  36. {
  37. // Set the context on the collector and filter
  38. ioCollector.SetContext(this);
  39. inShapeFilter.mBodyID2 = mBodyID;
  40. // Transform and scale the ray to local space
  41. RayCast ray = inRay.Transformed(GetInverseCenterOfMassTransform());
  42. // Scale the ray
  43. Vec3 inv_scale = GetShapeScale().Reciprocal();
  44. ray.mOrigin *= inv_scale;
  45. ray.mDirection *= inv_scale;
  46. // Cast the ray on the shape
  47. SubShapeIDCreator sub_shape_id(mSubShapeIDCreator);
  48. mShape->CastRay(ray, inRayCastSettings, sub_shape_id, ioCollector, inShapeFilter);
  49. }
  50. }
  51. void TransformedShape::CollidePoint(Vec3Arg inPoint, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  52. {
  53. if (mShape != nullptr)
  54. {
  55. // Set the context on the collector and filter
  56. ioCollector.SetContext(this);
  57. inShapeFilter.mBodyID2 = mBodyID;
  58. // Transform and scale the point to local space
  59. Vec3 point = (GetInverseCenterOfMassTransform() * inPoint) / GetShapeScale();
  60. // Do point collide on the shape
  61. SubShapeIDCreator sub_shape_id(mSubShapeIDCreator);
  62. mShape->CollidePoint(point, sub_shape_id, ioCollector, inShapeFilter);
  63. }
  64. }
  65. void TransformedShape::CollideShape(const Shape *inShape, Vec3Arg inShapeScale, Mat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  66. {
  67. if (mShape != nullptr)
  68. {
  69. // Set the context on the collector and filter
  70. ioCollector.SetContext(this);
  71. inShapeFilter.mBodyID2 = mBodyID;
  72. SubShapeIDCreator sub_shape_id1, sub_shape_id2(mSubShapeIDCreator);
  73. CollisionDispatch::sCollideShapeVsShape(inShape, mShape, inShapeScale, GetShapeScale(), inCenterOfMassTransform, GetCenterOfMassTransform(), sub_shape_id1, sub_shape_id2, inCollideShapeSettings, ioCollector, inShapeFilter);
  74. }
  75. }
  76. void TransformedShape::CastShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, CastShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  77. {
  78. if (mShape != nullptr)
  79. {
  80. // Set the context on the collector and filter
  81. ioCollector.SetContext(this);
  82. inShapeFilter.mBodyID2 = mBodyID;
  83. // Get center of mass of object we're casting against
  84. Mat44 center_of_mass_transform2 = GetCenterOfMassTransform();
  85. SubShapeIDCreator sub_shape_id1, sub_shape_id2(mSubShapeIDCreator);
  86. CollisionDispatch::sCastShapeVsShapeWorldSpace(inShapeCast, inShapeCastSettings, mShape, GetShapeScale(), inShapeFilter, center_of_mass_transform2, sub_shape_id1, sub_shape_id2, ioCollector);
  87. }
  88. }
  89. void TransformedShape::CollectTransformedShapes(const AABox &inBox, TransformedShapeCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  90. {
  91. if (mShape != nullptr)
  92. {
  93. // Set the context on the collector
  94. ioCollector.SetContext(this);
  95. mShape->CollectTransformedShapes(inBox, mShapePositionCOM, mShapeRotation, GetShapeScale(), mSubShapeIDCreator, ioCollector, inShapeFilter);
  96. }
  97. }
  98. void TransformedShape::GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox) const
  99. {
  100. if (mShape != nullptr)
  101. mShape->GetTrianglesStart(ioContext, inBox, mShapePositionCOM, mShapeRotation, GetShapeScale());
  102. }
  103. int TransformedShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
  104. {
  105. if (mShape != nullptr)
  106. return mShape->GetTrianglesNext(ioContext, inMaxTrianglesRequested, outTriangleVertices, outMaterials);
  107. else
  108. return 0;
  109. }
  110. JPH_NAMESPACE_END