SoftBodyCreator.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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> CreateSphere(float inRadius, uint inNumTheta, uint inNumPhi, SoftBodySharedSettings::EBendType inBendType, const SoftBodySharedSettings::VertexAttributes &inVertexAttributes)
  57. {
  58. // Create settings
  59. SoftBodySharedSettings *settings = new SoftBodySharedSettings;
  60. // Create vertices
  61. // NOTE: This is not how you should create a soft body sphere, we explicitly use polar coordinates to make the vertices unevenly distributed.
  62. // Doing it this way tests the pressure algorithm as it receives non-uniform triangles. Better is to use uniform triangles,
  63. // see the use of DebugRenderer::Create8thSphere for an example.
  64. SoftBodySharedSettings::Vertex v;
  65. (inRadius * Vec3::sUnitSpherical(0, 0)).StoreFloat3(&v.mPosition);
  66. settings->mVertices.push_back(v);
  67. (inRadius * Vec3::sUnitSpherical(JPH_PI, 0)).StoreFloat3(&v.mPosition);
  68. settings->mVertices.push_back(v);
  69. for (uint theta = 1; theta < inNumTheta - 1; ++theta)
  70. for (uint phi = 0; phi < inNumPhi; ++phi)
  71. {
  72. (inRadius * Vec3::sUnitSpherical(JPH_PI * theta / (inNumTheta - 1), 2.0f * JPH_PI * phi / inNumPhi)).StoreFloat3(&v.mPosition);
  73. settings->mVertices.push_back(v);
  74. }
  75. // Function to get the vertex index of a point on the sphere
  76. auto vertex_index = [inNumTheta, inNumPhi](uint inTheta, uint inPhi) -> uint
  77. {
  78. if (inTheta == 0)
  79. return 0;
  80. else if (inTheta == inNumTheta - 1)
  81. return 1;
  82. else
  83. return 2 + (inTheta - 1) * inNumPhi + inPhi % inNumPhi;
  84. };
  85. // Create faces
  86. SoftBodySharedSettings::Face f;
  87. for (uint phi = 0; phi < inNumPhi; ++phi)
  88. {
  89. for (uint theta = 0; theta < inNumTheta - 2; ++theta)
  90. {
  91. f.mVertex[0] = vertex_index(theta, phi);
  92. f.mVertex[1] = vertex_index(theta + 1, phi);
  93. f.mVertex[2] = vertex_index(theta + 1, phi + 1);
  94. settings->AddFace(f);
  95. if (theta > 0)
  96. {
  97. f.mVertex[1] = vertex_index(theta + 1, phi + 1);
  98. f.mVertex[2] = vertex_index(theta, phi + 1);
  99. settings->AddFace(f);
  100. }
  101. }
  102. f.mVertex[0] = vertex_index(inNumTheta - 2, phi + 1);
  103. f.mVertex[1] = vertex_index(inNumTheta - 2, phi);
  104. f.mVertex[2] = vertex_index(inNumTheta - 1, 0);
  105. settings->AddFace(f);
  106. }
  107. // Create constraints
  108. settings->CreateConstraints(&inVertexAttributes, 1, inBendType);
  109. // Optimize the settings
  110. settings->Optimize();
  111. return settings;
  112. }
  113. };