Pages

Friday, November 30, 2018

Analytical IK Solution - Improvements

Boundary singularities  

“In the elbow the singularity occurs when the elbow is fully stretched out, because then, the last axis of the upper arm and the first axis of the lower arm are in line. In such a case there are multiple solutions for the orientation of the elbow.” (Kumar, 2011, p. 5)

The singularity created when the target is out of range produces an infinite number of possibilities, which prevents the calculation of a suitable chain configuration.

When the target is unreachable, the exterior angle of the second joint in the chain is outside the arccosθ domain.




A value of -1 will position the second chain bone (and end effector) at an angle of 180° (bent back towards the chain base). If the value is less than -1 the joint will attempt to position the second chain bone beyond what is physically possible toward the chain base.

A value of 1 will position the second chain bone (and end effector) at an angle of 0° (at full extension away from the chain base towards the target position). If the value is greater than 1 the joint will attempt to position the second chain bone beyond what is physically possible toward the target location.

By clamping the value within the valid range of -1 and 1, when the target position is out of range I can simulate a fully extended reaching behaviour.

Figure 1: Previous implementation, no accomodation for boundary singularityFigure 2: Updated implementation, comparison showing value clamping to account for boundary singularity













As my intention is to mimic the natural movement of a humanoid, when the target is out of range of the chain an appropriate behaviour would be to have the arm reach toward (fully outstretched) from the chain base joint toward the target position.

Angle of reach 

An error I had overlooked in my initial attempt was calculating the direction of the target from the world origin.

Figure 3: Angle to target position calculated relative to the base joint position

Calculating this angle from the base of the chain allows the arm to be repositioned while maintaining accurate reaching behaviour.

This is a crucial correction and a mistake that will not be repeated. Ensuring that all angles are calculated relative to the current joint is essential to reliable, dynamic behaviours across multiple chains simultaneously.

Ambiguity 

For this specific chain problem, my analytical solution solves and yields a single configuration. While it is possible to alter the equations to force calculation of the opposite solution, this is unnecessary for the needs of this investigation.

I have chosen a far simpler approach, to expose a Boolean in the Unity inspector which simply inverts the chains joint angles to achieve the opposite configuration which still reaches the target.

Figure 4: Inversion of interior joint angles
The result:
Figure 5: Inversion of the chain bend direction

Seeking Behaviour 

To better mimic a natural reach toward a target position, the motion needs to be applied to the chain configuration over time.

Rather than applying the solved joint angles to the corresponding chain joints at each frame, I have made use of the Unity math function SmoothDampAngle which “Gradually changes an angle given in degrees towards a desired goal angle over time.” (Unity Technologies, 2018)

By making the ‘smooth’ variable public, the time taken for the correct joint angles to be applied can be edited from the Unity inspector so in future I can better modify the chain behaviour.

Figure 6: Applying rotation to joints over time
The result:
Figure 7: Updated solution behaviour

Readability and functionality 

To make the class more reader friendly, I have reduced the equations for the calculation of interior angles α and β (including their conversion to degrees and clamping within legal arccosθ range) to a single line, rather than over several stages.

Figure 8: Reduction of equations

Rather than use the Pythagorean theorem to identify length C, Vector2.Distance can be used to find the length from the base of the chain to the target location.

The same method is used to determine the bone lengths of the chain, which has now checked at start rather than continuously during runtime.

Class functionality has also been compartmentalised into appropriate functions to make potential future edits or review by other developers more straightforward.

Summary 

My analytical solution has now been developed to a standard where I can use it to mimic simple arm/leg limb movement in a 2D environment.

I will use this as my basis for investigating how to manage procedurally animate a humanoid in 2D before I take this knowledge and update my IK solution to a heuristic implementation in a 3D environment.



References
Kumar, A. (2011). Inverse Kinematics in a Robotic Arm and Methods to Avoid Singularities. [online] pp. 1 - 5. Available at: http://home.iitk.ac.in/~avinash/ME%20600.pdf [Accessed 27 Nov. 2018].

Unity Technologies (2018). Unity - Scripting API: Mathf.SmoothDampAngle. [online] Docs.unity3d.com. Available at: https://docs.unity3d.com/ScriptReference/Mathf.SmoothDampAngle.html [Accessed 29 Nov. 2018].

Bibliography
Juckett, R. (2008). Analytic Two-Bone IK in 2D. [online] Ryanjuckett.com. Available at: http://www.ryanjuckett.com/programming/analytic-two-bone-ik-in-2d/ [Accessed 28 Nov. 2018].

Kenwright, B. (2013). Game inverse kinematics. [Place of publication not identified]: CreatSpace, pp.39 - 54.