SoftBodyCreator.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Utils/SoftBodyCreator.h>
  6. namespace SoftBodyCreator {
  7. Ref<SoftBodySharedSettings> CreateCloth(uint inGridSizeX, uint inGridSizeZ, float inGridSpacing, const function<float(uint, uint)> &inVertexGetInvMass, SoftBodySharedSettings::EBendType inBendType, const SoftBodySharedSettings::VertexAttributes &inVertexAttributes)
  8. {
  9. const float cOffsetX = -0.5f * inGridSpacing * (inGridSizeX - 1);
  10. const float cOffsetZ = -0.5f * inGridSpacing * (inGridSizeZ - 1);
  11. // Create settings
  12. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  13. for (uint z = 0; z < inGridSizeZ; ++z)
  14. for (uint x = 0; x < inGridSizeX; ++x)
  15. {
  16. SoftBodySharedSettings::Vertex v;
  17. v.mPosition = Float3(cOffsetX + x * inGridSpacing, 0.0f, cOffsetZ + z * inGridSpacing);
  18. v.mInvMass = inVertexGetInvMass(x, z);
  19. settings->mVertices.push_back(v);
  20. }
  21. // Function to get the vertex index of a point on the cloth
  22. auto vertex_index = [inGridSizeX](uint inX, uint inY) -> uint
  23. {
  24. return inX + inY * inGridSizeX;
  25. };
  26. // Create faces
  27. for (uint z = 0; z < inGridSizeZ - 1; ++z)
  28. for (uint x = 0; x < inGridSizeX - 1; ++x)
  29. {
  30. SoftBodySharedSettings::Face f;
  31. f.mVertex[0] = vertex_index(x, z);
  32. f.mVertex[1] = vertex_index(x, z + 1);
  33. f.mVertex[2] = vertex_index(x + 1, z + 1);
  34. settings->AddFace(f);
  35. f.mVertex[1] = vertex_index(x + 1, z + 1);
  36. f.mVertex[2] = vertex_index(x + 1, z);
  37. settings->AddFace(f);
  38. }
  39. // Create edges
  40. settings->CreateConstraints(&inVertexAttributes, 1, inBendType);
  41. // Optimize the settings
  42. settings->Optimize();
  43. return settings;
  44. }
  45. Ref<SoftBodySharedSettings> CreateClothWithFixatedCorners(uint inGridSizeX, uint inGridSizeZ, float inGridSpacing)
  46. {
  47. auto inv_mass = [inGridSizeX, inGridSizeZ](uint inX, uint inZ) {
  48. return (inX == 0 && inZ == 0)
  49. || (inX == inGridSizeX - 1 && inZ == 0)
  50. || (inX == 0 && inZ == inGridSizeZ - 1)
  51. || (inX == inGridSizeX - 1 && inZ == inGridSizeZ - 1)? 0.0f : 1.0f;
  52. };
  53. return CreateCloth(inGridSizeX, inGridSizeZ, inGridSpacing, inv_mass);
  54. }
  55. Ref<SoftBodySharedSettings> CreateCube(uint inGridSize, float inGridSpacing)
  56. {
  57. const Vec3 cOffset = Vec3::sReplicate(-0.5f * inGridSpacing * (inGridSize - 1));
  58. // Create settings
  59. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  60. for (uint z = 0; z < inGridSize; ++z)
  61. for (uint y = 0; y < inGridSize; ++y)
  62. for (uint x = 0; x < inGridSize; ++x)
  63. {
  64. SoftBodySharedSettings::Vertex v;
  65. (cOffset + Vec3::sReplicate(inGridSpacing) * Vec3(float(x), float(y), float(z))).StoreFloat3(&v.mPosition);
  66. settings->mVertices.push_back(v);
  67. }
  68. // Function to get the vertex index of a point on the cloth
  69. auto vertex_index = [inGridSize](uint inX, uint inY, uint inZ) -> uint
  70. {
  71. return inX + inY * inGridSize + inZ * inGridSize * inGridSize;
  72. };
  73. // Create edges
  74. for (uint z = 0; z < inGridSize; ++z)
  75. for (uint y = 0; y < inGridSize; ++y)
  76. for (uint x = 0; x < inGridSize; ++x)
  77. {
  78. SoftBodySharedSettings::Edge e;
  79. e.mVertex[0] = vertex_index(x, y, z);
  80. if (x < inGridSize - 1)
  81. {
  82. e.mVertex[1] = vertex_index(x + 1, y, z);
  83. settings->mEdgeConstraints.push_back(e);
  84. }
  85. if (y < inGridSize - 1)
  86. {
  87. e.mVertex[1] = vertex_index(x, y + 1, z);
  88. settings->mEdgeConstraints.push_back(e);
  89. }
  90. if (z < inGridSize - 1)
  91. {
  92. e.mVertex[1] = vertex_index(x, y, z + 1);
  93. settings->mEdgeConstraints.push_back(e);
  94. }
  95. }
  96. settings->CalculateEdgeLengths();
  97. // Tetrahedrons to fill a cube
  98. const int tetra_indices[6][4][3] = {
  99. { {0, 0, 0}, {0, 1, 1}, {0, 0, 1}, {1, 1, 1} },
  100. { {0, 0, 0}, {0, 1, 0}, {0, 1, 1}, {1, 1, 1} },
  101. { {0, 0, 0}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1} },
  102. { {0, 0, 0}, {1, 0, 1}, {1, 0, 0}, {1, 1, 1} },
  103. { {0, 0, 0}, {1, 1, 0}, {0, 1, 0}, {1, 1, 1} },
  104. { {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {1, 1, 1} }
  105. };
  106. // Create volume constraints
  107. for (uint z = 0; z < inGridSize - 1; ++z)
  108. for (uint y = 0; y < inGridSize - 1; ++y)
  109. for (uint x = 0; x < inGridSize - 1; ++x)
  110. for (uint t = 0; t < 6; ++t)
  111. {
  112. SoftBodySharedSettings::Volume v;
  113. for (uint i = 0; i < 4; ++i)
  114. v.mVertex[i] = vertex_index(x + tetra_indices[t][i][0], y + tetra_indices[t][i][1], z + tetra_indices[t][i][2]);
  115. settings->mVolumeConstraints.push_back(v);
  116. }
  117. settings->CalculateVolumeConstraintVolumes();
  118. // Create faces
  119. for (uint y = 0; y < inGridSize - 1; ++y)
  120. for (uint x = 0; x < inGridSize - 1; ++x)
  121. {
  122. SoftBodySharedSettings::Face f;
  123. // Face 1
  124. f.mVertex[0] = vertex_index(x, y, 0);
  125. f.mVertex[1] = vertex_index(x, y + 1, 0);
  126. f.mVertex[2] = vertex_index(x + 1, y + 1, 0);
  127. settings->AddFace(f);
  128. f.mVertex[1] = vertex_index(x + 1, y + 1, 0);
  129. f.mVertex[2] = vertex_index(x + 1, y, 0);
  130. settings->AddFace(f);
  131. // Face 2
  132. f.mVertex[0] = vertex_index(x, y, inGridSize - 1);
  133. f.mVertex[1] = vertex_index(x + 1, y + 1, inGridSize - 1);
  134. f.mVertex[2] = vertex_index(x, y + 1, inGridSize - 1);
  135. settings->AddFace(f);
  136. f.mVertex[1] = vertex_index(x + 1, y, inGridSize - 1);
  137. f.mVertex[2] = vertex_index(x + 1, y + 1, inGridSize - 1);
  138. settings->AddFace(f);
  139. // Face 3
  140. f.mVertex[0] = vertex_index(x, 0, y);
  141. f.mVertex[1] = vertex_index(x + 1, 0, y + 1);
  142. f.mVertex[2] = vertex_index(x, 0, y + 1);
  143. settings->AddFace(f);
  144. f.mVertex[1] = vertex_index(x + 1, 0, y);
  145. f.mVertex[2] = vertex_index(x + 1, 0, y + 1);
  146. settings->AddFace(f);
  147. // Face 4
  148. f.mVertex[0] = vertex_index(x, inGridSize - 1, y);
  149. f.mVertex[1] = vertex_index(x, inGridSize - 1, y + 1);
  150. f.mVertex[2] = vertex_index(x + 1, inGridSize - 1, y + 1);
  151. settings->AddFace(f);
  152. f.mVertex[1] = vertex_index(x + 1, inGridSize - 1, y + 1);
  153. f.mVertex[2] = vertex_index(x + 1, inGridSize - 1, y);
  154. settings->AddFace(f);
  155. // Face 5
  156. f.mVertex[0] = vertex_index(0, x, y);
  157. f.mVertex[1] = vertex_index(0, x, y + 1);
  158. f.mVertex[2] = vertex_index(0, x + 1, y + 1);
  159. settings->AddFace(f);
  160. f.mVertex[1] = vertex_index(0, x + 1, y + 1);
  161. f.mVertex[2] = vertex_index(0, x + 1, y);
  162. settings->AddFace(f);
  163. // Face 6
  164. f.mVertex[0] = vertex_index(inGridSize - 1, x, y);
  165. f.mVertex[1] = vertex_index(inGridSize - 1, x + 1, y + 1);
  166. f.mVertex[2] = vertex_index(inGridSize - 1, x, y + 1);
  167. settings->AddFace(f);
  168. f.mVertex[1] = vertex_index(inGridSize - 1, x + 1, y);
  169. f.mVertex[2] = vertex_index(inGridSize - 1, x + 1, y + 1);
  170. settings->AddFace(f);
  171. }
  172. // Optimize the settings
  173. settings->Optimize();
  174. return settings;
  175. }
  176. Ref<SoftBodySharedSettings> CreateSphere(float inRadius, uint inNumTheta, uint inNumPhi)
  177. {
  178. // Create settings
  179. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  180. // Create vertices
  181. // NOTE: This is not how you should create a soft body sphere, we explicitly use polar coordinates to make the vertices unevenly distributed.
  182. // Doing it this way tests the pressure algorithm as it receives non-uniform triangles. Better is to use uniform triangles,
  183. // see the use of DebugRenderer::Create8thSphere for an example.
  184. SoftBodySharedSettings::Vertex v;
  185. (inRadius * Vec3::sUnitSpherical(0, 0)).StoreFloat3(&v.mPosition);
  186. settings->mVertices.push_back(v);
  187. (inRadius * Vec3::sUnitSpherical(JPH_PI, 0)).StoreFloat3(&v.mPosition);
  188. settings->mVertices.push_back(v);
  189. for (uint theta = 1; theta < inNumTheta - 1; ++theta)
  190. for (uint phi = 0; phi < inNumPhi; ++phi)
  191. {
  192. (inRadius * Vec3::sUnitSpherical(JPH_PI * theta / (inNumTheta - 1), 2.0f * JPH_PI * phi / inNumPhi)).StoreFloat3(&v.mPosition);
  193. settings->mVertices.push_back(v);
  194. }
  195. // Function to get the vertex index of a point on the sphere
  196. auto vertex_index = [inNumTheta, inNumPhi](uint inTheta, uint inPhi) -> uint
  197. {
  198. if (inTheta == 0)
  199. return 0;
  200. else if (inTheta == inNumTheta - 1)
  201. return 1;
  202. else
  203. return 2 + (inTheta - 1) * inNumPhi + inPhi % inNumPhi;
  204. };
  205. // Create faces
  206. SoftBodySharedSettings::Face f;
  207. for (uint phi = 0; phi < inNumPhi; ++phi)
  208. {
  209. for (uint theta = 0; theta < inNumTheta - 2; ++theta)
  210. {
  211. f.mVertex[0] = vertex_index(theta, phi);
  212. f.mVertex[1] = vertex_index(theta + 1, phi);
  213. f.mVertex[2] = vertex_index(theta + 1, phi + 1);
  214. settings->AddFace(f);
  215. if (theta > 0)
  216. {
  217. f.mVertex[1] = vertex_index(theta + 1, phi + 1);
  218. f.mVertex[2] = vertex_index(theta, phi + 1);
  219. settings->AddFace(f);
  220. }
  221. }
  222. f.mVertex[0] = vertex_index(inNumTheta - 2, phi + 1);
  223. f.mVertex[1] = vertex_index(inNumTheta - 2, phi);
  224. f.mVertex[2] = vertex_index(inNumTheta - 1, 0);
  225. settings->AddFace(f);
  226. }
  227. // Create edges
  228. SoftBodySharedSettings::VertexAttributes va;
  229. va.mCompliance = va.mShearCompliance = 1.0e-4f;
  230. settings->CreateConstraints(&va, 1);
  231. // Optimize the settings
  232. settings->Optimize();
  233. return settings;
  234. }
  235. };