This will show how to do cross dissolve blending between idle and walk.
  • Add a new member variable float named blendFactor. This will represent how much we are interpolating between the current animation and the target animation, on a scale of 0 to 1.0f.
    // Add this as a member variable
    float blendFactor = 0;
  • Now we want to add transition states and set the blendFactor appropriately when we enter them. Change the if statement in idle state to this:
    // Add this to the beginning of the if (state=="idle") block in the UpdateState method
    // Remove the old if (keyState.IsKeyDown(Keys.W)) block
    if (keyState.IsKeyDown(Keys.W))
    {
        blendFactor = 0;
        state = "idleToWalk";
    }
  • Likewise, change the statement in the walk state:
    // Add this to the beginning of the if (state=="walk") block in the UpdateState method
    // Remove the old if (keyState.IsKeyUp(Keys.W)) block
    if (keyState.IsKeyUp(Keys.W))
    {
        blendFactor = 0;
        state = "walkToIdle";
    }
  • Now, when the dwarf is moving between idle and walk, we want to increment the blend factor, scale the speed by the blend factor, and set the animations and blend animations to their appropriate values. Add this to the UpdateState() method:
    // Add this to the UpdateState method
    else if (state == "idleToWalk")
    {
        blendFactor += .1f;
        currentSpeed = blendFactor * WALK_SPEED;
        if (blendFactor >= 1)
        {
            blendFactor = 1;
            state = "walk";
        }
        foreach (BonePose p in poses)
        {
            p.CurrentController = idle;
            p.CurrentBlendController = walk;
            p.BlendFactor = blendFactor;
        }
    }
  • We want to do the same when the dwarf is moving between walking and idle. However, we will scale the speed by (1 - blendFactor) because the dwarf slows down as the blendFactor increases:
    // Add this to the UpdateState method
    else if (state == "walkToIdle")
    {
        blendFactor += .1f;
        currentSpeed = (1f-blendFactor) * WALK_SPEED;
        if (blendFactor >= 1)
        {
            blendFactor = 1;
            state = "idle";
        }
        foreach (BonePose p in poses)
        {
            p.CurrentController = walk;
            p.CurrentBlendController = idle;
            p.BlendFactor = blendFactor;
        }
    }

Last edited Mar 7, 2007 at 8:50 PM by dastle, version 10

Comments

Catalyst Oct 2, 2007 at 7:34 PM 
Great stuff.