RyanJuckett.com

Analytic Two-Bone IK in 2D - Solving for angle 2 Print E-mail
  
Monday, 29 December 2008 07:46
Article Index
Analytic Two-Bone IK in 2D
Defining the problem
The solvable domain
Math review
Describing the problem with math
Solving for angle 2
Solving for angle 1
Writing the code
All Pages

 

Solving for θ2

Our first step will be solving for \theta_2. In order to do this, we need to cancel \theta_1 out of our equations. By squaring and adding the equations for x and y, we can use the Pythagorean trigonometric identity to cancel out terms.

 

First off, lets square the equations.

x^2 = d_1^2 \cos^2 (\theta_1) \quad + \quad 2 d_1 d_2 \cos (\theta_1) \cos(\theta_1 + \theta_2) \quad + \quad d_2^2 \cos^2(\theta_1 + \theta_2)

y^2 = d_1^2 \sin^2 (\theta_1) \quad + \quad 2 d_1 d_2 \sin (\theta_1) \sin(\theta_1 + \theta_2) \quad + \quad d_2^2 \sin^2(\theta_1 + \theta_2)

 

Add the squared equations together.

x^2+y^2 = d_1^2 \Big(\sin^2(\theta_1) \quad+ \quad \cos^2(\theta_1)\Big)\\ \qquad\qquad\qquad\qquad\qquad\qquad + \quad 2 d_1 d_2 \Big(\sin(\theta_1) \sin(\theta_1 + \theta_2) \quad+ \quad \cos(\theta_1) \cos(\theta_1 + \theta_2)\Big)\\ \qquad\qquad\qquad\qquad\qquad\qquad + \quad d_2^2 \Big(\sin^2(\theta_1 + \theta_2) \quad + \quad \cos^2(\theta_1 + \theta_2)\Big)

 

Using Pythagorean trigonometric identity, simplify the d_1^2 and d_2^2 terms.

x^2+y^2 = d_1^2 \quad + \quad d_2^2 \quad + \quad 2 d_1 d_2 \Big(\sin(\theta_1) \sin(\theta_1 + \theta_2) \quad+ \quad \cos(\theta_1) \cos(\theta_1 + \theta_2)\Big)

 

Using the angle sum and difference identities for cosine and sine, expand \sin(\theta_1 + \theta_2) and \cos(\theta_1 + \theta_2).

x^2+y^2 = d_1^2 \quad + \quad d_2^2 \\ \qquad\qquad\qquad\qquad\qquad\qquad + \quad 2 d_1 d_2 \Big(\sin(\theta_1) ( \sin \theta_1 \cos \theta_2 + \cos \theta_1 \sin \theta_2 ) + \cos(\theta_1) ( \cos \theta_1 \cos \theta_2 - \sin \theta_1 \sin \theta_2) \Big)

 

Distribute the \sin (\theta_1) and \cos (\theta_1) terms.

x^2+y^2 = d_1^2 \quad + \quad d_2^2 \\ \qquad\qquad\qquad\qquad\qquad\qquad + \quad 2 d_1 d_2 \Big(\sin^2(\theta_1) \cos \theta_2 + \cos \theta_1 \sin \theta_1 \sin \theta_2 + \cos^2(\theta_1) \cos \theta_2 - \cos \theta_1 \sin \theta_1 \sin \theta_2 \Big)

 

The positive and negative \cos \theta_1 \sin \theta_1 \sin \theta_2 terms cancel each other out, and we can factor \cos \theta_2 out of the remaining terms.

x^2+y^2 = d_1^2 \quad + \quad d_2^2\quad + \quad 2 d_1 d_2 \Big( (\sin^2(\theta_1) + \cos^2(\theta_1)) \cos \theta_2 \Big)

 

Use the Pythagorean trigonometric identity to simplify.

x^2+y^2 = d_1^2 \quad + \quad d_2^2\quad + \quad 2 d_1 d_2 \cos \theta_2

 

Solve for \theta_2.

\cos \theta_2 = \frac{ x^2 + y^2 - d_1^2 - d_2^2 } { 2 d_1 d_2 }

\theta_2 = \arccos \frac{ x^2 + y^2 - d_1^2 - d_2^2 } { 2 d_1 d_2 }

 

We now have our analytic equation for \theta_2, but there are a few nuances to discuss in more detail.

  1. If \cos \theta_2 is not within the range [-1,1], it is outside of the \arccos \theta domain. This will happen when our target point is a location the bones cannot reach. A value of -1 will give us an angle of 180° (i.e. bone2 is fully bent) and a value of 1 will give us an angle of 0° (i.e. bone2 is fully extended). When the value is less than -1, our bone is trying to bend father than is physically possible to reach a point that is too close to the origin. When the value is greater than 1, our bone is trying to extend farther than is physically possible to reach a point that is too far from the origin. This means that we can compute the best non-valid solution by clamping the value into the legal [-1,1] range.
  2. If d_1 or d_2 is zero, we will divide by zero when computing \cos \theta_2. When one or both bones have a zero length, we can set \theta_2 to any value we desire (I suggest zero or the value used on a previous frame). When we solve for \theta_1 it will correct itself according to our selected \theta_2. The solvable domain for this case has also been reduced to a single circle centered at the origin with a radius of d_1 + d_2.
  3. When we can evaluate \arccos \theta, our result is limited to the range [0,\pi]. If we use the result as is, we will always find the solution where our second bone turns in the positive direction. If it is our intention to find the solution that bends in the negative direction, we need to negate the new \theta_2.


Last Updated ( Sunday, 02 May 2010 06:32 )
 

Creative Commons License
RyanJuckett.com site content by Ryan Juckett is licensed under a Creative Commons Attribution 3.0 United States License.