ragdoll.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><meta name="keywords" content="documentation, physics, character, NPC, forces, collisions"><title>Ragdoll Physics</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/advanced/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/ragdoll.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>Ragdoll Physics</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#sample-code">Sample Code</a></li><li><a href="#preparing-the-physics-game">Preparing the Physics Game</a></li><li><a href="#creating-the-ragdoll">Creating the Ragdoll</a><ul class="sectlevel2"><li><a href="#limbs">Limbs</a></li><li><a href="#joints">Joints</a></li><li><a href="#attaching-everything-to-the-scene">Attaching Everything to the Scene</a></li></ul></li><li><a href="#applying-forces">Applying Forces</a></li><li><a href="#detecting-collisions">Detecting Collisions</a></li><li><a href="#best-practices">Best Practices</a></li><li><a href="#ragdoll-physics-using-kinematicragdollcontrol-deprecated-as-of-jmonkeyengine-v3-3">Ragdoll Physics using KinematicRagdollControl (deprecated as of JMonkeyEngine v3.3)</a><ul class="sectlevel2"><li><a href="#sample-code-2">Sample Code</a></li></ul></li><li><a href="#ragdoll-physics-using-dynamicanimcontrol-jme-3-3-and-later">Ragdoll Physics using DynamicAnimControl (JME 3.3 and later)</a><ul class="sectlevel2"><li><a href="#sample-code-3">Sample Code</a></li></ul></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>The jMonkeyEngine3 has built-in support for <a href="http://jbullet.advel.cz">jBullet physics</a> via the <code>com.jme3.bullet</code> package. Physics are not only responsible for handing collisions, but they also make <a href="../../jme3/advanced/hinges_and_joints.html">hinges and joints</a> possible. One special example of physical joints are ragdoll physics, shown here.</p></div>
  4. <div style="text-align: center;" class="imageblock"><div class="content"><img src="../../jme3/advanced/ragdoll.png" alt="ragdoll.png" width="" height=""></div></div></div></div>
  5. <div class="sect1"><h2 id="sample-code">Sample Code</h2><div class="sectionbody"><div class="ulist"><ul><li><p><a href="https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/bullet/TestRagDoll.java">TestRagDoll.java</a> (Tip: Click to pull the ragdoll up)</p></li></ul></div></div></div>
  6. <div class="sect1"><h2 id="preparing-the-physics-game">Preparing the Physics Game</h2><div class="sectionbody"><div class="olist arabic"><ol class="arabic"><li><p>Create a SimpleApplication with a <a href="../../jme3/advanced/physics.html">BulletAppState</a></p><div class="ulist"><ul><li><p>This gives us a PhysicsSpace for PhysicControls</p></li></ul></div></li><li><p>Add a physical floor (A box collision shape with mass zero)</p></li></ol></div></div></div>
  7. <div class="sect2"><h3 id="creating-the-ragdoll">Creating the Ragdoll</h3><div class="paragraph"><p>A ragdoll is a simple “person (dummy) that you build out of cylinder collision shapes. The ragdoll has 11 limbs: 1 for shoulders, 1 for the body, 1 for hips; plus 2 arms and 2 legs that are made up of two limbs each. In your game, you will likely replace the cylinders with your own (better looking) limb models. In this example here we just use simple cylinders.</p></div>
  8. <div class="sect2"><h3 id="limbs">Limbs</h3><div class="paragraph"><p>Since you&#8217;re just creating the ragdoll for this example, all the limbs have the same shape, and you can write a simple helper method to create them. The function returns a PhysicsControl with CollisionShape with the width, height, location, and rotation (vertical or horizontal) that you specify. You choose a CapsuleCollisionShape (a cylinder with rounded top and bottom) so the limbs collide smoothly against one another.</p></div>
  9. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> Node createLimb(<span class="type">float</span> width, <span class="type">float</span> height, Vector3f location, <span class="type">boolean</span> rotate) {
  10. <span class="type">int</span> axis = rotate ? PhysicsSpace.AXIS_X : PhysicsSpace.AXIS_Y;
  11. CapsuleCollisionShape shape = <span class="keyword">new</span> CapsuleCollisionShape(width, height, axis);
  12. Node node = <span class="keyword">new</span> Node(<span class="string"><span class="delimiter">&quot;</span><span class="content">Limb</span><span class="delimiter">&quot;</span></span>);
  13. RigidBodyControl rigidBodyControl = <span class="keyword">new</span> RigidBodyControl(shape, <span class="integer">1</span>);
  14. node.setLocalTranslation(location);
  15. node.addControl(rigidBodyControl);
  16. <span class="keyword">return</span> node;
  17. }</code></pre></div></div>
  18. <div class="paragraph"><p>You write a custom helper method to initialize the limbs. Look at the screenshot above for orientation.</p></div>
  19. <div class="ulist"><ul><li><p>All cylinders have the same diameter, 0.2f.</p></li><li><p>You make the body and shoulders longer than the other limbs, 1.0f instead of 0.5f.</p></li><li><p>You determine the coordinates for positioning the limbs to form a person.</p></li><li><p>The shoulders and hips are <em>vertical</em> cylinders, this is why we set the rotation to true.</p></li></ul></div>
  20. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Node shoulders = createLimb(<span class="float">0.2f</span>, <span class="float">1.0f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.00f</span>, <span class="float">1.5f</span>, <span class="integer">0</span>), <span class="predefined-constant">true</span>);
  21. Node uArmL = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f(-<span class="float">0.75f</span>, <span class="float">0.8f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  22. Node uArmR = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.75f</span>, <span class="float">0.8f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  23. Node lArmL = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f(-<span class="float">0.75f</span>,-<span class="float">0.2f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  24. Node lArmR = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.75f</span>,-<span class="float">0.2f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  25. Node body = createLimb(<span class="float">0.2f</span>, <span class="float">1.0f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.00f</span>, <span class="float">0.5f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  26. Node hips = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.00f</span>,-<span class="float">0.5f</span>, <span class="integer">0</span>), <span class="predefined-constant">true</span>);
  27. Node uLegL = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f(-<span class="float">0.25f</span>,-<span class="float">1.2f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  28. Node uLegR = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.25f</span>,-<span class="float">1.2f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  29. Node lLegL = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f(-<span class="float">0.25f</span>,-<span class="float">2.2f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);
  30. Node lLegR = createLimb(<span class="float">0.2f</span>, <span class="float">0.5f</span>, <span class="keyword">new</span> Vector3f( <span class="float">0.25f</span>,-<span class="float">2.2f</span>, <span class="integer">0</span>), <span class="predefined-constant">false</span>);</code></pre></div></div>
  31. <div class="paragraph"><p>You now have the outline of a person. But if you ran the application now, the individual limbs would fall down independently of one another – the ragdoll is still lacking joints.</p></div></div>
  32. <div class="sect2"><h3 id="joints">Joints</h3><div class="paragraph"><p>As before, you write a small helper method. This time its purpose is to quickly join two limbs A and B at the connection point that we specify.</p></div>
  33. <div class="ulist"><ul><li><p>Convert A&#8217;s and B&#8217;s connectionPoint vector from world coordinate space to local coordinate space.</p></li><li><p>Use a ConeJoint, a special joint that approximates the degree of freedom that limbs typically have. The ConeJoint constructor requires the two nodes, and the two local pivot coordinates that we just determined.</p></li><li><p>Set the joints limits to allow swinging, but not twisting.</p></li></ul></div>
  34. <div class="listingblock"><div class="content"><pre>private PhysicsJoint join(Node A, Node B, Vector3f connectionPoint) {
  35. Vector3f pivotA = A.worldToLocal(connectionPoint, new Vector3f());
  36. Vector3f pivotB = B.worldToLocal(connectionPoint, new Vector3f());
  37. ConeJoint joint = new ConeJoint(A.getControl(RigidBodyControl.class),
  38. B.getControl(RigidBodyControl.class),
  39. pivotA, pivotB);
  40. joint.setLimit(1f, 1f, 0);
  41. return joint;
  42. }</pre></div></div>
  43. <div class="paragraph"><p>Use the helper method to connect all limbs with joints where they belong, at one end of the limb.</p></div>
  44. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">join(body, shoulders, <span class="keyword">new</span> Vector3f( <span class="float">0.00f</span>, <span class="float">1.4f</span>, <span class="integer">0</span>));
  45. join(body, hips, <span class="keyword">new</span> Vector3f( <span class="float">0.00f</span>, -<span class="float">0.5f</span>, <span class="integer">0</span>));
  46. join(uArmL, shoulders, <span class="keyword">new</span> Vector3f(-<span class="float">0.75f</span>, <span class="float">1.4f</span>, <span class="integer">0</span>));
  47. join(uArmR, shoulders, <span class="keyword">new</span> Vector3f( <span class="float">0.75f</span>, <span class="float">1.4f</span>, <span class="integer">0</span>));
  48. join(uArmL, lArmL, <span class="keyword">new</span> Vector3f(-<span class="float">0.75f</span>, <span class="float">0.4f</span>, <span class="integer">0</span>));
  49. join(uArmR, lArmR, <span class="keyword">new</span> Vector3f( <span class="float">0.75f</span>, <span class="float">0.4f</span>, <span class="integer">0</span>));
  50. join(uLegL, hips, <span class="keyword">new</span> Vector3f(-<span class="float">0.25f</span>, -<span class="float">0.5f</span>, <span class="integer">0</span>));
  51. join(uLegR, hips, <span class="keyword">new</span> Vector3f( <span class="float">0.25f</span>, -<span class="float">0.5f</span>, <span class="integer">0</span>));
  52. join(uLegL, lLegL, <span class="keyword">new</span> Vector3f(-<span class="float">0.25f</span>, -<span class="float">1.7f</span>, <span class="integer">0</span>));
  53. join(uLegR, lLegR, <span class="keyword">new</span> Vector3f( <span class="float">0.25f</span>, -<span class="float">1.7f</span>, <span class="integer">0</span>));</code></pre></div></div>
  54. <div class="paragraph"><p>Now the ragdoll is connected. If you ran the app now, the doll would collapse, but the limbs would stay together.</p></div></div>
  55. <div class="sect2"><h3 id="attaching-everything-to-the-scene">Attaching Everything to the Scene</h3><div class="paragraph"><p>We create one (non-physical) Node named ragDoll, and attach all other nodes to it.</p></div>
  56. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ragDoll.attachChild(shoulders);
  57. ragDoll.attachChild(body);
  58. ragDoll.attachChild(hips);
  59. ragDoll.attachChild(uArmL);
  60. ragDoll.attachChild(uArmR);
  61. ragDoll.attachChild(lArmL);
  62. ragDoll.attachChild(lArmR);
  63. ragDoll.attachChild(uLegL);
  64. ragDoll.attachChild(uLegR);
  65. ragDoll.attachChild(lLegL);
  66. ragDoll.attachChild(lLegR);</code></pre></div></div>
  67. <div class="paragraph"><p>To use the ragdoll in a scene, we attach its main node to the rootNode, and to the PhysicsSpace.</p></div>
  68. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">rootNode.attachChild(ragDoll);
  69. bulletAppState.getPhysicsSpace().addAll(ragDoll);</code></pre></div></div></div></div>
  70. <div class="sect1"><h2 id="applying-forces">Applying Forces</h2><div class="sectionbody"><div class="paragraph"><p>To pull the doll up, you could add an input handler that triggers the following action:</p></div>
  71. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Vector3f upforce = <span class="keyword">new</span> Vector3f(<span class="integer">0</span>, <span class="integer">200</span>, <span class="integer">0</span>);
  72. shoulders.applyContinuousForce(<span class="predefined-constant">true</span>, upforce);</code></pre></div></div>
  73. <div class="paragraph"><p>We can use the action to pick the doll up and put it back on its feet, or what ever. Read more about <a href="../../jme3/advanced/physics.html#forcesmoving_physical_objects">Forces</a> here.</p></div></div></div>
  74. <div class="sect1"><h2 id="detecting-collisions">Detecting Collisions</h2><div class="sectionbody"><div class="paragraph"><p>Read the <a href="../../jme3/advanced/physics.html#responding_to_a_physicscollisionevent">Responding to a PhysicsCollisionEvent</a> chapter in the general physics documentation on how to detect collisions. You can detect collisions between limbs or between limbs and the floor, and trigger game events.</p></div></div></div>
  75. <div class="sect1"><h2 id="best-practices">Best Practices</h2><div class="sectionbody"><div class="paragraph"><p>If you experience weird behaviour in a ragdoll – such as exploding into pieces and then reassembling – check your collision shapes. Verify you did not position the limbs too close to one another when assmebling the ragdoll. You typically see physical nodes being ejected when their collision shapes intersect, which puts physics in an impossible state.</p></div></div></div>
  76. <div class="sect2"><h3 id="ragdoll-physics-using-kinematicragdollcontrol-deprecated-as-of-jmonkeyengine-v3-3">Ragdoll Physics using KinematicRagdollControl (deprecated as of JMonkeyEngine v3.3)</h3><div class="paragraph"><p>KinematicRagdollControl is an unfinished work in progress. The intent was to automate the creation of limbs and joints for ragdoll physics. The idea was to create a control,</p></div>
  77. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ragdoll = <span class="keyword">new</span> KinematicRagdollControl(<span class="float">0.5f</span>);</code></pre></div></div>
  78. <div class="paragraph"><p>and add it to a rigged model and also to physics space:</p></div>
  79. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">model.addControl(ragdoll);
  80. getPhysicsSpace().add(ragdoll);</code></pre></div></div>
  81. <div class="paragraph"><p>A rigid body and a physics joint would be created automatically for each bone in the skeleton, or alternatively just for the bones specified by invoking the addBoneName() method. As long as the control was in kinematic mode, the physics objects would mimic the motion of the bones, including skeletal animations and rotation/translation of the model. When it was time to simulate a ragdoll, you would invoke</p></div>
  82. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ragdoll.setRagdollMode();</code></pre></div></div>
  83. <div class="paragraph"><p>and thereafter physical forces would override any skeletal animation.</p></div>
  84. <div class="sect2"><h3 id="sample-code-2">Sample Code</h3><div class="ulist"><ul><li><p><a href="https://github.com/jMonkeyEngine/jmonkeyengine/blob/v3.2/jme3-examples/src/main/java/jme3test/bullet/TestRagdollCharacter.java">v3.2-branch TestRagdollCharacter.java</a> In this example, the control remains in kinematic mode. Hold down the "U" key to advance the model toward the wall. Press the spacebar to play the "Slice" animation. When the model comes into contact with the wall, the blocks will tumble.</p></li><li><p><a href="https://github.com/jMonkeyEngine/jmonkeyengine/blob/v3.2/jme3-examples/src/main/java/jme3test/bullet/TestBoneRagdoll.java">v3.2-branch TestBoneRagdoll.java</a> Press the left mouse button to shoot a cannonball at the model. Ragdoll physics starts when a cannonball touches the model. Press the spacebar to resume kinematic mode and cause the model to rise to his feet.</p></li></ul></div></div></div>
  85. <div class="sect2"><h3 id="ragdoll-physics-using-dynamicanimcontrol-jme-3-3-and-later">Ragdoll Physics using DynamicAnimControl (JME 3.3 and later)</h3><div class="paragraph"><p>DynamicAnimControl is a replacement for KinematicRagdollControl. The intent is similar, except that a DynamicAnimControl can be simultaneously kinematic for some bones and dynamic for others. Also, DynamicAnimControl can be configured to work with a wider variety of models than KinematicRagdollControl.</p></div>
  86. <div class="paragraph"><p>To simplify the implementation, the ragdoll created by a DynamicAnimControl is composed of "links". Just as a RigidBodyControl connects a rigid body to a spatial, a link connects a rigid body to one or more bones in the model&#8217;s skeleton. Just like a RigidBodyControl, a link can be in kinematic mode or dynamic mode.</p></div>
  87. <div class="paragraph"><p>And just as the bones in a skeleton are arranged in parent/child hierarchy, so are the links in a DynamicAnimControl ragdoll. In the ragdoll hierarchy, there is exactly one link that has no parent; it is referred to as the "torso". Every other link is a "bone link" which has another link as its parent. Each bone link is connected to its parent by a physics joint.</p></div>
  88. <div class="paragraph"><p>As with KinematicRagdollControl, you start by creating a control,</p></div>
  89. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ragdoll = <span class="keyword">new</span> DynamicAnimControl();</code></pre></div></div>
  90. <div class="paragraph"><p>but before adding it to the model, you must configure it by specifying the mass of the torso and also the mass and range of motion for each linked bone:</p></div>
  91. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ragdoll.setMass(DacConfiguration.torsoName, <span class="float">1f</span>);
  92. ragdoll.link(<span class="string"><span class="delimiter">&quot;</span><span class="content">Waist</span><span class="delimiter">&quot;</span></span>, <span class="float">1f</span>, <span class="keyword">new</span> RangeOfMotion(<span class="float">1f</span>, -<span class="float">0.4f</span>, <span class="float">0.8f</span>, -<span class="float">0.8f</span>, <span class="float">0.4f</span>, -<span class="float">0.4f</span>));
  93. ...</code></pre></div></div>
  94. <div class="paragraph"><p>You probably don&#8217;t want to link every bone in the model&#8217;s skeleton. For instance, if the model has articulated fingers, you probably want to link the hand bones but not the individual finger bones. Unlinked bones will be managed by their nearest linked ancestor, and the torso will manage any bones for which no ancestor is linked. If you link too many bones, the ragdoll may become inflexible or jittery due to collisions between rigid bodies that don&#8217;t share a physics joint.</p></div>
  95. <div class="paragraph"><p>Only after the control is configured should you add it to the model and to physics space:</p></div>
  96. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">model.addControl(ragdoll);
  97. getPhysicsSpace().add(ragdoll);</code></pre></div></div>
  98. <div class="paragraph"><p>Note that the control must be added to the Spatial with the SkinningControl, which isn&#8217;t necessarily the model&#8217;s root spatial.</p></div>
  99. <div class="paragraph"><p>As long as a link is in kinematic mode, its physics objects will mimic the motion of the bones, including skeletal animations. When it&#8217;s time to simulate a ragdoll, you can invoke</p></div>
  100. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">ragdoll.setRagdollMode();</code></pre></div></div>
  101. <div class="paragraph"><p>to put all links into dynamic mode. Thereafter, physical forces will override any skeletal animation.</p></div>
  102. <div class="sect2"><h3 id="sample-code-3">Sample Code</h3><div class="ulist"><ul><li><p><a href="https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/bullet/TestBoneRagdoll.java">master-branch TestBoneRagdoll.java</a> Press the left mouse button to shoot a cannonball at the model. Ragdoll physics starts when a cannonball touches the model. Press the spacebar to resume kinematic mode and cause the model to rise to his feet.</p></li></ul></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2019-12-20 23:30:51 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  103. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  104. indexName: 'jmonkeyengine',
  105. inputSelector: '#doc-search',
  106. debug: false // Set debug to true if you want to inspect the dropdown
  107. });</script></body></html>