A helping hand for bedroom coders throughout the land.
Avatars - Step One - Getting Started

Following my introduction to the series of articles I'm writing on the use of avatars in XNA games, (which I'd advise you to read before this article) it would only make sense to start with the basics of implementing avatars in an XNA 3.1 game. The following shows an implementation of how to get avatars into XNA:

Step 1: Start a new XNA 3.1 project, and select the Xbox 360 3.1 template.


Step 2: In the variable declarations add:

AvatarDescription avatarDescription; //Stores the data relating to an avatar's appearance in a byte array, and allows generation of a random avatar
AvatarRenderer avatarRenderer; //Allows an easy method to render avatars
AvatarAnimation avatarCelebrateAnimation; //Stores the animation keyframe bone matrices so that the bones of the avatar can be modified

Step 3: In the Game1 class constructor add:

Components.Add(new GamerServicesComponent(this)); //Avatars require the GamerServices component so we need to attach it

Step 4: In the LoadContent function add:

avatarDescription = AvatarDescription.CreateRandom(); //Generate a random avatar, by randomising the description byte array with acceptable values 
avatarRenderer = new AvatarRenderer(avatarDescription); //Create a renderer for the avatar we have just randomly created
avatarCelebrateAnimation = new AvatarAnimation(AvatarAnimationPreset.Celebrate); // Load the celebrate animation 

Step 5: In the Update function add:

if (avatarRenderer.IsLoaded) //If the avatar has been loaded, update the animation
{
avatarCelebrateAnimation.Update(gameTime.ElapsedGameTime, true);
}

Step 6: In the Draw function add:

avatarRenderer.View = Matrix.CreateLookAt(new Vector3(0, 1, -3), new Vector3(0, 1, 0), new Vector3(0, 1, 0)); //Set the view matrix supplying a sensible eye position, target and up direction
avatarRenderer.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, .01f, 40.0f); //Again a sensible projection matrix
avatarRenderer.Draw(avatarCelebrateAnimation.BoneTransforms, avatarCelebrateAnimation.Expression); //Now draw the avatar, passing to it the animations bones and expression texture

Step 7: Deploy and run on your Xbox 360

So how does all that work? Basically what happens is we store data relating to the appearance of the avatar, the avatar's animation and a system to render it. Every update call we move through the animation a little, and then in every draw call we render out the avatar according to its appearance description and current animation bone world matrices.

Complete source (Game1.cs)

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AvatarStepOne
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;

AvatarDescription avatarDescription;
AvatarRenderer avatarRenderer;
AvatarAnimation avatarCelebrateAnimation;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Components.Add(new GamerServicesComponent(this));
}

protected override void LoadContent()
{
avatarDescription = AvatarDescription.CreateRandom();
avatarRenderer = new AvatarRenderer(avatarDescription);
avatarCelebrateAnimation = new AvatarAnimation(AvatarAnimationPreset.Celebrate);
}

protected override void Update(GameTime gameTime)
{
// Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

if (avatarRenderer.IsLoaded)
{
avatarCelebrateAnimation.Update(gameTime.ElapsedGameTime, true);
}

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

avatarRenderer.View = Matrix.CreateLookAt(new Vector3(0, 1, -3), new Vector3(0, 1, 0), new Vector3(0, 1, 0));
avatarRenderer.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, .01f, 40.0f);
avatarRenderer.Draw(avatarCelebrateAnimation.BoneTransforms, avatarCelebrateAnimation.Expression);

base.Draw(gameTime);
}
}
}

Posted Sun, Jul 5 2009 6:18 PM by Barnaby Smith
Filed under: , , , ,

Comments

MVi wrote Avatars - Step Two - Persistent Avatar Descriptions
on Sun, Jul 12 2009 2:15 PM

Since it's not always convenient to be using random avatars or a signed in gamers avatar (especially