PlaneShape.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/Shape/PlaneShape.h>
  6. #include <Jolt/Physics/Collision/Shape/ConvexShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
  8. #include <Jolt/Physics/Collision/RayCast.h>
  9. #include <Jolt/Physics/Collision/ShapeCast.h>
  10. #include <Jolt/Physics/Collision/ShapeFilter.h>
  11. #include <Jolt/Physics/Collision/CastResult.h>
  12. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  13. #include <Jolt/Physics/Collision/TransformedShape.h>
  14. #include <Jolt/Physics/Collision/CollidePointResult.h>
  15. #include <Jolt/Physics/Collision/CollideSoftBodyVertexIterator.h>
  16. #include <Jolt/Core/Profiler.h>
  17. #include <Jolt/Core/StreamIn.h>
  18. #include <Jolt/Core/StreamOut.h>
  19. #include <Jolt/Geometry/Plane.h>
  20. #include <Jolt/ObjectStream/TypeDeclarations.h>
  21. #ifdef JPH_DEBUG_RENDERER
  22. #include <Jolt/Renderer/DebugRenderer.h>
  23. #endif // JPH_DEBUG_RENDERER
  24. JPH_NAMESPACE_BEGIN
  25. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(PlaneShapeSettings)
  26. {
  27. JPH_ADD_BASE_CLASS(PlaneShapeSettings, ShapeSettings)
  28. JPH_ADD_ATTRIBUTE(PlaneShapeSettings, mPlane)
  29. JPH_ADD_ATTRIBUTE(PlaneShapeSettings, mMaterial)
  30. JPH_ADD_ATTRIBUTE(PlaneShapeSettings, mHalfExtent)
  31. }
  32. ShapeSettings::ShapeResult PlaneShapeSettings::Create() const
  33. {
  34. if (mCachedResult.IsEmpty())
  35. Ref<Shape> shape = new PlaneShape(*this, mCachedResult);
  36. return mCachedResult;
  37. }
  38. inline static void sPlaneGetOrthogonalBasis(Vec3Arg inNormal, Vec3 &outPerp1, Vec3 &outPerp2)
  39. {
  40. outPerp1 = inNormal.Cross(Vec3::sAxisY()).NormalizedOr(Vec3::sAxisX());
  41. outPerp2 = outPerp1.Cross(inNormal).Normalized();
  42. outPerp1 = inNormal.Cross(outPerp2);
  43. }
  44. void PlaneShape::GetVertices(Vec3 *outVertices) const
  45. {
  46. // Create orthogonal basis
  47. Vec3 normal = mPlane.GetNormal();
  48. Vec3 perp1, perp2;
  49. sPlaneGetOrthogonalBasis(normal, perp1, perp2);
  50. // Scale basis
  51. perp1 *= mHalfExtent;
  52. perp2 *= mHalfExtent;
  53. // Calculate corners
  54. Vec3 point = -normal * mPlane.GetConstant();
  55. outVertices[0] = point + perp1 + perp2;
  56. outVertices[1] = point + perp1 - perp2;
  57. outVertices[2] = point - perp1 - perp2;
  58. outVertices[3] = point - perp1 + perp2;
  59. }
  60. void PlaneShape::CalculateLocalBounds()
  61. {
  62. // Get the vertices of the plane
  63. Vec3 vertices[4];
  64. GetVertices(vertices);
  65. // Encapsulate the vertices and a point mHalfExtent behind the plane
  66. mLocalBounds = AABox();
  67. Vec3 normal = mPlane.GetNormal();
  68. for (const Vec3 &v : vertices)
  69. {
  70. mLocalBounds.Encapsulate(v);
  71. mLocalBounds.Encapsulate(v - mHalfExtent * normal);
  72. }
  73. }
  74. PlaneShape::PlaneShape(const PlaneShapeSettings &inSettings, ShapeResult &outResult) :
  75. Shape(EShapeType::Plane, EShapeSubType::Plane, inSettings, outResult),
  76. mPlane(inSettings.mPlane),
  77. mMaterial(inSettings.mMaterial),
  78. mHalfExtent(inSettings.mHalfExtent)
  79. {
  80. if (!mPlane.GetNormal().IsNormalized())
  81. {
  82. outResult.SetError("Plane normal needs to be normalized!");
  83. return;
  84. }
  85. CalculateLocalBounds();
  86. outResult.Set(this);
  87. }
  88. MassProperties PlaneShape::GetMassProperties() const
  89. {
  90. // Object should always be static, return default mass properties
  91. return MassProperties();
  92. }
  93. void PlaneShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  94. {
  95. // Get the vertices of the plane
  96. Vec3 vertices[4];
  97. GetVertices(vertices);
  98. // Reverse if scale is inside out
  99. if (ScaleHelpers::IsInsideOut(inScale))
  100. {
  101. std::swap(vertices[0], vertices[3]);
  102. std::swap(vertices[1], vertices[2]);
  103. }
  104. // Transform them to world space
  105. outVertices.clear();
  106. Mat44 com = inCenterOfMassTransform.PreScaled(inScale);
  107. for (const Vec3 &v : vertices)
  108. outVertices.push_back(com * v);
  109. }
  110. #ifdef JPH_DEBUG_RENDERER
  111. void PlaneShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  112. {
  113. // Get the vertices of the plane
  114. Vec3 local_vertices[4];
  115. GetVertices(local_vertices);
  116. // Reverse if scale is inside out
  117. if (ScaleHelpers::IsInsideOut(inScale))
  118. {
  119. std::swap(local_vertices[0], local_vertices[3]);
  120. std::swap(local_vertices[1], local_vertices[2]);
  121. }
  122. // Transform them to world space
  123. RMat44 com = inCenterOfMassTransform.PreScaled(inScale);
  124. RVec3 vertices[4];
  125. for (uint i = 0; i < 4; ++i)
  126. vertices[i] = com * local_vertices[i];
  127. // Determine the color
  128. Color color = inUseMaterialColors? GetMaterial(SubShapeID())->GetDebugColor() : inColor;
  129. // Draw the plane
  130. if (inDrawWireframe)
  131. {
  132. inRenderer->DrawWireTriangle(vertices[0], vertices[1], vertices[2], color);
  133. inRenderer->DrawWireTriangle(vertices[0], vertices[2], vertices[3], color);
  134. }
  135. else
  136. {
  137. inRenderer->DrawTriangle(vertices[0], vertices[1], vertices[2], color, DebugRenderer::ECastShadow::On);
  138. inRenderer->DrawTriangle(vertices[0], vertices[2], vertices[3], color, DebugRenderer::ECastShadow::On);
  139. }
  140. }
  141. #endif // JPH_DEBUG_RENDERER
  142. bool PlaneShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  143. {
  144. JPH_PROFILE_FUNCTION();
  145. // Test starting inside of negative half space
  146. float distance = mPlane.SignedDistance(inRay.mOrigin);
  147. if (distance <= 0.0f)
  148. {
  149. ioHit.mFraction = 0.0f;
  150. ioHit.mSubShapeID2 = inSubShapeIDCreator.GetID();
  151. return true;
  152. }
  153. // Test ray parallel to plane
  154. float dot = inRay.mDirection.Dot(mPlane.GetNormal());
  155. if (dot == 0.0f)
  156. return false;
  157. // Calculate hit fraction
  158. float fraction = -distance / dot;
  159. if (fraction >= 0.0f && fraction < ioHit.mFraction)
  160. {
  161. ioHit.mFraction = fraction;
  162. ioHit.mSubShapeID2 = inSubShapeIDCreator.GetID();
  163. return true;
  164. }
  165. return false;
  166. }
  167. void PlaneShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  168. {
  169. JPH_PROFILE_FUNCTION();
  170. // Test shape filter
  171. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  172. return;
  173. // Inside solid half space?
  174. float distance = mPlane.SignedDistance(inRay.mOrigin);
  175. if (inRayCastSettings.mTreatConvexAsSolid
  176. && distance <= 0.0f // Inside plane
  177. && ioCollector.GetEarlyOutFraction() > 0.0f) // Willing to accept hits at fraction 0
  178. {
  179. // Hit at fraction 0
  180. RayCastResult hit;
  181. hit.mBodyID = TransformedShape::sGetBodyID(ioCollector.GetContext());
  182. hit.mFraction = 0.0f;
  183. hit.mSubShapeID2 = inSubShapeIDCreator.GetID();
  184. ioCollector.AddHit(hit);
  185. }
  186. float dot = inRay.mDirection.Dot(mPlane.GetNormal());
  187. if (dot != 0.0f // Parallel ray will not hit plane
  188. && (inRayCastSettings.mBackFaceModeConvex == EBackFaceMode::CollideWithBackFaces || dot < 0.0f)) // Back face culling
  189. {
  190. // Calculate hit with plane
  191. float fraction = -distance / dot;
  192. if (fraction >= 0.0f && fraction < ioCollector.GetEarlyOutFraction())
  193. {
  194. RayCastResult hit;
  195. hit.mBodyID = TransformedShape::sGetBodyID(ioCollector.GetContext());
  196. hit.mFraction = fraction;
  197. hit.mSubShapeID2 = inSubShapeIDCreator.GetID();
  198. ioCollector.AddHit(hit);
  199. }
  200. }
  201. }
  202. void PlaneShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  203. {
  204. JPH_PROFILE_FUNCTION();
  205. // Test shape filter
  206. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  207. return;
  208. // Check if the point is inside the plane
  209. if (mPlane.SignedDistance(inPoint) < 0.0f)
  210. ioCollector.AddHit({ TransformedShape::sGetBodyID(ioCollector.GetContext()), inSubShapeIDCreator.GetID() });
  211. }
  212. void PlaneShape::CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const
  213. {
  214. JPH_PROFILE_FUNCTION();
  215. // Convert plane to world space
  216. Plane plane = mPlane.Scaled(inScale).GetTransformed(inCenterOfMassTransform);
  217. for (CollideSoftBodyVertexIterator v = inVertices, sbv_end = inVertices + inNumVertices; v != sbv_end; ++v)
  218. if (v.GetInvMass() > 0.0f)
  219. {
  220. // Calculate penetration
  221. float penetration = -plane.SignedDistance(v.GetPosition());
  222. if (v.UpdatePenetration(penetration))
  223. v.SetCollision(plane, inCollidingShapeIndex);
  224. }
  225. }
  226. // This is a version of GetSupportingFace that returns a face that is large enough to cover the shape we're colliding with but not as large as the regular GetSupportedFace to avoid numerical precision issues
  227. inline static void sGetSupportingFace(const ConvexShape *inShape, Vec3Arg inShapeCOM, const Plane &inPlane, Mat44Arg inPlaneToWorld, ConvexShape::SupportingFace &outPlaneFace)
  228. {
  229. // Project COM of shape onto plane
  230. Plane world_plane = inPlane.GetTransformed(inPlaneToWorld);
  231. Vec3 center = world_plane.ProjectPointOnPlane(inShapeCOM);
  232. // Create orthogonal basis for the plane
  233. Vec3 normal = world_plane.GetNormal();
  234. Vec3 perp1, perp2;
  235. sPlaneGetOrthogonalBasis(normal, perp1, perp2);
  236. // Base the size of the face on the bounding box of the shape, ensuring that it is large enough to cover the entire shape
  237. float size = inShape->GetLocalBounds().GetSize().Length();
  238. perp1 *= size;
  239. perp2 *= size;
  240. // Emit the vertices
  241. outPlaneFace.resize(4);
  242. outPlaneFace[0] = center + perp1 + perp2;
  243. outPlaneFace[1] = center + perp1 - perp2;
  244. outPlaneFace[2] = center - perp1 - perp2;
  245. outPlaneFace[3] = center - perp1 + perp2;
  246. }
  247. void PlaneShape::sCastConvexVsPlane(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, [[maybe_unused]] const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  248. {
  249. JPH_PROFILE_FUNCTION();
  250. // Get the shapes
  251. JPH_ASSERT(inShapeCast.mShape->GetType() == EShapeType::Convex);
  252. JPH_ASSERT(inShape->GetType() == EShapeType::Plane);
  253. const ConvexShape *convex_shape = static_cast<const ConvexShape *>(inShapeCast.mShape);
  254. const PlaneShape *plane_shape = static_cast<const PlaneShape *>(inShape);
  255. // Shape cast is provided relative to COM of inShape, so all we need to do is transform our plane with inScale
  256. Plane plane = plane_shape->mPlane.Scaled(inScale);
  257. Vec3 normal = plane.GetNormal();
  258. // Get support function
  259. ConvexShape::SupportBuffer shape1_support_buffer;
  260. const ConvexShape::Support *shape1_support = convex_shape->GetSupportFunction(ConvexShape::ESupportMode::Default, shape1_support_buffer, inShapeCast.mScale);
  261. // Get the support point of the convex shape in the opposite direction of the plane normal in our local space
  262. Vec3 normal_in_convex_shape_space = inShapeCast.mCenterOfMassStart.Multiply3x3Transposed(normal);
  263. Vec3 support_point = inShapeCast.mCenterOfMassStart * shape1_support->GetSupport(-normal_in_convex_shape_space);
  264. float signed_distance = plane.SignedDistance(support_point);
  265. float convex_radius = shape1_support->GetConvexRadius();
  266. float penetration_depth = -signed_distance + convex_radius;
  267. float dot = inShapeCast.mDirection.Dot(normal);
  268. // Collision output
  269. Mat44 com_hit;
  270. Vec3 point1, point2;
  271. float fraction;
  272. // Do we start in collision?
  273. if (penetration_depth > 0.0f)
  274. {
  275. // Back face culling?
  276. if (inShapeCastSettings.mBackFaceModeConvex == EBackFaceMode::IgnoreBackFaces && dot > 0.0f)
  277. return;
  278. // Shallower hit?
  279. if (penetration_depth <= -ioCollector.GetEarlyOutFraction())
  280. return;
  281. // We're hitting at fraction 0
  282. fraction = 0.0f;
  283. // Get contact point
  284. com_hit = inCenterOfMassTransform2;
  285. point1 = inCenterOfMassTransform2 * (support_point - normal * convex_radius);
  286. point2 = inCenterOfMassTransform2 * (support_point - normal * signed_distance);
  287. }
  288. else if (dot < 0.0f) // Moving towards the plane?
  289. {
  290. // Calculate hit fraction
  291. fraction = penetration_depth / dot;
  292. JPH_ASSERT(fraction >= 0.0f);
  293. // Further than early out fraction?
  294. if (fraction >= ioCollector.GetEarlyOutFraction())
  295. return;
  296. // Get contact point
  297. com_hit = inCenterOfMassTransform2.PostTranslated(fraction * inShapeCast.mDirection);
  298. point1 = point2 = com_hit * (support_point - normal * convex_radius);
  299. }
  300. else
  301. {
  302. // Moving away from the plane
  303. return;
  304. }
  305. // Create cast result
  306. Vec3 penetration_axis_world = com_hit.Multiply3x3(-normal);
  307. bool back_facing = dot > 0.0f;
  308. ShapeCastResult result(fraction, point1, point2, penetration_axis_world, back_facing, inSubShapeIDCreator1.GetID(), inSubShapeIDCreator2.GetID(), TransformedShape::sGetBodyID(ioCollector.GetContext()));
  309. // Gather faces
  310. if (inShapeCastSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
  311. {
  312. // Get supporting face of convex shape
  313. Mat44 shape_to_world = com_hit * inShapeCast.mCenterOfMassStart;
  314. convex_shape->GetSupportingFace(SubShapeID(), normal_in_convex_shape_space, inShapeCast.mScale, shape_to_world, result.mShape1Face);
  315. // Get supporting face of plane
  316. if (!result.mShape1Face.empty())
  317. sGetSupportingFace(convex_shape, shape_to_world.GetTranslation(), plane, inCenterOfMassTransform2, result.mShape2Face);
  318. }
  319. // Notify the collector
  320. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
  321. ioCollector.AddHit(result);
  322. }
  323. struct PlaneShape::PSGetTrianglesContext
  324. {
  325. Float3 mVertices[4];
  326. bool mDone = false;
  327. };
  328. void PlaneShape::GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const
  329. {
  330. static_assert(sizeof(PSGetTrianglesContext) <= sizeof(GetTrianglesContext), "GetTrianglesContext too small");
  331. JPH_ASSERT(IsAligned(&ioContext, alignof(PSGetTrianglesContext)));
  332. PSGetTrianglesContext *context = new (&ioContext) PSGetTrianglesContext();
  333. // Get the vertices of the plane
  334. Vec3 vertices[4];
  335. GetVertices(vertices);
  336. // Reverse if scale is inside out
  337. if (ScaleHelpers::IsInsideOut(inScale))
  338. {
  339. std::swap(vertices[0], vertices[3]);
  340. std::swap(vertices[1], vertices[2]);
  341. }
  342. // Transform them to world space
  343. Mat44 com = Mat44::sRotationTranslation(inRotation, inPositionCOM).PreScaled(inScale);
  344. for (uint i = 0; i < 4; ++i)
  345. (com * vertices[i]).StoreFloat3(&context->mVertices[i]);
  346. }
  347. int PlaneShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
  348. {
  349. static_assert(cGetTrianglesMinTrianglesRequested >= 2, "cGetTrianglesMinTrianglesRequested is too small");
  350. JPH_ASSERT(inMaxTrianglesRequested >= cGetTrianglesMinTrianglesRequested);
  351. // Check if we're done
  352. PSGetTrianglesContext &context = (PSGetTrianglesContext &)ioContext;
  353. if (context.mDone)
  354. return 0;
  355. context.mDone = true;
  356. // 1st triangle
  357. outTriangleVertices[0] = context.mVertices[0];
  358. outTriangleVertices[1] = context.mVertices[1];
  359. outTriangleVertices[2] = context.mVertices[2];
  360. // 2nd triangle
  361. outTriangleVertices[3] = context.mVertices[0];
  362. outTriangleVertices[4] = context.mVertices[2];
  363. outTriangleVertices[5] = context.mVertices[3];
  364. if (outMaterials != nullptr)
  365. {
  366. // Get material
  367. const PhysicsMaterial *material = GetMaterial(SubShapeID());
  368. outMaterials[0] = material;
  369. outMaterials[1] = material;
  370. }
  371. return 2;
  372. }
  373. void PlaneShape::sCollideConvexVsPlane(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, [[maybe_unused]] const ShapeFilter &inShapeFilter)
  374. {
  375. JPH_PROFILE_FUNCTION();
  376. // Get the shapes
  377. JPH_ASSERT(inShape1->GetType() == EShapeType::Convex);
  378. JPH_ASSERT(inShape2->GetType() == EShapeType::Plane);
  379. const ConvexShape *shape1 = static_cast<const ConvexShape *>(inShape1);
  380. const PlaneShape *shape2 = static_cast<const PlaneShape *>(inShape2);
  381. // Transform the plane to the space of the convex shape
  382. Plane scaled_plane = shape2->mPlane.Scaled(inScale2);
  383. Plane plane = scaled_plane.GetTransformed(inCenterOfMassTransform1.InversedRotationTranslation() * inCenterOfMassTransform2);
  384. Vec3 normal = plane.GetNormal();
  385. // Get support function
  386. ConvexShape::SupportBuffer shape1_support_buffer;
  387. const ConvexShape::Support *shape1_support = shape1->GetSupportFunction(ConvexShape::ESupportMode::Default, shape1_support_buffer, inScale1);
  388. // Get the support point of the convex shape in the opposite direction of the plane normal
  389. Vec3 support_point = shape1_support->GetSupport(-normal);
  390. float signed_distance = plane.SignedDistance(support_point);
  391. float convex_radius = shape1_support->GetConvexRadius();
  392. float penetration_depth = -signed_distance + convex_radius;
  393. if (penetration_depth > -inCollideShapeSettings.mMaxSeparationDistance)
  394. {
  395. // Get contact point
  396. Vec3 point1 = inCenterOfMassTransform1 * (support_point - normal * convex_radius);
  397. Vec3 point2 = inCenterOfMassTransform1 * (support_point - normal * signed_distance);
  398. Vec3 penetration_axis_world = inCenterOfMassTransform1.Multiply3x3(-normal);
  399. // Create collision result
  400. CollideShapeResult result(point1, point2, penetration_axis_world, penetration_depth, inSubShapeIDCreator1.GetID(), inSubShapeIDCreator2.GetID(), TransformedShape::sGetBodyID(ioCollector.GetContext()));
  401. // Gather faces
  402. if (inCollideShapeSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
  403. {
  404. // Get supporting face of shape 1
  405. shape1->GetSupportingFace(SubShapeID(), normal, inScale1, inCenterOfMassTransform1, result.mShape1Face);
  406. // Get supporting face of shape 2
  407. if (!result.mShape1Face.empty())
  408. sGetSupportingFace(shape1, inCenterOfMassTransform1.GetTranslation(), scaled_plane, inCenterOfMassTransform2, result.mShape2Face);
  409. }
  410. // Notify the collector
  411. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
  412. ioCollector.AddHit(result);
  413. }
  414. }
  415. void PlaneShape::SaveBinaryState(StreamOut &inStream) const
  416. {
  417. Shape::SaveBinaryState(inStream);
  418. inStream.Write(mPlane);
  419. inStream.Write(mHalfExtent);
  420. }
  421. void PlaneShape::RestoreBinaryState(StreamIn &inStream)
  422. {
  423. Shape::RestoreBinaryState(inStream);
  424. inStream.Read(mPlane);
  425. inStream.Read(mHalfExtent);
  426. CalculateLocalBounds();
  427. }
  428. void PlaneShape::SaveMaterialState(PhysicsMaterialList &outMaterials) const
  429. {
  430. outMaterials = { mMaterial };
  431. }
  432. void PlaneShape::RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials)
  433. {
  434. JPH_ASSERT(inNumMaterials == 1);
  435. mMaterial = inMaterials[0];
  436. }
  437. void PlaneShape::sRegister()
  438. {
  439. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::Plane);
  440. f.mConstruct = []() -> Shape * { return new PlaneShape; };
  441. f.mColor = Color::sDarkRed;
  442. for (EShapeSubType s : sConvexSubShapeTypes)
  443. {
  444. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::Plane, sCollideConvexVsPlane);
  445. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::Plane, sCastConvexVsPlane);
  446. CollisionDispatch::sRegisterCastShape(EShapeSubType::Plane, s, CollisionDispatch::sReversedCastShape);
  447. CollisionDispatch::sRegisterCollideShape(EShapeSubType::Plane, s, CollisionDispatch::sReversedCollideShape);
  448. }
  449. }
  450. JPH_NAMESPACE_END