SoftBodyCreator.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 inGridSize, float inGridSpacing, bool inFixateCorners)
  8. {
  9. const float cOffset = -0.5f * inGridSpacing * (inGridSize - 1);
  10. // Create settings
  11. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  12. for (uint y = 0; y < inGridSize; ++y)
  13. for (uint x = 0; x < inGridSize; ++x)
  14. {
  15. SoftBodySharedSettings::Vertex v;
  16. v.mPosition = Float3(cOffset + x * inGridSpacing, 0.0f, cOffset + y * inGridSpacing);
  17. settings->mVertices.push_back(v);
  18. }
  19. // Function to get the vertex index of a point on the cloth
  20. auto vertex_index = [inGridSize](uint inX, uint inY) -> uint
  21. {
  22. return inX + inY * inGridSize;
  23. };
  24. if (inFixateCorners)
  25. {
  26. // Fixate corners
  27. settings->mVertices[vertex_index(0, 0)].mInvMass = 0.0f;
  28. settings->mVertices[vertex_index(inGridSize - 1, 0)].mInvMass = 0.0f;
  29. settings->mVertices[vertex_index(0, inGridSize - 1)].mInvMass = 0.0f;
  30. settings->mVertices[vertex_index(inGridSize - 1, inGridSize - 1)].mInvMass = 0.0f;
  31. }
  32. // Create edges
  33. for (uint y = 0; y < inGridSize; ++y)
  34. for (uint x = 0; x < inGridSize; ++x)
  35. {
  36. SoftBodySharedSettings::Edge e;
  37. e.mCompliance = 0.00001f;
  38. e.mVertex[0] = vertex_index(x, y);
  39. if (x < inGridSize - 1)
  40. {
  41. e.mVertex[1] = vertex_index(x + 1, y);
  42. settings->mEdgeConstraints.push_back(e);
  43. }
  44. if (y < inGridSize - 1)
  45. {
  46. e.mVertex[1] = vertex_index(x, y + 1);
  47. settings->mEdgeConstraints.push_back(e);
  48. }
  49. if (x < inGridSize - 1 && y < inGridSize - 1)
  50. {
  51. e.mVertex[1] = vertex_index(x + 1, y + 1);
  52. settings->mEdgeConstraints.push_back(e);
  53. e.mVertex[0] = vertex_index(x + 1, y);
  54. e.mVertex[1] = vertex_index(x, y + 1);
  55. settings->mEdgeConstraints.push_back(e);
  56. }
  57. }
  58. settings->CalculateEdgeLengths();
  59. // Create faces
  60. for (uint y = 0; y < inGridSize - 1; ++y)
  61. for (uint x = 0; x < inGridSize - 1; ++x)
  62. {
  63. SoftBodySharedSettings::Face f;
  64. f.mVertex[0] = vertex_index(x, y);
  65. f.mVertex[1] = vertex_index(x, y + 1);
  66. f.mVertex[2] = vertex_index(x + 1, y + 1);
  67. settings->AddFace(f);
  68. f.mVertex[1] = vertex_index(x + 1, y + 1);
  69. f.mVertex[2] = vertex_index(x + 1, y);
  70. settings->AddFace(f);
  71. }
  72. // Optimize the settings
  73. SoftBodySharedSettings::OptimizationResults results;
  74. settings->Optimize(results);
  75. return settings;
  76. }
  77. Ref<SoftBodySharedSettings> CreateCube(uint inGridSize, float inGridSpacing)
  78. {
  79. const Vec3 cOffset = Vec3::sReplicate(-0.5f * inGridSpacing * (inGridSize - 1));
  80. // Create settings
  81. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  82. for (uint z = 0; z < inGridSize; ++z)
  83. for (uint y = 0; y < inGridSize; ++y)
  84. for (uint x = 0; x < inGridSize; ++x)
  85. {
  86. SoftBodySharedSettings::Vertex v;
  87. (cOffset + Vec3::sReplicate(inGridSpacing) * Vec3(float(x), float(y), float(z))).StoreFloat3(&v.mPosition);
  88. settings->mVertices.push_back(v);
  89. }
  90. // Function to get the vertex index of a point on the cloth
  91. auto vertex_index = [inGridSize](uint inX, uint inY, uint inZ) -> uint
  92. {
  93. return inX + inY * inGridSize + inZ * inGridSize * inGridSize;
  94. };
  95. // Create edges
  96. for (uint z = 0; z < inGridSize; ++z)
  97. for (uint y = 0; y < inGridSize; ++y)
  98. for (uint x = 0; x < inGridSize; ++x)
  99. {
  100. SoftBodySharedSettings::Edge e;
  101. e.mVertex[0] = vertex_index(x, y, z);
  102. if (x < inGridSize - 1)
  103. {
  104. e.mVertex[1] = vertex_index(x + 1, y, z);
  105. settings->mEdgeConstraints.push_back(e);
  106. }
  107. if (y < inGridSize - 1)
  108. {
  109. e.mVertex[1] = vertex_index(x, y + 1, z);
  110. settings->mEdgeConstraints.push_back(e);
  111. }
  112. if (z < inGridSize - 1)
  113. {
  114. e.mVertex[1] = vertex_index(x, y, z + 1);
  115. settings->mEdgeConstraints.push_back(e);
  116. }
  117. }
  118. settings->CalculateEdgeLengths();
  119. // Tetrahedrons to fill a cube
  120. const int tetra_indices[6][4][3] = {
  121. { {0, 0, 0}, {0, 1, 1}, {0, 0, 1}, {1, 1, 1} },
  122. { {0, 0, 0}, {0, 1, 0}, {0, 1, 1}, {1, 1, 1} },
  123. { {0, 0, 0}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1} },
  124. { {0, 0, 0}, {1, 0, 1}, {1, 0, 0}, {1, 1, 1} },
  125. { {0, 0, 0}, {1, 1, 0}, {0, 1, 0}, {1, 1, 1} },
  126. { {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {1, 1, 1} }
  127. };
  128. // Create volume constraints
  129. for (uint z = 0; z < inGridSize - 1; ++z)
  130. for (uint y = 0; y < inGridSize - 1; ++y)
  131. for (uint x = 0; x < inGridSize - 1; ++x)
  132. for (uint t = 0; t < 6; ++t)
  133. {
  134. SoftBodySharedSettings::Volume v;
  135. for (uint i = 0; i < 4; ++i)
  136. v.mVertex[i] = vertex_index(x + tetra_indices[t][i][0], y + tetra_indices[t][i][1], z + tetra_indices[t][i][2]);
  137. settings->mVolumeConstraints.push_back(v);
  138. }
  139. settings->CalculateVolumeConstraintVolumes();
  140. // Create faces
  141. for (uint y = 0; y < inGridSize - 1; ++y)
  142. for (uint x = 0; x < inGridSize - 1; ++x)
  143. {
  144. SoftBodySharedSettings::Face f;
  145. // Face 1
  146. f.mVertex[0] = vertex_index(x, y, 0);
  147. f.mVertex[1] = vertex_index(x, y + 1, 0);
  148. f.mVertex[2] = vertex_index(x + 1, y + 1, 0);
  149. settings->AddFace(f);
  150. f.mVertex[1] = vertex_index(x + 1, y + 1, 0);
  151. f.mVertex[2] = vertex_index(x + 1, y, 0);
  152. settings->AddFace(f);
  153. // Face 2
  154. f.mVertex[0] = vertex_index(x, y, inGridSize - 1);
  155. f.mVertex[1] = vertex_index(x + 1, y + 1, inGridSize - 1);
  156. f.mVertex[2] = vertex_index(x, y + 1, inGridSize - 1);
  157. settings->AddFace(f);
  158. f.mVertex[1] = vertex_index(x + 1, y, inGridSize - 1);
  159. f.mVertex[2] = vertex_index(x + 1, y + 1, inGridSize - 1);
  160. settings->AddFace(f);
  161. // Face 3
  162. f.mVertex[0] = vertex_index(x, 0, y);
  163. f.mVertex[1] = vertex_index(x + 1, 0, y + 1);
  164. f.mVertex[2] = vertex_index(x, 0, y + 1);
  165. settings->AddFace(f);
  166. f.mVertex[1] = vertex_index(x + 1, 0, y);
  167. f.mVertex[2] = vertex_index(x + 1, 0, y + 1);
  168. settings->AddFace(f);
  169. // Face 4
  170. f.mVertex[0] = vertex_index(x, inGridSize - 1, y);
  171. f.mVertex[1] = vertex_index(x, inGridSize - 1, y + 1);
  172. f.mVertex[2] = vertex_index(x + 1, inGridSize - 1, y + 1);
  173. settings->AddFace(f);
  174. f.mVertex[1] = vertex_index(x + 1, inGridSize - 1, y + 1);
  175. f.mVertex[2] = vertex_index(x + 1, inGridSize - 1, y);
  176. settings->AddFace(f);
  177. // Face 5
  178. f.mVertex[0] = vertex_index(0, x, y);
  179. f.mVertex[1] = vertex_index(0, x, y + 1);
  180. f.mVertex[2] = vertex_index(0, x + 1, y + 1);
  181. settings->AddFace(f);
  182. f.mVertex[1] = vertex_index(0, x + 1, y + 1);
  183. f.mVertex[2] = vertex_index(0, x + 1, y);
  184. settings->AddFace(f);
  185. // Face 6
  186. f.mVertex[0] = vertex_index(inGridSize - 1, x, y);
  187. f.mVertex[1] = vertex_index(inGridSize - 1, x + 1, y + 1);
  188. f.mVertex[2] = vertex_index(inGridSize - 1, x, y + 1);
  189. settings->AddFace(f);
  190. f.mVertex[1] = vertex_index(inGridSize - 1, x + 1, y);
  191. f.mVertex[2] = vertex_index(inGridSize - 1, x + 1, y + 1);
  192. settings->AddFace(f);
  193. }
  194. // Optimize the settings
  195. SoftBodySharedSettings::OptimizationResults results;
  196. settings->Optimize(results);
  197. return settings;
  198. }
  199. Ref<SoftBodySharedSettings> CreateSphere(float inRadius, uint inNumTheta, uint inNumPhi)
  200. {
  201. // Create settings
  202. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  203. // Create vertices
  204. SoftBodySharedSettings::Vertex v;
  205. (inRadius * Vec3::sUnitSpherical(0, 0)).StoreFloat3(&v.mPosition);
  206. settings->mVertices.push_back(v);
  207. (inRadius * Vec3::sUnitSpherical(JPH_PI, 0)).StoreFloat3(&v.mPosition);
  208. settings->mVertices.push_back(v);
  209. for (uint theta = 1; theta < inNumTheta - 1; ++theta)
  210. for (uint phi = 0; phi < inNumPhi; ++phi)
  211. {
  212. (inRadius * Vec3::sUnitSpherical(JPH_PI * theta / (inNumTheta - 1), 2.0f * JPH_PI * phi / inNumPhi)).StoreFloat3(&v.mPosition);
  213. settings->mVertices.push_back(v);
  214. }
  215. // Function to get the vertex index of a point on the sphere
  216. auto vertex_index = [inNumTheta, inNumPhi](uint inTheta, uint inPhi) -> uint
  217. {
  218. if (inTheta == 0)
  219. return 0;
  220. else if (inTheta == inNumTheta - 1)
  221. return 1;
  222. else
  223. return 2 + (inTheta - 1) * inNumPhi + inPhi % inNumPhi;
  224. };
  225. // Create edge constraints
  226. for (uint phi = 0; phi < inNumPhi; ++phi)
  227. {
  228. for (uint theta = 0; theta < inNumTheta - 1; ++theta)
  229. {
  230. SoftBodySharedSettings::Edge e;
  231. e.mCompliance = 0.0001f;
  232. e.mVertex[0] = vertex_index(theta, phi);
  233. e.mVertex[1] = vertex_index(theta + 1, phi);
  234. settings->mEdgeConstraints.push_back(e);
  235. e.mVertex[1] = vertex_index(theta + 1, phi + 1);
  236. settings->mEdgeConstraints.push_back(e);
  237. if (theta > 0)
  238. {
  239. e.mVertex[1] = vertex_index(theta, phi + 1);
  240. settings->mEdgeConstraints.push_back(e);
  241. }
  242. }
  243. }
  244. settings->CalculateEdgeLengths();
  245. // Create faces
  246. SoftBodySharedSettings::Face f;
  247. for (uint phi = 0; phi < inNumPhi; ++phi)
  248. {
  249. for (uint theta = 0; theta < inNumTheta - 2; ++theta)
  250. {
  251. f.mVertex[0] = vertex_index(theta, phi);
  252. f.mVertex[1] = vertex_index(theta + 1, phi);
  253. f.mVertex[2] = vertex_index(theta + 1, phi + 1);
  254. settings->AddFace(f);
  255. if (theta > 0)
  256. {
  257. f.mVertex[1] = vertex_index(theta + 1, phi + 1);
  258. f.mVertex[2] = vertex_index(theta, phi + 1);
  259. settings->AddFace(f);
  260. }
  261. }
  262. f.mVertex[0] = vertex_index(inNumTheta - 2, phi + 1);
  263. f.mVertex[1] = vertex_index(inNumTheta - 2, phi);
  264. f.mVertex[2] = vertex_index(inNumTheta - 1, 0);
  265. settings->AddFace(f);
  266. }
  267. // Optimize the settings
  268. SoftBodySharedSettings::OptimizationResults results;
  269. settings->Optimize(results);
  270. return settings;
  271. }
  272. };