softbody.adoc 6.9 KB

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