SoftBodyShape.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/SoftBody/SoftBodyShape.h>
  6. #include <Jolt/Core/Profiler.h>
  7. #include <Jolt/Geometry/RayTriangle.h>
  8. #include <Jolt/Physics/Collision/RayCast.h>
  9. #include <Jolt/Physics/Collision/CastResult.h>
  10. #include <Jolt/Physics/Collision/TransformedShape.h>
  11. #include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
  12. #include <Jolt/Physics/Collision/CastConvexVsTriangles.h>
  13. #include <Jolt/Physics/Collision/CastSphereVsTriangles.h>
  14. #include <Jolt/Physics/Collision/CollideConvexVsTriangles.h>
  15. #include <Jolt/Physics/Collision/CollideSphereVsTriangles.h>
  16. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  17. #ifdef JPH_DEBUG_RENDERER
  18. #include <Jolt/Renderer/DebugRenderer.h>
  19. #endif // JPH_DEBUG_RENDERER
  20. JPH_NAMESPACE_BEGIN
  21. uint SoftBodyShape::GetSubShapeIDBits() const
  22. {
  23. // Ensure we have enough bits to encode our shape [0, n - 1]
  24. uint32 n = (uint32)mSoftBodyMotionProperties->GetFaces().size() - 1;
  25. return 32 - CountLeadingZeros(n);
  26. }
  27. AABox SoftBodyShape::GetLocalBounds() const
  28. {
  29. return mSoftBodyMotionProperties->GetLocalBounds();
  30. }
  31. bool SoftBodyShape::CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const
  32. {
  33. JPH_PROFILE_FUNCTION();
  34. uint num_triangle_bits = GetSubShapeIDBits();
  35. uint triangle_idx = uint(-1);
  36. const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
  37. for (const SoftBodyMotionProperties::Face &f : mSoftBodyMotionProperties->GetFaces())
  38. {
  39. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  40. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  41. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  42. float fraction = RayTriangle(inRay.mOrigin, inRay.mDirection, x1, x2, x3);
  43. if (fraction < ioHit.mFraction)
  44. {
  45. // Store fraction
  46. ioHit.mFraction = fraction;
  47. // Store triangle index
  48. triangle_idx = uint(&f - mSoftBodyMotionProperties->GetFaces().data());
  49. }
  50. }
  51. if (triangle_idx == uint(-1))
  52. return false;
  53. ioHit.mSubShapeID2 = inSubShapeIDCreator.PushID(triangle_idx, num_triangle_bits).GetID();
  54. return true;
  55. }
  56. void SoftBodyShape::CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  57. {
  58. JPH_PROFILE_FUNCTION();
  59. // Test shape filter
  60. if (!inShapeFilter.ShouldCollide(this, inSubShapeIDCreator.GetID()))
  61. return;
  62. uint num_triangle_bits = GetSubShapeIDBits();
  63. const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
  64. for (const SoftBodyMotionProperties::Face &f : mSoftBodyMotionProperties->GetFaces())
  65. {
  66. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  67. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  68. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  69. // Back facing check
  70. if (inRayCastSettings.mBackFaceMode == EBackFaceMode::IgnoreBackFaces && (x2 - x1).Cross(x3 - x1).Dot(inRay.mDirection) > 0.0f)
  71. return;
  72. // Test ray against triangle
  73. float fraction = RayTriangle(inRay.mOrigin, inRay.mDirection, x1, x2, x3);
  74. if (fraction < ioCollector.GetEarlyOutFraction())
  75. {
  76. // Better hit than the current hit
  77. RayCastResult hit;
  78. hit.mBodyID = TransformedShape::sGetBodyID(ioCollector.GetContext());
  79. hit.mFraction = fraction;
  80. hit.mSubShapeID2 = inSubShapeIDCreator.PushID(uint(&f - mSoftBodyMotionProperties->GetFaces().data()), num_triangle_bits).GetID();
  81. ioCollector.AddHit(hit);
  82. }
  83. }
  84. }
  85. void SoftBodyShape::CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter) const
  86. {
  87. sCollidePointUsingRayCast(*this, inPoint, inSubShapeIDCreator, ioCollector, inShapeFilter);
  88. }
  89. void SoftBodyShape::CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, SoftBodyVertex *ioVertices, uint inNumVertices, float inDeltaTime, Vec3Arg inDisplacementDueToGravity, int inCollidingShapeIndex) const
  90. {
  91. /* Not implemented */
  92. }
  93. const PhysicsMaterial *SoftBodyShape::GetMaterial(const SubShapeID &inSubShapeID) const
  94. {
  95. SubShapeID remainder;
  96. uint triangle_idx = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
  97. JPH_ASSERT(remainder.IsEmpty());
  98. const SoftBodyMotionProperties::Face &f = mSoftBodyMotionProperties->GetFace(triangle_idx);
  99. return mSoftBodyMotionProperties->GetMaterials()[f.mMaterialIndex];
  100. }
  101. Vec3 SoftBodyShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const
  102. {
  103. SubShapeID remainder;
  104. uint triangle_idx = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
  105. JPH_ASSERT(remainder.IsEmpty());
  106. const SoftBodyMotionProperties::Face &f = mSoftBodyMotionProperties->GetFace(triangle_idx);
  107. const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
  108. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  109. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  110. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  111. return (x2 - x1).Cross(x3 - x1).NormalizedOr(Vec3::sAxisY());
  112. }
  113. void SoftBodyShape::GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const
  114. {
  115. SubShapeID remainder;
  116. uint triangle_idx = inSubShapeID.PopID(GetSubShapeIDBits(), remainder);
  117. JPH_ASSERT(remainder.IsEmpty());
  118. const SoftBodyMotionProperties::Face &f = mSoftBodyMotionProperties->GetFace(triangle_idx);
  119. const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
  120. for (uint32 i : f.mVertex)
  121. outVertices.push_back(inCenterOfMassTransform * (inScale * vertices[i].mPosition));
  122. }
  123. void SoftBodyShape::GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const
  124. {
  125. outSubmergedVolume = 0.0f;
  126. outTotalVolume = mSoftBodyMotionProperties->GetVolume();
  127. outCenterOfBuoyancy = Vec3::sZero();
  128. }
  129. #ifdef JPH_DEBUG_RENDERER
  130. void SoftBodyShape::Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const
  131. {
  132. const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
  133. for (const SoftBodyMotionProperties::Face &f : mSoftBodyMotionProperties->GetFaces())
  134. {
  135. RVec3 x1 = inCenterOfMassTransform * vertices[f.mVertex[0]].mPosition;
  136. RVec3 x2 = inCenterOfMassTransform * vertices[f.mVertex[1]].mPosition;
  137. RVec3 x3 = inCenterOfMassTransform * vertices[f.mVertex[2]].mPosition;
  138. inRenderer->DrawTriangle(x1, x2, x3, inColor, DebugRenderer::ECastShadow::On);
  139. }
  140. }
  141. #endif // JPH_DEBUG_RENDERER
  142. struct SoftBodyShape::SBSGetTrianglesContext
  143. {
  144. Mat44 mCenterOfMassTransform;
  145. int mTriangleIndex;
  146. };
  147. void SoftBodyShape::GetTrianglesStart(GetTrianglesContext &ioContext, [[maybe_unused]] const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const
  148. {
  149. SBSGetTrianglesContext &context = reinterpret_cast<SBSGetTrianglesContext &>(ioContext);
  150. context.mCenterOfMassTransform = Mat44::sRotationTranslation(inRotation, inPositionCOM) * Mat44::sScale(inScale);
  151. context.mTriangleIndex = 0;
  152. }
  153. int SoftBodyShape::GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials) const
  154. {
  155. SBSGetTrianglesContext &context = reinterpret_cast<SBSGetTrianglesContext &>(ioContext);
  156. const Array<SoftBodyMotionProperties::Face> &faces = mSoftBodyMotionProperties->GetFaces();
  157. const Array<SoftBodyVertex> &vertices = mSoftBodyMotionProperties->GetVertices();
  158. const PhysicsMaterialList &materials = mSoftBodyMotionProperties->GetMaterials();
  159. int num_triangles = min(inMaxTrianglesRequested, (int)faces.size() - context.mTriangleIndex);
  160. for (int i = 0; i < num_triangles; ++i)
  161. {
  162. const SoftBodyMotionProperties::Face &f = faces[context.mTriangleIndex + i];
  163. Vec3 x1 = context.mCenterOfMassTransform * vertices[f.mVertex[0]].mPosition;
  164. Vec3 x2 = context.mCenterOfMassTransform * vertices[f.mVertex[1]].mPosition;
  165. Vec3 x3 = context.mCenterOfMassTransform * vertices[f.mVertex[2]].mPosition;
  166. x1.StoreFloat3(outTriangleVertices++);
  167. x2.StoreFloat3(outTriangleVertices++);
  168. x3.StoreFloat3(outTriangleVertices++);
  169. if (outMaterials != nullptr)
  170. *outMaterials++ = materials[f.mMaterialIndex];
  171. }
  172. context.mTriangleIndex += num_triangles;
  173. return num_triangles;
  174. }
  175. Shape::Stats SoftBodyShape::GetStats() const
  176. {
  177. return Stats(sizeof(*this), (uint)mSoftBodyMotionProperties->GetFaces().size());
  178. }
  179. float SoftBodyShape::GetVolume() const
  180. {
  181. return mSoftBodyMotionProperties->GetVolume();
  182. }
  183. void SoftBodyShape::sCollideConvexVsSoftBody(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)
  184. {
  185. JPH_ASSERT(inShape1->GetType() == EShapeType::Convex);
  186. const ConvexShape *shape1 = static_cast<const ConvexShape *>(inShape1);
  187. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::SoftBody);
  188. const SoftBodyShape *shape2 = static_cast<const SoftBodyShape *>(inShape2);
  189. const Array<SoftBodyVertex> &vertices = shape2->mSoftBodyMotionProperties->GetVertices();
  190. const Array<SoftBodyMotionProperties::Face> &faces = shape2->mSoftBodyMotionProperties->GetFaces();
  191. uint num_triangle_bits = shape2->GetSubShapeIDBits();
  192. CollideConvexVsTriangles collider(shape1, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1.GetID(), inCollideShapeSettings, ioCollector);
  193. for (const SoftBodyMotionProperties::Face &f : faces)
  194. {
  195. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  196. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  197. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  198. collider.Collide(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
  199. }
  200. }
  201. void SoftBodyShape::sCollideSphereVsSoftBody(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)
  202. {
  203. JPH_ASSERT(inShape1->GetSubType() == EShapeSubType::Sphere);
  204. const SphereShape *shape1 = static_cast<const SphereShape *>(inShape1);
  205. JPH_ASSERT(inShape2->GetSubType() == EShapeSubType::SoftBody);
  206. const SoftBodyShape *shape2 = static_cast<const SoftBodyShape *>(inShape2);
  207. const Array<SoftBodyVertex> &vertices = shape2->mSoftBodyMotionProperties->GetVertices();
  208. const Array<SoftBodyMotionProperties::Face> &faces = shape2->mSoftBodyMotionProperties->GetFaces();
  209. uint num_triangle_bits = shape2->GetSubShapeIDBits();
  210. CollideSphereVsTriangles collider(shape1, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1.GetID(), inCollideShapeSettings, ioCollector);
  211. for (const SoftBodyMotionProperties::Face &f : faces)
  212. {
  213. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  214. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  215. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  216. collider.Collide(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
  217. }
  218. }
  219. void SoftBodyShape::sCastConvexVsSoftBody(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)
  220. {
  221. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::SoftBody);
  222. const SoftBodyShape *shape = static_cast<const SoftBodyShape *>(inShape);
  223. const Array<SoftBodyVertex> &vertices = shape->mSoftBodyMotionProperties->GetVertices();
  224. const Array<SoftBodyMotionProperties::Face> &faces = shape->mSoftBodyMotionProperties->GetFaces();
  225. uint num_triangle_bits = shape->GetSubShapeIDBits();
  226. CastConvexVsTriangles caster(inShapeCast, inShapeCastSettings, inScale, inCenterOfMassTransform2, inSubShapeIDCreator1, ioCollector);
  227. for (const SoftBodyMotionProperties::Face &f : faces)
  228. {
  229. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  230. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  231. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  232. caster.Cast(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
  233. }
  234. }
  235. void SoftBodyShape::sCastSphereVsSoftBody(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)
  236. {
  237. JPH_ASSERT(inShape->GetSubType() == EShapeSubType::SoftBody);
  238. const SoftBodyShape *shape = static_cast<const SoftBodyShape *>(inShape);
  239. const Array<SoftBodyVertex> &vertices = shape->mSoftBodyMotionProperties->GetVertices();
  240. const Array<SoftBodyMotionProperties::Face> &faces = shape->mSoftBodyMotionProperties->GetFaces();
  241. uint num_triangle_bits = shape->GetSubShapeIDBits();
  242. CastSphereVsTriangles caster(inShapeCast, inShapeCastSettings, inScale, inCenterOfMassTransform2, inSubShapeIDCreator1, ioCollector);
  243. for (const SoftBodyMotionProperties::Face &f : faces)
  244. {
  245. Vec3 x1 = vertices[f.mVertex[0]].mPosition;
  246. Vec3 x2 = vertices[f.mVertex[1]].mPosition;
  247. Vec3 x3 = vertices[f.mVertex[2]].mPosition;
  248. caster.Cast(x1, x2, x3, 0b111, inSubShapeIDCreator2.PushID(uint(&f - faces.data()), num_triangle_bits).GetID());
  249. }
  250. }
  251. void SoftBodyShape::sRegister()
  252. {
  253. ShapeFunctions &f = ShapeFunctions::sGet(EShapeSubType::SoftBody);
  254. f.mConstruct = nullptr; // Not supposed to be constructed by users!
  255. f.mColor = Color::sDarkGreen;
  256. for (EShapeSubType s : sConvexSubShapeTypes)
  257. {
  258. CollisionDispatch::sRegisterCollideShape(s, EShapeSubType::SoftBody, sCollideConvexVsSoftBody);
  259. CollisionDispatch::sRegisterCastShape(s, EShapeSubType::SoftBody, sCastConvexVsSoftBody);
  260. }
  261. // Specialized collision functions
  262. CollisionDispatch::sRegisterCollideShape(EShapeSubType::Sphere, EShapeSubType::SoftBody, sCollideSphereVsSoftBody);
  263. CollisionDispatch::sRegisterCastShape(EShapeSubType::Sphere, EShapeSubType::SoftBody, sCastSphereVsSoftBody);
  264. }
  265. JPH_NAMESPACE_END