SoftBodyCreator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. return settings;
  73. }
  74. Ref<SoftBodySharedSettings> CreateCube(uint inGridSize, float inGridSpacing)
  75. {
  76. const Vec3 cOffset = Vec3::sReplicate(-0.5f * inGridSpacing * (inGridSize - 1));
  77. // Create settings
  78. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  79. for (uint z = 0; z < inGridSize; ++z)
  80. for (uint y = 0; y < inGridSize; ++y)
  81. for (uint x = 0; x < inGridSize; ++x)
  82. {
  83. SoftBodySharedSettings::Vertex v;
  84. (cOffset + Vec3::sReplicate(inGridSpacing) * Vec3(float(x), float(y), float(z))).StoreFloat3(&v.mPosition);
  85. settings->mVertices.push_back(v);
  86. }
  87. // Function to get the vertex index of a point on the cloth
  88. auto vertex_index = [inGridSize](uint inX, uint inY, uint inZ) -> uint
  89. {
  90. return inX + inY * inGridSize + inZ * inGridSize * inGridSize;
  91. };
  92. // Create edges
  93. for (uint z = 0; z < inGridSize; ++z)
  94. for (uint y = 0; y < inGridSize; ++y)
  95. for (uint x = 0; x < inGridSize; ++x)
  96. {
  97. SoftBodySharedSettings::Edge e;
  98. e.mVertex[0] = vertex_index(x, y, z);
  99. if (x < inGridSize - 1)
  100. {
  101. e.mVertex[1] = vertex_index(x + 1, y, z);
  102. settings->mEdgeConstraints.push_back(e);
  103. }
  104. if (y < inGridSize - 1)
  105. {
  106. e.mVertex[1] = vertex_index(x, y + 1, z);
  107. settings->mEdgeConstraints.push_back(e);
  108. }
  109. if (z < inGridSize - 1)
  110. {
  111. e.mVertex[1] = vertex_index(x, y, z + 1);
  112. settings->mEdgeConstraints.push_back(e);
  113. }
  114. }
  115. settings->CalculateEdgeLengths();
  116. // Tetrahedrons to fill a cube
  117. const int tetra_indices[6][4][3] = {
  118. { {0, 0, 0}, {0, 1, 1}, {0, 0, 1}, {1, 1, 1} },
  119. { {0, 0, 0}, {0, 1, 0}, {0, 1, 1}, {1, 1, 1} },
  120. { {0, 0, 0}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1} },
  121. { {0, 0, 0}, {1, 0, 1}, {1, 0, 0}, {1, 1, 1} },
  122. { {0, 0, 0}, {1, 1, 0}, {0, 1, 0}, {1, 1, 1} },
  123. { {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {1, 1, 1} }
  124. };
  125. // Create volume constraints
  126. for (uint z = 0; z < inGridSize - 1; ++z)
  127. for (uint y = 0; y < inGridSize - 1; ++y)
  128. for (uint x = 0; x < inGridSize - 1; ++x)
  129. for (uint t = 0; t < 6; ++t)
  130. {
  131. SoftBodySharedSettings::Volume v;
  132. for (uint i = 0; i < 4; ++i)
  133. v.mVertex[i] = vertex_index(x + tetra_indices[t][i][0], y + tetra_indices[t][i][1], z + tetra_indices[t][i][2]);
  134. settings->mVolumeConstraints.push_back(v);
  135. }
  136. settings->CalculateVolumeConstraintVolumes();
  137. // Create faces
  138. for (uint y = 0; y < inGridSize - 1; ++y)
  139. for (uint x = 0; x < inGridSize - 1; ++x)
  140. {
  141. SoftBodySharedSettings::Face f;
  142. // Face 1
  143. f.mVertex[0] = vertex_index(x, y, 0);
  144. f.mVertex[1] = vertex_index(x, y + 1, 0);
  145. f.mVertex[2] = vertex_index(x + 1, y + 1, 0);
  146. settings->AddFace(f);
  147. f.mVertex[1] = vertex_index(x + 1, y + 1, 0);
  148. f.mVertex[2] = vertex_index(x + 1, y, 0);
  149. settings->AddFace(f);
  150. // Face 2
  151. f.mVertex[0] = vertex_index(x, y, inGridSize - 1);
  152. f.mVertex[1] = vertex_index(x + 1, y + 1, inGridSize - 1);
  153. f.mVertex[2] = vertex_index(x, y + 1, inGridSize - 1);
  154. settings->AddFace(f);
  155. f.mVertex[1] = vertex_index(x + 1, y, inGridSize - 1);
  156. f.mVertex[2] = vertex_index(x + 1, y + 1, inGridSize - 1);
  157. settings->AddFace(f);
  158. // Face 3
  159. f.mVertex[0] = vertex_index(x, 0, y);
  160. f.mVertex[1] = vertex_index(x + 1, 0, y + 1);
  161. f.mVertex[2] = vertex_index(x, 0, y + 1);
  162. settings->AddFace(f);
  163. f.mVertex[1] = vertex_index(x + 1, 0, y);
  164. f.mVertex[2] = vertex_index(x + 1, 0, y + 1);
  165. settings->AddFace(f);
  166. // Face 4
  167. f.mVertex[0] = vertex_index(x, inGridSize - 1, y);
  168. f.mVertex[1] = vertex_index(x, inGridSize - 1, y + 1);
  169. f.mVertex[2] = vertex_index(x + 1, inGridSize - 1, y + 1);
  170. settings->AddFace(f);
  171. f.mVertex[1] = vertex_index(x + 1, inGridSize - 1, y + 1);
  172. f.mVertex[2] = vertex_index(x + 1, inGridSize - 1, y);
  173. settings->AddFace(f);
  174. // Face 5
  175. f.mVertex[0] = vertex_index(0, x, y);
  176. f.mVertex[1] = vertex_index(0, x, y + 1);
  177. f.mVertex[2] = vertex_index(0, x + 1, y + 1);
  178. settings->AddFace(f);
  179. f.mVertex[1] = vertex_index(0, x + 1, y + 1);
  180. f.mVertex[2] = vertex_index(0, x + 1, y);
  181. settings->AddFace(f);
  182. // Face 6
  183. f.mVertex[0] = vertex_index(inGridSize - 1, x, y);
  184. f.mVertex[1] = vertex_index(inGridSize - 1, x + 1, y + 1);
  185. f.mVertex[2] = vertex_index(inGridSize - 1, x, y + 1);
  186. settings->AddFace(f);
  187. f.mVertex[1] = vertex_index(inGridSize - 1, x + 1, y);
  188. f.mVertex[2] = vertex_index(inGridSize - 1, x + 1, y + 1);
  189. settings->AddFace(f);
  190. }
  191. return settings;
  192. }
  193. Ref<SoftBodySharedSettings> CreateSphere(float inRadius, uint inNumTheta, uint inNumPhi)
  194. {
  195. // Create settings
  196. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  197. // Create vertices
  198. SoftBodySharedSettings::Vertex v;
  199. (inRadius * Vec3::sUnitSpherical(0, 0)).StoreFloat3(&v.mPosition);
  200. settings->mVertices.push_back(v);
  201. (inRadius * Vec3::sUnitSpherical(JPH_PI, 0)).StoreFloat3(&v.mPosition);
  202. settings->mVertices.push_back(v);
  203. for (uint theta = 1; theta < inNumTheta - 1; ++theta)
  204. for (uint phi = 0; phi < inNumPhi; ++phi)
  205. {
  206. (inRadius * Vec3::sUnitSpherical(JPH_PI * theta / (inNumTheta - 1), 2.0f * JPH_PI * phi / inNumPhi)).StoreFloat3(&v.mPosition);
  207. settings->mVertices.push_back(v);
  208. }
  209. // Function to get the vertex index of a point on the sphere
  210. auto vertex_index = [inNumTheta, inNumPhi](uint inTheta, uint inPhi) -> uint
  211. {
  212. if (inTheta == 0)
  213. return 0;
  214. else if (inTheta == inNumTheta - 1)
  215. return 1;
  216. else
  217. return 2 + (inTheta - 1) * inNumPhi + inPhi % inNumPhi;
  218. };
  219. // Create edge constraints
  220. for (uint phi = 0; phi < inNumPhi; ++phi)
  221. {
  222. for (uint theta = 0; theta < inNumTheta - 1; ++theta)
  223. {
  224. SoftBodySharedSettings::Edge e;
  225. e.mCompliance = 0.0001f;
  226. e.mVertex[0] = vertex_index(theta, phi);
  227. e.mVertex[1] = vertex_index(theta + 1, phi);
  228. settings->mEdgeConstraints.push_back(e);
  229. e.mVertex[1] = vertex_index(theta + 1, phi + 1);
  230. settings->mEdgeConstraints.push_back(e);
  231. if (theta > 0)
  232. {
  233. e.mVertex[1] = vertex_index(theta, phi + 1);
  234. settings->mEdgeConstraints.push_back(e);
  235. }
  236. }
  237. }
  238. settings->CalculateEdgeLengths();
  239. // Create faces
  240. SoftBodySharedSettings::Face f;
  241. for (uint phi = 0; phi < inNumPhi; ++phi)
  242. {
  243. for (uint theta = 0; theta < inNumTheta - 2; ++theta)
  244. {
  245. f.mVertex[0] = vertex_index(theta, phi);
  246. f.mVertex[1] = vertex_index(theta + 1, phi);
  247. f.mVertex[2] = vertex_index(theta + 1, phi + 1);
  248. settings->AddFace(f);
  249. if (theta > 0)
  250. {
  251. f.mVertex[1] = vertex_index(theta + 1, phi + 1);
  252. f.mVertex[2] = vertex_index(theta, phi + 1);
  253. settings->AddFace(f);
  254. }
  255. }
  256. f.mVertex[0] = vertex_index(inNumTheta - 2, phi + 1);
  257. f.mVertex[1] = vertex_index(inNumTheta - 2, phi);
  258. f.mVertex[2] = vertex_index(inNumTheta - 1, 0);
  259. settings->AddFace(f);
  260. }
  261. return settings;
  262. }
  263. };