SoftBodyShape.cpp 16 KB

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