Figure 1: Hierarchy within Unity |
This simulates forward kinematics with each child being rotated in world space proportional to that of their parent, while their individual joint space is only affected when they are specifically modified – and so on down the chain.
Finding Side Lengths
Now that I am confident I understand the functions and result of the necessary equation to calculate an IK solution from my research in the previous entry, rather than explicitly define my own math functions I will make use of the math library within Unity.To clarify the angles of each joint, the bone lengths must be known.
Figure 2: Required values |
Using the Pythagorean theorem, I can identify length C (see Figure 2), the distance from the base of the chain to the target location.
Figure 3: Calculate distance c |
Calculating Interior Angles
Once the lengths are known, Cosine Law solves the joint angles α and β.Figure 4: Calculate joint angles |
The angle from the base of the chain to the target is then calculated.
Figure 5: Angle from chain base position to target position |
Applying Corrective Rotation to Chain Joints
Because Unity’s ‘Mathf.Acos’ and ‘Mathf.Atan2’ return a float value in radians and the Unity Transform component requires degrees, the value must be converted back into degrees before being applied to the corresponding chain joint.Figure 6: Applying joint rotations |
To ensure that the rotation of each joint was updated in joint space rather than world space the degree value is applied to the Transform component of each joint with localEulerAngles which sets “the rotation as Euler angles in degrees relative to the parent transform's rotation” (Unity Technologies, 2018)
The result gives an arm that will continuously try to position the end effector at the target position.
Figure 7: Analytical IK Solution |
Undersirable Behaviour
The current implementation does have two significant shortcomings:1: The class does not provide a solution for when the distance of the target from the chain base is greater than the sum length of the chain:
Figure 8: Boundary singularity |
This is a “boundary singularities (i.e., workspace singularities). When a fully extended arm is beyond the reach of the target position” (Kenwright, 2013, p. 8)
2: Appropriate reaching behaviour is only demonstrated when the arm base is positioned at world origin.
Figure 9: Angles are solved relative to world origin |
Next Objective
Now the basic functionality has been implemented, in my next post I will re-write my solution to address the unwanted behaviour, while aiming to provide greater control over the action of the chain behaviour.References
Kenwright, B. (2013). Game inverse kinematics. [Place of publication not identified]: CreatSpace, pp.7-10, 39-54.
Unity Technologies (2018). Unity - Scripting API: Transform.localEulerAngles. [online] Docs.unity3d.com. Available at: https://docs.unity3d.com/ScriptReference/Transform-localEulerAngles.html [Accessed 26 Nov. 2018].
Bibliography
Faraji, S. and Ijspeert, A. (2017). Singularity-Tolerant Inverse Kinematics for Bipedal Robots: An Efficient Use of Computational Power to Reduce Energy Consumption. IEEE Robotics and Automation Letters, [online] 2(2), pp.1132-1139. Available at: https://infoscience.epfl.ch/record/228940/files/root.pdf [Accessed 25 Nov. 20189].
Unity Technologies (2018). Unity - Scripting API: Mathf.Atan2. [online] Docs.unity3d.com. Available at: https://docs.unity3d.com/ScriptReference/Mathf.Atan2.html [Accessed 25 Nov. 2018].
Unity Technologies (2018). Unity - Scripting API: Mathf.Acos. [online] Docs.unity3d.com. Available at: https://docs.unity3d.com//ScriptReference/Mathf.Acos.html [Accessed 24 Nov. 2018].
Weisstein, E. (2018). "Law of Cosines." From MathWorld--A Wolfram Web Resource.. [online] Mathworld.wolfram.com. Available at: http://mathworld.wolfram.com/LawofCosines.html [Accessed 24 Nov. 2018].