softbody.adoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. = Physics : SoftBody
  2. :revnumber: 2.0
  3. :revdate: 2020/07/24
  4. [WARNING]
  5. ====
  6. Writing in progress, checkout the forum for additional infos link:http://hub.jmonkeyengine.org/t/bullet-softbody-in-jme3-1/33110[http://hub.jmonkeyengine.org/t/bullet-softbody-in-jme3-1/33110]
  7. ====
  8. what is a soft body ?
  9. how a soft body is simulated ? ( node, link, face ; spring & damper )
  10. link:https://en.wikipedia.org/wiki/Soft_body_dynamics[https://en.wikipedia.org/wiki/Soft_body_dynamics]
  11. == Getting Started
  12. The dynamics libraries for softbody (.so , .dll …) isn't (yet) prebuild for your platform so you have to checkout the entire project from GitHub then build the project.
  13. === How to :
  14. . checkout the project from link:https://github.com/Dokthar/jmonkeyengine/tree/bullet_SoftBody[GitHub].
  15. . go into the file gradle.properties and set the line : buildNativeProject = true .
  16. . run the command : ./gradlew build
  17. . then build the sdk : ant -f ./sdk/ build-zip (or build the sdk installer)
  18. . create a new project and make sure you use the following libraries : jme3-bullet-native and jme3-bullet (instead of jme3-jBullet)
  19. == Simple Example
  20. [source,java]
  21. ----
  22. public void simpleInitApp() {
  23. softBodyAppState = new BulletSoftBodyAppState();
  24. getStateManager().attach(softBodyAppState);
  25. SoftBodyWorldInfo sbwi = softBodyAppState.getPhysicsSoftSpace().getWorldInfo();
  26. sbwi.setGravity(Vector3f.UNIT_Y.mult(-0.981f));
  27. Geometry bunny = new Geometry("b", StandfordBunny.getMesh().deepClone());
  28. Material matN = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
  29. bunny.setMaterial(matN);
  30. rootNode.attachChild(bunny);
  31. bunny.addControl(new SoftBodyControl(false));
  32. PhysicsSoftBody soft = bunny.getControl(SoftBodyControl.class);
  33. soft.generateClusters(8);
  34. soft.config().setDynamicFrictionCoef(0.8f);
  35. soft.config().setPoseMatchingCoef(0.2f);
  36. soft.config().setCollisionsFlags(PhysicsSoftBody.Config.CL_SS + PhysicsSoftBody.Config.CL_RS);
  37. soft.setPose(false, true);
  38. soft.setTotalMass(100, true);
  39. soft.randomizeConstraints();
  40. softBodyAppState.getPhysicsSoftSpace().add(soft);
  41. Box floorMesh = new Box(100f, 0.5f, 100f);
  42. Geometry floor = new Geometry("floor", floorMesh);
  43. floor.move(0, -5f, 0);
  44. floor.addControl(new RigidBodyControl(0));
  45. softBodyAppState.getPhysicsSpace().add(floor);
  46. }
  47. ----
  48. == Documentation
  49. soft mesh / surface
  50. Cluster ?
  51. (generate) bending constraint ?
  52. config collision flag ?
  53. === Ropes
  54. A rope can be created by creating a new SoftBody on a Mesh with Mesh.Mode.Lines.
  55. More information on creating xref:core:scene/custom_meshes.adoc[custom meshes].
  56. ==== Example
  57. [source,java]
  58. ----
  59. Mesh ropeMesh = new Mesh();
  60. Vector3f[] vertices = {
  61. new Vector3f(0.00f, 0, 0),
  62. new Vector3f(0.50f, 0, 0),
  63. new Vector3f(1.00f, 0, 0),
  64. new Vector3f(1.50f, 0, 0),
  65. new Vector3f(2.00f, 0, 0),
  66. new Vector3f(2.50f, 0, 0),
  67. new Vector3f(3.00f, 0, 0),
  68. new Vector3f(3.50f, 0, 0),
  69. new Vector3f(4.00f, 0, 0)};
  70. int[] indices = {
  71. 0, 1,
  72. 1, 2,
  73. 2, 3,
  74. 3, 4,
  75. 4, 5,
  76. 5, 6,
  77. 6, 7,
  78. 7, 8};
  79. ropeMesh.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
  80. ropeMesh.setBuffer(VertexBuffer.Type.Index, 2, BufferUtils.createIntBuffer(indices));
  81. ropeMesh.setMode(Mesh.Mode.Lines);
  82. ropeMesh.updateBound();
  83. ----
  84. Once you have a Line Mesh, you can do as follow :
  85. [source,java]
  86. ----
  87. Geometry t = new Geometry("softRope", ropeMesh);
  88. t.move(0, 5, 0);
  89. t.addControl(new SoftBodyControl(false));
  90. ----
  91. ==== Anchors
  92. An Anchors act like a weld between a SoftBody and a RigidBody.
  93. Adding a anchors to a soft body require to specify the SoftBody's node and the RigidBody to weld with.
  94. [source,java]
  95. ----
  96. soft.appendAnchor(nodeId, rigid);
  97. ----
  98. === Note
  99. An anchor bind a SoftBody's node (or Vertex) to a RigidBody, the node will be attached to a RigidBody even if there are space in between.
  100. [WARNING]
  101. ====
  102. Anchors are not yet Serialized
  103. ====
  104. ==== Example
  105. [source,java]
  106. ----
  107. Geometry t = new Geometry("softRope", ropeMesh);
  108. Box box = new Box(1f, 1f, 1f);
  109. Geometry b = new Geometry("rigidBox", box);
  110. t.move(0, 5, 0);
  111. b.move(3f, 5, 0);
  112. t.addControl(new SoftBodyControl(false));
  113. soft = t.getControl(SoftBodyControl.class);
  114. soft.setMass(0, 0); // make the first node static
  115. b.addControl(new RigidBodyControl(1));
  116. rigid = b.getControl(RigidBodyControl.class);
  117. softBodyAppState.getPhysicsSpace().add(rigid);
  118. softBodyAppState.getPhysicsSoftSpace().add(soft);
  119. soft.appendAnchor(8, rigid); //where 8 is the last node of the rope
  120. ----
  121. Anchors can be removed as below. Note that you have to keep the node and RigidBody bind with.
  122. [source,java]
  123. ----
  124. soft.removeAnchor(8,rigid);
  125. ----
  126. ==== Joints
  127. Joints require SoftBodies with Clusters ( see generateClusters(int k) ).
  128. [WARNING]
  129. ====
  130. joint don't use values pivotA & pivotB (not yet)
  131. ====
  132. === LinearJoint
  133. [source,java]
  134. ----
  135. public SoftLinearJoint(Vector3f position, PhysicsSoftBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB);
  136. public SoftLinearJoint(Vector3f position, PhysicsSoftBody nodeA, PhysicsSoftBody nodeB, Vector3f pivotA, Vector3f pivotB);
  137. ----
  138. === AngularJoint
  139. [source,java]
  140. ----
  141. public SoftAngularJoint(Vector3f axis, PhysicsSoftBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB);
  142. public SoftAngularJoint(Vector3f axis, PhysicsSoftBody nodeA, PhysicsSoftBody nodeB, Vector3f pivotA, Vector3f pivotB);
  143. ----
  144. ==== Examples
  145. === LinearJoint
  146. [source,java]
  147. ----
  148. Box box = new Box(3f, 1f, 3f);
  149. Geometry b = new Geometry("rigidBox", box);
  150. b.move(0, 5f, 0);
  151. b.addControl(new RigidBodyControl(1));
  152. rigid = b.getControl(RigidBodyControl.class);
  153. Torus torus = new Torus(20, 10, 1f, 1.5f);
  154. Geometry t = new Geometry("softTorus", torus);
  155. t.addControl(new SoftBodyControl(true, false));
  156. soft = t.getControl(SoftBodyControl.class);
  157. soft.generateBendingConstraints(4, soft.material());
  158. soft.generateClusters(4);
  159. softBodyAppState.getPhysicsSpace().add(rigid);
  160. softBodyAppState.getPhysicsSoftSpace().add(soft);
  161. joint = new SoftLinearJoint(Vector3f.UNIT_X, soft, rigid, new Vector3f(0f,2,0f), new Vector3f(0f,-2,0f));
  162. softBodyAppState.getPhysicsSoftSpace().add(joint);
  163. ----
  164. === AngularJoint
  165. [source,java]
  166. ----
  167. Box box = new Box(1f, 1f, 1f);
  168. Geometry b = new Geometry("rigidBox", box);
  169. b.move(0, 5f, 0);
  170. b.addControl(new RigidBodyControl(1));
  171. rigid = b.getControl(RigidBodyControl.class);
  172. Torus torus = new Torus(20, 10, 1f, 1.5f);
  173. Geometry t = new Geometry("softTorus", torus);
  174. t.addControl(new SoftBodyControl(true, false));
  175. soft = t.getControl(SoftBodyControl.class);
  176. soft.generateBendingConstraints(4, soft.material());
  177. soft.generateClusters(4);
  178. softBodyAppState.getPhysicsSpace().add(rigid);
  179. softBodyAppState.getPhysicsSoftSpace().add(soft);
  180. joint = new SoftAngularJoint(Vector3f.UNIT_X, soft, rigid, new Vector3f(0,0,2), new Vector3f(0, 0, 2));
  181. softBodyAppState.getPhysicsSoftSpace().add(joint);
  182. ----