Game Development Reference
In-Depth Information
How to do it...
Perform the following steps to get the basic IK functionality:
1. Let's begin by creating a new class that extends AbstractControl , and define
a list that will contain the bones we want to use as tip bones.
2. In the setSpatial method, we add both the feet and toe bones to the list. We
also supply some values that KinematicRagdollControl should work with
when applying the IK and tell it which bones to work with, as shown in the follow-
ing code:
setupJaime(spatial.getControl(KinematicRagdollControl.class));
spatial.getControl(KinematicRagdollControl.class).setIKThreshold(0.01f);
spatial.getControl(KinematicRagdollControl.class).setLimbDampening(0.9f);
spatial.getControl(KinematicRagdollControl.class).setIkRotSpeed(18);
3. We create a method called sampleTargetPositions that goes through each
of our targets and finds a position the control should try to reach, as shown in the
following code:
public void sampleTargetPositions(){
float offset = -1.9f;
for(Bone bone: targets){
Vector3f targetPosition =
bone.getModelSpacePosition().add(spatial.getWorldTranslation());
CollisionResult closestResult =
contactPointForBone(targetPosition, offset);
if(closestResult != null){
spatial.getControl(KinematicRagdollControl.class).setIKTarget(bone,
closestResult.getContactPoint().addLocal(0, 0.05f, 0),
2);
}
}
4. Finally, in the created method, we call KinematicRagdollControl and tell it
to switch to the Inverse Kinematics mode:
spatial.getControl(KinematicRagdollControl.class).setIKMode();
Search WWH ::




Custom Search