Jump to content

Foot Placement with Inverse Kinematics


Josh
 Share

Recommended Posts

I wanted to make a character's feet align to the ground when standing on a slope or on stairs. This was very easy to get a basic version set up.

The code here only handles the feet, so the legs currently will get stretched out. Each model may require a little bit different final rotation.

The next step is to adjust the knee bones. The hip, knee, and foot form a triangle. One of the constructors for the Plane class accepts three points in space, so those three bones can be used to find a plane. Then you transform their positions to a matrix facing in the direction of the plane normal, solve the triangle, transform back, and that's how IK works.

void MyComponent::UpdateFeet()
{
    auto entity = GetEntity();
    auto world = entity->GetWorld();
    if (not world) return;
    for (int n = 0; n < footbones.size(); ++n)
    {
        if (not footbones[n]) continue;
        auto pos = footbones[n]->GetPosition(true);
        pos.y = 0;
        pos = TransformPoint(pos, entity, NULL);
        auto pickmode = entity->GetPickMode();
        entity->SetPickMode(PICK_NONE);
        auto pickinfo = world->Pick(pos + Vec3(0, footradius + 0.75, 0), pos - Vec3(0, footreach, 0), footradius, true);
        entity->SetPickMode(pickmode);
        if (pickinfo.entity)
        {
            pos = TransformPoint(pickinfo.position, NULL, entity);
            footbones[n]->SetPosition(pos, true);
            Vec3 normal = TransformNormal(pickinfo.normal, NULL, entity);
            footbones[n]->AlignToVector(-normal, 1);
            footbones[n]->Turn(0, 180, 0);
        }
    }
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...