Loading the Animations

Although this model contains 21 animations, we will only be using some of them. The Animations property of a ModelAnimator object contains all the animations that can run for the specific model. The AnimationController object can take one of these animations and step through it.
  • This code snippet declares the member variables for the animations we will use:
    private AnimationController idle, run, walk, nod, crouch, stayCrouched;

  • Create an InitializeAnimations method and call it from the constructor. This snippet will show by example how to create animation objects. Also, the last line sets the current animation for all bones to be the idle animation:
    private void InitializeAnimations()
    {
       idle = new AnimationController(Game, animator.Animations["idle0"]);
       run = new AnimationController(Game, animator.Animations["run"]);
       walk = new AnimationController(Game, animator.Animations["walk"]);
       nod = new AnimationController(Game, animator.Animations["nodHead"]);
       crouch = new AnimationController(Game, animator.Animations["crouchDown"]);
       stayCrouched = new AnimationController(Game, animator.Animations["stayCrouched"]);

       foreach (BonePose p in bones)
           p.CurrentAnimation = idle;
    }

Rotations and Idle State

  • For now, lets only worry about walking. We want to transition to a new state when the user wants to walk and presses "w". However, if we go directly from idle to walk, it will look sloppy, so it is best to transition to an in between state called "idleToWalk"
  • We also want the user to be able to rotate the dwarf with the "a" and "d" keys. Unfortunately, there is no rotate animation, so we'll have to make do with just rotating the model.
  • Add this code block to UpdateEffects because we want to do the rotation logic only once per FSM call:
        KeyboardState keyState = Keyboard.GetState();
        if (keyState.IsKeyDown(Keys.D))
        {
            rotation *=
                Matrix.CreateFromAxisAngle(Vector3.Up, -MathHelper.Pi / 32.0f);

        }
        if (keyState.IsKeyDown(Keys.A))
        {
            rotation *=
                Matrix.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi / 32.0f);
        }
  • Set your RunCurrentState method to the following snippet. Notice the code that runs right before we transition to a new state. This represents the state transition function. The BlendFactor represents the amount to blend between the current animation and the animation to which we are blending.
        protected override void RunCurrentState(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            switch (CurrentState)
            {
                case "idle":
                    if (keyState.IsKeyDown(Keys.W))
                    {
                        foreach (BonePose p in bones)
                        {
                            p.CurrentBlendAnimation = walk;
                            p.BlendFactor = 0;
                        }
                        Transition("idleToWalk");
                    }
                    else
                    {
                        UpdateEffects();
                    }
                    break;
            }
        }

Last edited Feb 12, 2007 at 3:31 AM by dastle, version 3

Comments

No comments yet.