AI CHOREOGRAPHY / STUDY / LEVEL 5 OF 7
The coach's
body.
The routine only says where the coach's hands go. Nobody stores where the elbows are. The game works them out every frame: given a shoulder, a hand and two bones of fixed length, where must the elbow be? This is inverse kinematics (IK), and the engine does it in two lines, cg.ik2. The same function places the knees when the coach squats.
One arm, from the front
the arm on your left as you face the coach (its right arm)Controls
cg.ik2 from cg.jsFor any shoulder and hand there is a whole circle of possible elbows. The hint picks the one on the side you want: out and down looks natural; "up" looks like a chicken wing.
A squat, from the side
hip lowered, foot on the floor, knee solvedSquat
coach.jsshown.squatThe game never sets a knee angle. It lowers the hip, keeps the foot where it is, and asks IK for the knee, with a hint that points forward so the knee bends toward you.
The real code
coach.jsSmoothing the shown pose, placing the hip for a squat, then pose2: clamp the reach and call cg.ik2 for every arm and leg.
The engine's IK
cg.jsThis page imports these exact functions.
What you should be able to explain
- Forward vs inverse kinematics. Forward: joint angles in, hand position out (the rod in Haptic Fishing level 5). Inverse: hand position in, joint positions out. Games use IK whenever a body must reach a point: a hand on a target, a foot on the ground.
- Two bones, one answer, with a hint. With the shoulder and hand fixed and both bone lengths known, the elbow can lie anywhere on a circle around the shoulder–hand line. The hint vector chooses the point on that circle nearest to its direction.
- Reach. If the hand is farther than the two bones can stretch,
pose2pulls it back onto the reach circle first; otherwise there is no solution (the square root would be of a negative number). - Smoothing. The shown pose moves a fraction
1 − e−18·dtof the remaining way each frame. This exponential smoothing is independent of frame rate and turns a sudden jump into a quick, soft glide.
Exercises: ① Drag the target beyond the dashed circle. What happens to the arm, and to the numbers? ② Try each elbow hint with the same target. Which would look wrong on a human, and why? ③ Turn smoothing off and press "jump" a few times. How does the motion feel different? ④ Push the squat to 1: how much lower is the hip, and where does the knee go?