10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Inverse Kinematics<br />

Inverse kinematics is a method by which rigid objects interconnected by joints can<br />

move to form different poses. A good example of this type of object is a marionette: a<br />

puppet controlled by a puppeteer using strings. The hands and legs of the puppet<br />

consist of several parts connected by joints. For example, the upper arm would be<br />

connected to the torso at the shoulder. The upper arm would in turn be connected to<br />

the forearm through the elbow joint. The hand would be connected to the forearm at<br />

the wrist. These interconnected objects form a kinematic chain. Inverse kinematics<br />

allows kinematic chains to move, constrained by the range of motion allowed by the<br />

joints. The simplest form of a kinematic chain has a free end that’s controlled externally.<br />

For example, the hand of a marionette would be a free end since it’s attached<br />

to a puppeteer’s string. Because joints connect them, the hand, forearm, and upper<br />

arm move when the puppeteer pulls this string. Try this yourself: let your left arm go<br />

limp, and pull it up by the hand using your right arm. Notice how the external force<br />

is pulling your left hand, which translates the pulling motion to the forearm, which<br />

in turn pulls the upper arm. The notion of interconnected objects pulling each other<br />

is the primary concept in inverse kinematics. This type of motion is very similar to<br />

the motion of the snake application we will develop.<br />

In the example application, users will control the head of the snake using the keyboard.<br />

When the head moves, interconnected body segments will move based on<br />

inverse kinematic principles. The whole snake will be a kinematic chain.<br />

Kinematic chains consist of one or more kinematic pairs. For example, the upper<br />

arm and forearm form a kinematic pair. Adjoining body segments in our snake will<br />

also form kinematic pairs. We will develop the snake as a <strong>com</strong>posite object. The<br />

head of the snake will be the root node, and body segments will be connected to<br />

each other. Each node will be a <strong>com</strong>posite object as they have child objects connected<br />

to them. The last segment of the snake, which is the tail (or the rattle for a<br />

rattlesnake), will be a <strong>com</strong>ponent object.<br />

Creating Component and Composite Nodes for the Snake<br />

All our <strong>com</strong>ponents will be display objects on the stage. Therefore we can develop the<br />

<strong>com</strong>ponent class by extending the Sprite class. This allows us to inherit the properties<br />

and methods to manipulate <strong>com</strong>ponents on the stage to make them move and<br />

respond to events. Example 6-11 shows the Component class that’ll be used to create<br />

animated figures. The only difference in this class when <strong>com</strong>pared to previous <strong>com</strong>ponent<br />

classes is that it extends the Sprite class and declares a method called<br />

update( ).<br />

Example: Animating Composite Objects Using Inverse Kinematics | 223

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!