2
0

SoftBodyCreator.cpp 9.1 KB

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