SoftBodyCreator.cpp 8.9 KB

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