XNA move individual bones

Started by
1 comment, last by Buckeye 14 years, 2 months ago
Can someone please tell me why I'm not able to move just the one bone called "MouthBone"? Nothings happening when I'm changing the value of "boneTestingY". Here's the .cs code:
[Source]
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GraphicsDevice device;
        SpriteBatch spriteBatch;
        Effect effect;
        Model model;
        Texture2D modelTex;
        Matrix worldMatrix;
        Matrix viewMatrix;
        Matrix projectionMatrix;
        Matrix[] originalTransforms;
        Vector3 lightDirection = new Vector3(0, 5, -5);
        VertexDeclaration vertexDecl;
        float modelRotation = 0.0f;
        float boneTestingY = 0.0f;
        float cameraZ = 0.0f;

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

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.IsFullScreen = false;
            Window.Title = "BoneTesting";
            graphics.ApplyChanges();
            lightDirection.Normalize();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            vertexDecl = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
            effect = Content.Load<Effect>("effects");
            model = Content.Load<Model>("testDude");
            modelTex = Content.Load<Texture2D>("SkinTex");
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart meshPart in mesh.MeshParts)
                {
                    meshPart.Effect = effect.Clone(device);
                }
            }
            originalTransforms = new Matrix[model.Bones.Count];
            for (int i = 0; i < model.Bones.Count; i++)
            {
                originalTransforms = model.Bones.Transform;
            }
            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 10, cameraZ), new Vector3(0, 0, -30), new Vector3(0, 1, 0));
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                modelRotation -= 1.5f;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                modelRotation += 1.5f;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                boneTestingY += 1.5f;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                boneTestingY -= 1.5f;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                cameraZ -= 1.5f;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                cameraZ += 1.5f;
            }
            // TODO: Add your update logic here
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);
            worldMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(modelRotation)) * Matrix.CreateTranslation(new Vector3(0, 0, -50));
            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 10, cameraZ), new Vector3(0, 0, -30), new Vector3(0, 1, 0));
            model.Bones["MainBone"].Transform = worldMatrix * originalTransforms[0];
            model.Bones["MouthBone"].Transform = Matrix.CreateTranslation(new Vector3(0, boneTestingY, 0)) * originalTransforms[0];
            Matrix[] modelTransforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(modelTransforms);
             foreach (ModelMesh mesh in model.Meshes)
             {
                 foreach (Effect currentEffect in mesh.Effects)
                 {
                     currentEffect.CurrentTechnique = currentEffect.Techniques["Textured"];
                     currentEffect.Parameters["xTexture"].SetValue(modelTex);
                     currentEffect.Parameters["xEnableLighting"].SetValue(true);
                     currentEffect.Parameters["xLightDirection"].SetValue(lightDirection);
                     currentEffect.Parameters["xAmbient"].SetValue(0.5f);
                     currentEffect.Parameters["xWorld"].SetValue(modelTransforms[mesh.ParentBone.Index] * worldMatrix);
                     currentEffect.Parameters["xView"].SetValue(viewMatrix);
                     currentEffect.Parameters["Bones"].SetValue(modelTransforms);
                     currentEffect.Parameters["xProjection"].SetValue(projectionMatrix);
                 }
                 mesh.Draw();
             }

            base.Draw(gameTime);
        }
    }
}
[/Source]
And here's the shader (Started with Riemers basic shader, and edited it adding from SkinningSample):
[Source]
//------------------------------------------------------
//--                                                	--
//--				www.riemers.net			--
//--				Basic shaders			--
//--			Use/modify as you like              --
//--                                                	--
//------------------------------------------------------

struct VertexToPixel
{
    float4 Position   	: POSITION;    
    float4 Color		: COLOR0;
    float LightingFactor: TEXCOORD0;
    float2 TextureCoords: TEXCOORD1;
};

struct VS_INPUT
{
	float4 BoneIndices : BLENDINDICES0;
    float4 BoneWeights : BLENDWEIGHT0;
};

struct PixelToFrame
{
    float4 Color : COLOR0;
};

//------- XNA-to-HLSL variables --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xLightDirection;
float xAmbient;
bool xEnableLighting;
bool xShowNormals;

float4x4 Bones[59];

//------- Texture Samplers --------

Texture xTexture;
sampler TextureSampler = sampler_state { texture = <xTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
//------- Technique: Textured --------

VertexToPixel TexturedVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float2 inTexCoords: TEXCOORD0, VS_INPUT input)
{	
	VertexToPixel Output = (VertexToPixel)0;
	float4x4 preViewProjection = mul (xView, xProjection);
	float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
    float4x4 skinTransform = 0;
    
    skinTransform += Bones[input.BoneIndices.x] * input.BoneWeights.x;
    skinTransform += Bones[input.BoneIndices.y] * input.BoneWeights.y;
    skinTransform += Bones[input.BoneIndices.z] * input.BoneWeights.z;
    skinTransform += Bones[input.BoneIndices.w] * input.BoneWeights.w;
    
    // Skin the vertex position.
    float4 position = mul(inPos, skinTransform);
	Output.Position = mul(position, preWorldViewProjection);	
	Output.TextureCoords = inTexCoords;
	
	float3 Normal = normalize(mul(normalize(inNormal), xWorld));	
	Output.LightingFactor = 1;
	if (xEnableLighting)
		Output.LightingFactor = saturate(dot(Normal, -xLightDirection));
    
	return Output;    
}

PixelToFrame TexturedPS(VertexToPixel PSIn) 
{
	PixelToFrame Output = (PixelToFrame)0;		
	
	Output.Color = tex2D(TextureSampler, PSIn.TextureCoords);
	Output.Color.rgb *= saturate(PSIn.LightingFactor + xAmbient);

	return Output;
}

technique Textured_2_0
{
	pass Pass0
	{   
		VertexShader = compile vs_2_0 TexturedVS();
		PixelShader  = compile ps_2_0 TexturedPS();
	}
}

technique Textured
{
	pass Pass0
	{   
		VertexShader = compile vs_1_1 TexturedVS();
		PixelShader  = compile ps_1_1 TexturedPS();
	}
}
[/Source]
CrazyPoly
Advertisement
Okay, so I figured it out somehow, but the stuff's getting all messed up. It seems the matrices mush be the cause. It's almost as if the pivot is placed way outside the object. I've made sure I'm not translating before I'm rotating, so I'm not sure what the problem might be. Any ideas? I've exported from 3ds, and I centered the pivot before export...
CrazyPoly
See my response to your previous post on the same subject.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement