ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: Animating Composite Objects Using Inverse Kinematics | 229

The following steps relate to Figure 6-7.



  1. Initial condition where the kinematic pair consists of two line segments. The reg-
    istration point of the segments is at the beginning of the lines (C1 for the child
    and P1 for the parent). The distance between the registration point of the child
    C1 and the joint isd.

  2. Parent segment moves to position P2 and rotates (independent of the child seg-
    ment).

  3. Child segment rotates to orient itself to point to the joint.

  4. Child segment moves towards point P2 to maintain distanced.


There is some trigonometry involved in this whole process. However, we will use the


built-inPointclass methods to accomplish much of the rotation and motion. We can


now build the body segments for the snake.


Building the Body and Tail Segments


We didn’t have to worry about inverse kinematic motion for the snake head as it was


the root node of the composite structure. Head motion was controlled by the key-


board. However, the body segments that’ll be attached to the head have to move as


kinematic pairs. Example 6-15 shows theBodySegmentclass. It’s a composite object


and extends theCompositeclass shown in Example 6-12. The constructor draws the


body segment, and theupdate( )method is overridden to implemented inverse kine-


matic motion.


Example 6-15. BodySegment.as


1 package
2 {
3 import flash.geom.Point;
4
5 public class BodySegment extends Composite
6 {
7 private var segLen:Number = 20;
8
9 public function BodySegment(color:uint = 0xC0C0C0)
10 {
11 graphics.lineStyle(10, color); // grey color
12 graphics.moveTo(0, 0);
13 graphics.lineTo(segLen, 0);
14 }
15
16 override public function update( ):void
17 {
18 var myParent:Composite = this.getParent( );
19 var parentLoc:Point = new Point(myParent.x, myParent.y);
20 var myLoc:Point = new Point(this.x, this.y);
21
22 // rotate to orient to parents new location
Free download pdf