TransformedShape.cpp 4.4 KB

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