The basic idea is to consider the stages of the arm one after another and use transformations to move from one to the next.
Suppose the three arm segments have lengths L1, L2, and L3 and the angles of rotation at the three joints are A1, A2, and A3.
The first joint rotates the arm by angle A1 at a fixed shoulder. The second joint is translated distance L1 away in the direction of the first arm segment.
The second joint rotates the arm by angle A2 at the "elbow." The third joint is translated distance L2 away in the direction of the second arm segment.
The third joint rotates the arm by angle A3 at the "wrist." The end of the arm is translated distance L3 away in the direction of the third arm segment.
To draw the arm, start by drawing the shoulder at the origin. This example adds a translation first to center the shoulder in the drawing area.
Next the code applies the rotation A1 at the shoulder and draws a rectangle representing the first arm segment. There are is one tricky issue here.
The rotation is prepended to the previous translation so the rotation occurs before the translation that centers the shoulder. Imagine the arm in its own world coordinate space with the shoulder at the origin. The program draws a horizontal rectangle representing the first arm segment. The transformations then rotate it to the correct angle and translate it so the shoulder is centered. This puts the first arm segment in the correct position.
Next the code must draw the elbow joint. To do this, it prepends a translation of distance L1 in the X direction and 0 in the Y direction. It then draws the elbow joint at the origin.
Again imagine the arm in world coordinate space and suppose you draw the elbow at the origin. The new translation moves the joint to (L1, 0). The rotation of angle A1 rotates the joint to match the rotation of the first arm segment. Finally the shoulder's translation moves the whole thing so the base of the first arm segment is centered in the drawing area.
The code continues like this as many times as necessary to draw each arm segment and joint. In eachv step, it prepends the new translations and rotations.
The following code shows how the program draws the arm. The code is quite simple; it's understanding the code that's tricky.
The TrackBar controls tbarJoint1, tbarJoint2, and tbarJoint3 let you adjust the joint angles at run time.
|