Back to "flash Basic Theory Class-Catalog"
The 13th chapter introduces some basic kinematics and the difference between forward and reverse kinematics. In the previous chapter we talked about forward kinematics, and this chapter will study the inverse kinematics which is closely related to it. The action involved is dragging and stretching.
As in the case of forward kinematics, the example in this chapter is also to establish a system from an independent joint. We start with a single joint and then to multiple joints. First, I'll show you the simplest way to calculate the angle and position. Just use the basic trigonometry in the code to do the approximate calculation. Finally, we will give you a brief introduction to the cosine theorem, so that the calculated results are more accurate, but will consume a lot of calculation-this is the so-called trade-off.
Drag and stretch of a single object
As I said before, the reverse kinematics system can be divided into two different types: dragging and stretching.
When the free end of the system extends to the target point, the other end of the system-the fixed end may not be able to move, so if the target point position is beyond the range of the free end motion, then the free end can never reach the target point. For example, when we try to grab something, our fingers move toward the object, and the wrist turns our fingers closer to the target position, and the elbows, shoulders and other parts of the body stretch as much as possible. Sometimes, the combination of all these positions will expose the fingers to the object; If the object is moving back and forth, our limbs will have to make an immediate reflection of the position, in order to allow the fingers to reach the object as much as possible. The reverse kinematics will tell us how to set all these parts in place to achieve the best stretching effect.
Another kind of inverse kinematics is when the object is dragged and pulled. In this example, the free end is dragged by some external force. Whenever the rest of the system is followed, they place themselves in a natural position. Can be imagined as a corpse without consciousness (sorry, that's all I can think of). Grab his hand and drag it away.
The force we exert on each other's hands will spread to the wrist, elbows, shoulders, and the rest of the body, and they all move in the direction of the tug. In this example, the reverse kinematics will tell us how all of these parts are grouped together in the correct position.
The best way to understand this is to use an example program to illustrate that each example uses a joint. We need to use the Segment class, so make sure it's in the engineering or classpath we're working on.
Single joint stretching
For stretching, all joints must be able to rotate towards the target. Target, if you haven't read my meaning, think of it as a mouse. To rotate the joint to the target, you need to know the distance between the two points on the x,y axis. You can then use the math.atan2 to find the angle of the arc system. By converting it to an angle system, we get the rotation of the joints. The code is as follows (visible onesegment.as):
package {
import flash.display.Sprite;
import flash.events.Event;
public class OneSegment extends Sprite {
private var segment0:Segment;
public function OneSegment() {
init();
}
private function init():void {
segment0 = new Segment(100, 20);
addChild(segment0);
segment0.x = stage.stageWidth / 2;
segment0.y = stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
var dx:Number = mouseX - segment0.x;
var dy:Number = mouseY - segment0.y;
var angle:Number = Math.atan2(dy, dx);
segment0.rotation = angle * 180 / Math.PI;
}
}
}
As shown in Figure 14-1, the results are run. Test to see how the joints follow the mouse. Even if the joint is far away, it's almost like catching the mouse.
Fig. 14-1 single joint to mouse stretching