inverse_kinematics.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. .. _doc_inverse_kinematics:
  2. Inverse kinematics
  3. ==================
  4. This tutorial is a follow-up of :ref:`doc_working_with_3d_skeletons`.
  5. Previously, we were able to control the rotations of bones in order to manipulate
  6. where our arm was (forward kinematics). But what if we wanted to solve this problem
  7. in reverse? Inverse kinematics (IK) tells us *how* to rotate our bones in order to reach
  8. a desired position.
  9. A simple example of IK is the human arm: While we intuitively know the target
  10. position of an object we want to reach for, our brains need to figure out how much to
  11. move each joint in our arm to get to that target.
  12. Initial problem
  13. ~~~~~~~~~~~~~~~
  14. Talking in Godot terminology, the task we want to solve here is to position
  15. the 2 angles on the joints of our upperarm and lowerarm so that the tip of the
  16. lowerarm bone is as close to the target point (which is set by the target Vector3)
  17. as possible using only rotations. This task is calculation-intensive and never
  18. resolved by analytical equation solving, as it is an under-constrained
  19. problem which means that there is more than one solution to an
  20. IK problem.
  21. .. image:: img/inverse_kinematics.png
  22. For easy calculation in this chapter, we consider the target being a
  23. child of Skeleton. If this is not the case for your setup you can always
  24. reparent it in your script, as you will save on calculations if you
  25. do so.
  26. In the picture, you see the angles alpha and beta. In this case, we don't
  27. use poles and constraints, so we need to add our own. On the picture
  28. the angles are 2D angles living in a plane which is defined by bone
  29. base, bone tip, and target.
  30. The rotation axis is easily calculated using the cross-product of the bone
  31. vector and the target vector. The rotation in this case will be always in
  32. positive direction. If ``t`` is the Transform which we get from the
  33. get_bone_global_pose() function, the bone vector is
  34. ::
  35. t.basis[2]
  36. So we have all the information we need to execute our algorithm.
  37. In game dev it is common to resolve this problem by iteratively closing
  38. to the desired location, adding/subtracting small numbers to the angles
  39. until the distance change achieved is less than some small error value.
  40. Sounds easy enough, but there are still Godot problems we need to resolve
  41. to achieve our goal.
  42. - **How to find coordinates of the tip of the bone?**
  43. - **How to find the vector from the bone base to the target?**
  44. For our goal (tip of the bone moved within area of target), we need to know
  45. where the tip of our IK bone is. As we don't use a leaf bone as IK bone, we
  46. know the coordinate of the bone base is the tip of the parent bone. All these
  47. calculations are quite dependent on the skeleton's structure. You could use
  48. pre-calculated constants, or you could add an extra bone at the tip of the
  49. IK bone and calculate using that.
  50. Implementation
  51. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52. We will use an exported variable for the bone length to make it easy.
  53. ::
  54. export var ik_bone = "lowerarm"
  55. export var ik_bone_length = 1.0
  56. export var ik_error = 0.1
  57. Now, we need to apply our transformations from the IK bone to the base of
  58. the chain, so we apply a rotation to the IK bone, then move from our IK bone up to
  59. its parent, apply rotation again, then move to the parent of the
  60. current bone again, etc. So we need to limit our chain somewhat.
  61. ::
  62. export var ik_limit = 2
  63. For the ``_ready()`` function:
  64. ::
  65. var skel
  66. func _ready():
  67. skel = get_node("arm/Armature/Skeleton")
  68. set_process(true)
  69. Now we can write our chain-passing function:
  70. ::
  71. func pass_chain():
  72. var b = skel.find_bone(ik_bone)
  73. var l = ik_limit
  74. while b >= 0 and l > 0:
  75. print( "name":", skel.get_bone_name(b))
  76. print( "local transform":", skel.get_bone_pose(b))
  77. print( "global transform":", skel.get_bone_global_pose(b))
  78. b = skel.get_bone_parent(b)
  79. l = l - 1
  80. And for the ``_process()`` function:
  81. ::
  82. func _process(delta):
  83. pass_chain(delta)
  84. Executing this script will pass through the bone chain, printing bone
  85. transforms.
  86. ::
  87. extends Spatial
  88. export var ik_bone = "lowerarm"
  89. export var ik_bone_length = 1.0
  90. export var ik_error = 0.1
  91. export var ik_limit = 2
  92. var skel
  93. func _ready():
  94. skel = get_node("arm/Armature/Skeleton")
  95. set_process(true)
  96. func pass_chain(delta):
  97. var b = skel.find_bone(ik_bone)
  98. var l = ik_limit
  99. while b >= 0 and l > 0:
  100. print("name: ", skel.get_bone_name(b))
  101. print("local transform: ", skel.get_bone_pose(b))
  102. print( "global transform:", skel.get_bone_global_pose(b))
  103. b = skel.get_bone_parent(b)
  104. l = l - 1
  105. func _process(delta):
  106. pass_chain(delta)
  107. Now we need to actually work with the target. The target should be placed
  108. somewhere accessible. Since "arm" is an imported scene, we better place
  109. the target node within our top level scene. But for us to work with target
  110. easily its Transform should be on the same level as the Skeleton.
  111. To cope with this problem, we create a "target" node under our scene root
  112. node and at runtime we will reparent it, copying the global transform
  113. which will achieve the desired effect.
  114. Create a new Spatial node under the root node and rename it to "target".
  115. Then modify the ``_ready()`` function to look like this:
  116. ::
  117. var skel
  118. var target
  119. func _ready():
  120. skel = get_node("arm/Armature/Skeleton")
  121. target = get_node("target")
  122. var ttrans = target.get_global_transform()
  123. remove_child(target)
  124. skel.add_child(target)
  125. target.set_global_transform(ttrans)
  126. set_process(true)