Inverse kinematics — Godot Engine (3.0) documentation in English (2024)

This tutorial is a follow-up of Working with 3D skeletons.

Previously, we were able to control the rotations of bones in order to manipulatewhere our arm was (forward kinematics). But what if we wanted to solve this problemin reverse? Inverse kinematics (IK) tells us how to rotate our bones in order to reacha desired position.

A simple example of IK is the human arm: While we intuitively know the targetposition of an object we want to reach for, our brains need to figure out how much tomove each joint in our arm to get to that target.

Initial problem

Talking in Godot terminology, the task we want to solve here is to positionthe 2 angles on the joints of our upperarm and lowerarm so that the tip of thelowerarm bone is as close to the target point (which is set by the target Vector3)as possible using only rotations. This task is calculation-intensive and neverresolved by analytical equation solving, as it is an under-constrainedproblem which means that there is more than one solution to anIK problem.

Inverse kinematics — Godot Engine (3.0) documentation in English (1)

For easy calculation in this chapter, we consider the target being achild of Skeleton. If this is not the case for your setup you can alwaysreparent it in your script, as you will save on calculations if youdo so.

In the picture, you see the angles alpha and beta. In this case, we don’tuse poles and constraints, so we need to add our own. On the picturethe angles are 2D angles living in a plane which is defined by bonebase, bone tip, and target.

The rotation axis is easily calculated using the cross-product of the bonevector and the target vector. The rotation in this case will be always inpositive direction. If t is the Transform which we get from theget_bone_global_pose() function, the bone vector is

t.basis[2]

So we have all the information we need to execute our algorithm.

In game dev it is common to resolve this problem by iteratively closingto the desired location, adding/subtracting small numbers to the anglesuntil the distance change achieved is less than some small error value.Sounds easy enough, but there are still Godot problems we need to resolveto achieve our goal.

  • How to find coordinates of the tip of the bone?
  • How to find the vector from the bone base to the target?

For our goal (tip of the bone moved within area of target), we need to knowwhere the tip of our IK bone is. As we don’t use a leaf bone as IK bone, weknow the coordinate of the bone base is the tip of the parent bone. All thesecalculations are quite dependent on the skeleton’s structure. You could usepre-calculated constants, or you could add an extra bone at the tip of theIK bone and calculate using that.

Implementation

We will use an exported variable for the bone length to make it easy.

export var ik_bone = "lowerarm"export var ik_bone_length = 1.0export var ik_error = 0.1

Now, we need to apply our transformations from the IK bone to the base ofthe chain, so we apply a rotation to the IK bone, then move from our IK bone up toits parent, apply rotation again, then move to the parent of thecurrent bone again, etc. So we need to limit our chain somewhat.

export var ik_limit = 2

For the _ready() function:

var skelfunc _ready(): skel = get_node("arm/Armature/Skeleton") set_process(true)

Now we can write our chain-passing function:

func pass_chain(): var b = skel.find_bone(ik_bone) var l = ik_limit while b >= 0 and l > 0: print( "name":", skel.get_bone_name(b)) print( "local transform":", skel.get_bone_pose(b)) print( "global transform":", skel.get_bone_global_pose(b)) b = skel.get_bone_parent(b) l = l - 1

And for the _process() function:

func _process(delta): pass_chain(delta)

Executing this script will pass through the bone chain, printing bonetransforms.

extends Spatialexport var ik_bone = "lowerarm"export var ik_bone_length = 1.0export var ik_error = 0.1export var ik_limit = 2var skelfunc _ready(): skel = get_node("arm/Armature/Skeleton") set_process(true)func pass_chain(delta): var b = skel.find_bone(ik_bone) var l = ik_limit while b >= 0 and l > 0: print("name: ", skel.get_bone_name(b)) print("local transform: ", skel.get_bone_pose(b)) print( "global transform:", skel.get_bone_global_pose(b)) b = skel.get_bone_parent(b) l = l - 1func _process(delta): pass_chain(delta)

Now we need to actually work with the target. The target should be placedsomewhere accessible. Since “arm” is an imported scene, we better placethe target node within our top level scene. But for us to work with targeteasily its Transform should be on the same level as the Skeleton.

To cope with this problem, we create a “target” node under our scene rootnode and at runtime we will reparent it, copying the global transformwhich will achieve the desired effect.

Create a new Spatial node under the root node and rename it to “target”.Then modify the _ready() function to look like this:

var skelvar targetfunc _ready(): skel = get_node("arm/Armature/Skeleton") target = get_node("target") var ttrans = target.get_global_transform() remove_child(target) skel.add_child(target) target.set_global_transform(ttrans) set_process(true)
Inverse kinematics — Godot Engine (3.0) documentation in English (2024)

FAQs

Does Godot have inverse kinematics? ›

This is an example of different Inverse Kinematic algorithms implemented in Godot. It contains four scenes, showing different ways they can be used.

What is the inverse kinematics methodology? ›

The inverse kinematic model gives the joint velocities q ˙ for a desired end-effector velocity X ˙ . This model is equivalent to the inverse differential model, which determines the differential variation of the joint variables dq corresponding to a given differential displacement of the end-effector coordinates dX.

What is inverse kinematics unity? ›

Start from a chosen position in space, or a goal, and work backwards to find a way to orient the joints so that the end point reaches the goal. This can be useful if you want a character to grab an object or stand on an uneven surface. This approach is known as inverse kinematics (IK).

What is the inverse kinematics output? ›

Inverse Kinematics is the inverse function/algorithm of Forward Kinematics. The Inverse Kinematics function/algorithm takes a target position as the input, and calculates the pose required for the end effector to reach the target position — the pose is the output.

Why is inverse kinematics so hard? ›

Unfortunately, the inverse kinematics problem can be ill-posed because there is either no solution (in this case the target location is infeasible, i.e., out of the reachable workspace), or because there are many solutions.

Is inverse kinematics necessary? ›

The importance of Inverse Kinematics is obvious: in order to move the platform to a certain position, Inverse Kinematics is used to reveal how to position the actuators to achieve that. Forward Kinematics is useful in more subtle cases and is particularly valuable for Virtual Reality applications as seen below.

What is the formula for inverse kinematics? ›

There exist two general approaches for solving IK problems. In the analytical approach, the equation xD=f(q) is mathematically inverted into a closed-form expression through symbolic manipulations. In the numerical approach, we are given an initial guess q0 and then calculate a sequence of configurations q0,q1,q2,…

What is an example of inverse kinematics? ›

Inverse kinematics is the use of kinematic equations to determine the motion of a robot to reach a desired position. For example, to perform automated bin picking, a robotic arm used in a manufacturing line needs precise motion from an initial position to a desired position between bins and manufacturing machines.

What is the difference between inverse kinematics and dynamics? ›

While inverse dynamics asks for torques that produce a certain time-trajectory of positions and velocities, inverse kinematics only asks for a static set of joint angles such that a certain point (or a set of points) of the character (or robot) is positioned at a certain designated location.

What are the benefits of inverse kinematics? ›

Inverse kinematics (IK) is a technique for animating characters that allows you to control the position and orientation of their limbs by specifying the desired end points, such as the hands or feet. This can make it easier to create realistic and natural human motion, especially for complex poses and interactions.

Why is it called inverse kinematics? ›

Forward kinematics uses the joint parameters to compute the configuration of the chain, and inverse kinematics reverses this calculation to determine the joint parameters that achieve a desired configuration.

What is the difference between forward kinematics and inverse kinematics? ›

Forward kinematics can help you evaluate the cost and feasibility of a path, while inverse kinematics can help you generate the path and control the robot. Some applications of robot motion planning include industrial robot programming and simulation, autonomous navigation and manipulation, and human-robot interaction.

What are the different methods of solving inverse kinematics? ›

Inverse kinematics methods in robotics include analytical kinematics solvers, numerical methods such as Newton-Raphson or Jacobian inverse, swarm intelligence techniques, geometric approaches, and soft-computing based methods 1 2 3 4 5.

What is an example of inverse kinematics animation? ›

Let's take the example of an arm. To animate an arm using forward kinematics, you rotate the upper arm away from the shoulder, then rotate the forearm, the hand from the wrist and so, on adding rotation keys for each child object. To animate the arm using inverse kinematics, you move a goal that positions the wrist.

What is kinematics in simple terms? ›

Kinematics is a subfield of physics and mathematics, developed in classical mechanics, that describes the motion of points, bodies (objects), and systems of bodies (groups of objects) without considering the forces that cause them to move.

Where is inverse kinematics used? ›

Inverse kinematics is the use of kinematic equations to determine the motion of a robot to reach a desired position. For example, to perform automated bin picking, a robotic arm used in a manufacturing line needs precise motion from an initial position to a desired position between bins and manufacturing machines.

Is inverse kinematic the same as forward kinematics? ›

Forward kinematics uses the joint parameters to compute the configuration of the chain, and inverse kinematics reverses this calculation to determine the joint parameters that achieve a desired configuration.

What is the difference between static body and kinematic body in Godot? ›

In the Godot Wiki's description of Kinematic Bodies, it states: “Kinematic bodies are special types of bodies that are meant to be user-controlled. They are not affected by physics at all; to other types of bodies, such as a character or a rigid body, these are the same as a static body.”

How do you find inverse kinematics in robotics? ›

Inverse kinematics is simply the reverse problem i.e., given the target position and orientation of the end-effector, we have to find the joint parameters. For example we have a kinematic chain with n joints as shown in fig 1. Lets say q1, q2, q3, ....... qn are the joint variables.

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 6044

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.