SoftBodyShape.cpp 15 KB

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