Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

### Simple Kinematics

The term kinematics means a lot of things. To a 3D artist it means one thing, to the 3D game programmer it means another, and to a physicist it means yet another. However, in this section of the book it means the mechanics of moving linked chains of rigid bodies. In computer animation there are two kinematic problems. The first is called forward kinematics and the second is inverse kinematics. The forward kinematic problem is shown in Figure 13.38; here you see a 2D serially linked chain of rigid bodies (straight arms). Each joint can freely rotate in the plane, thus there are 2 degrees of freedom in this example, θ1, and θ2. In addition, each arm has length l1 and l2, respectively. The forward kinematic problem can be stated as follows:

Figure 13.38 The forward kinematic problem.

Given θ1, θ2, l1, and l2, find the position of p2.

Why are we interested in this? Well, if you are going to write a 2D or 3D game and want to have real-time models that have links that move around, then you better know how to do this. For example, 3D animation is accomplished in two ways. The quick and dirty method is to have a set of meshes that represent the 3D animation of an object. The more flexible method is to have a single 3D mesh that has a number of joints and arms and then to “play” motion data through the 3D model. However, to do this you must understand the physics/mechanics of how to move a hand in relation to the wrist, in relation to the elbow, in relation to the shoulder, in relation to the hips, and so forth—see the problem?

The second kinematic problem is the converse of the first:

Given the position of p2, find values θ1, θ2 that satisfy all the constraints l1, and l2 of the physical model. This is much harder than you can imagine. Figure 13.39 shows an example of why this is true.

Figure 13.39 The inverse kinematic problem.

Referring to the figure, you see that there are two possible solutions that satisfy all the constraints. I’m not going to tackle this problem in general since in most cases we never need to solve it, and the math is gnarly, but I will give an example later in the section of how to go about it.

#### Solving the Forward Kinematic Problem

What I want to do is show you how to solve the forward kinematic problem because it’s almost trivial. Referring back to Figure 13.38, the problem is nothing more than relative motions. If you look at the problem from joint 2, then locating p2 is nothing more than a translation of l2 and a rotation of θ2. However, the point p2 itself is just located as a translation from p1 of l1 and a rotation of θ1. Thus, the solution of the problem is nothing more than a series of translations and rotations from frame to frame or link to link. Let’s solve the problem in pieces.

Forget about the first arm and just focus on the second, that is, let’s work our way backward. The starting point is p1 and we want to move a distance l2 out on the x-axis and then rotate an angle θ2 around the x,y plane (or the z-axis in 3D) to locate the point p2. This is easy—all we need to do is the following transformation from p1:

    p2 = p1*Tl2*Rθ2

But we don’t have p1? That’s okay—we assume that we do for the derivation. Anyway, Tl2 and Rθ2 are the standard 2D translation and rotation matrices you learned about in Chapter 8, “Vector Rasterization and 2D Transformations.” Therefore, we have

          |1  0  0|        | cos θ2 sin θ2   0|

    Tl2 =  |0  1  0|   Rθ2 = |-sin θ2 cos θ2  0|

          |l1 0  1|         |  0      0     1|

Therefore, p2 is the product:

              |1  0  0|   | cos θ2 sin θ2  0|

    p2 = p1 *  |0  1  0| * |-sin θ2 cos θ2  0|

              |l2  0  1|   |  0     0      1|

Okay, if we can compute p2 from p1, then p1 should be p0 transformed in the same way, that is, translated by l1 and rotated by θ1. Or mathematically:

    p2 = p0*Tl2*Rθ2*Tl1*Rθ1

where p0 = [0,0,1], that is, the origin in homogenous 2D coordinates (we could use any point if we wanted, but basically this represents the base of the kinematic chain). The reason for 3 components in a 2D system is so we can use homogenous transforms and accomplish translation with matrices, hence that last 1.0 is a place holder. All points are in the form (x,y,1). Furthermore, Tl1 and Rθ1 are of the same form as Tl2 and Rθ2, but with different values. Note the order of multiplication—since we’re working backward, we must first transform p0 by Tl2*Rθ2 then by Tl1*Rθ1, so order counts!

With all that in mind, we see that the point p2 is really just the starting point p0 multiplied by the matrices (Tl2*Rθ2) * (Tl1*Rθ1). This holds for as many links as needed, or in general:

    pn = p0 * Tn*Rn * Tn-1*Rn-1 * Tn-2*Rn-2 *…* T1*R1

for n links.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.