Interacting with Objects (XNA Game Studio 4.0 Programming)

Sometimes you want the avatar to interact with items in your game. If you have a baseball game, you might want the avatars to be able to hold a baseball bat and throw a baseball. The avatar bones returned from the avatar animations and the values expected by the AvatarRenderer.Draw method are in local bone space.This means that the bones are relative to their parent bone and the bind pose for the avatar.To find the location of the avatar’s hand to place a baseball, find the world space position of the bone that represents the avatar’s hand. Then, use this bone to place the baseball model.

Let’s see how this works by creating an example of an avatar lifting a hand weight. Start with the previous code from this topic that draws a random avatar. For additional member variables, add the following member variables.

tmp14-170_thumb

 

 

 

Avatar drawn with a blended animation


Figure 10.10 Avatar drawn with a blended animation

The bonesWorldSpace stores the world space values for each of the bones that make up the avatar skeleton.You also need variables for the hand-weight model and the world matrix used to draw it.

In the LoadContent method, load the model and populate the list of bones in world space.Add the following lines of code to your game’s LoadContent method:

tmp14-172_thumb

Next, in the game’s Update method, calculate the world space transforms for the avatar animation. Add the following to your game’s Update method:

tmp14-173_thumb

Before you convert the bone transforms into world space, check that the AvatarRenderer.State property is set to Ready so you can verify that the avatar and its skeleton have completed loading.Then, call the ConvertBonesToWorldSpace method passing in the AvatarRenderer and AvatarAnimation we want to find the world space position of.After calculating the world space transforms, use the

AvatarBone.SpecialRight bones transform for the world matrix for the hand-weight model.The SpecialRight bone is a piece of the avatar skeleton that is located near the inside of the forearm in front of the hand. It is a good bone to use when you want to place objects in the avatar’s hand.

To implement the ConvertBonesToWorldSpace helper method, add the following method to your game:

tmp14-174

To calculate the bone’s position in world space, you first need the bind pose of the avatar.The animation frames are relative to the bind pose transforms, so you need both the animation transforms and the bind pose transforms. Each of the bones is also relative to its parent bone, so you also need to multiply the transform by its parent world space matrix. For the root node, use the World property of the AvatarRenderer because this is the location in world space of the avatar.Again, you can loop over the bones linearly as you calculate the world space transforms because you know the parent bones are calculated before any of the children.

Finally, you can draw the hand-weight model using the world space transform you calculated.Add the following lines of code to your game’s Draw method:

tmp14-175_thumb

Running the sample displays an avatar with the weight model in his or her right hand (see Figure 10.11).

Hand weight drawn at the avatar's hand location

Figure 10.11 Hand weight drawn at the avatar’s hand location

Next post:

Previous post: