Hey I was just working on the CollisionDemo in Chapter 16 when it tells you to write the code:

MatrixDecompose(spheres.World, out trans, out scale, out rot); // on page 337

But it never goes on to tell you the code to the method that it is reffering to? I found the code on the CD but its kinda complicated and I just copyed and pasted it but I dont really know what it does... Here is the code I found on the CD if it will save you some time looking it up.

public void MatrixDecompose(Matrix mat,

out Vector3 trans,

out Vector3 scale,

out Matrix rot)

{

trans = Vector3.Zero;

scale = Vector3.Zero;

rot = Matrix.Identity;

Vector3[] cols = new Vector3[]{

new Vector3(mat.M11,mat.M12,mat.M13),

new Vector3(mat.M21,mat.M22,mat.M23),

new Vector3(mat.M31,mat.M32,mat.M33)

};

scale.X = cols[0].Length();

scale.Y = cols[1].Length();

scale.Z = cols[2].Length();

trans.X = mat.M41 / (scale.X == 0 ? 1 : scale.X);

trans.Y = mat.M42 / (scale.Y == 0 ? 1 : scale.Y);

trans.Z = mat.M43 / (scale.Z == 0 ? 1 : scale.Z);

if (scale.X != 0)

{

cols[0].X /= scale.X;

cols[0].Y /= scale.X;

cols[0].Z /= scale.X;

}

if (scale.Y != 0)

{

cols[1].X /= scale.Y;

cols[1].Y /= scale.Y;

cols[1].Z /= scale.Y;

}

if (scale.Z != 0)

{

cols[2].X /= scale.Z;

cols[2].Y /= scale.Z;

cols[2].Z /= scale.Z;

}

rot.M11 = cols[0].X;

rot.M12 = cols[0].Y;

rot.M13 = cols[0].Z;

rot.M14 = 0;

rot.M41 = 0;

rot.M21 = cols[1].X;

rot.M22 = cols[1].Y;

rot.M23 = cols[1].Z;

rot.M24 = 0;

rot.M42 = 0;

rot.M31 = cols[2].X;

rot.M32 = cols[2].Y;

rot.M33 = cols[2].Z;

rot.M34 = 0;

rot.M43 = 0;

rot.M44 = 1;

}

I forgot to change the code from my 1.0 book. The custom method I had there can be replaced with the built-in XNA Game Studio 3.0 Matrix.Decompose method.

The Decompose method extracts out a Matrix that contains rotation, scale and translation into its own components.

Here is a link to the help page on the built in method:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.matrix.decompose.aspx

It differs slightly with the one currently there. Instead of passing in the matrix you want to decompose, you call the Decompose on the matrix object. The rotation is returned as a Quaternion instead of a Matrix. Regardless, it accomplishes the same task:

"Extracts the scalar, translation, and rotation components from a 3D scale/rotate/translate (SRT) Matrix ."

Isn't it nice when the framework does all the heavy lifting for us?

So to incorporate the built in Decompose method from XNA Game Studio (which I suggest you do), replace the following code:

Vector3 trans, scale;

Matrix rot;

MatrixDecompose(spheres[i].World, out trans, out scale, out rot);

spheres[i].Radius = scale.Length();

with this code:

Vector3 trans, scale;

Quaternion rot;

spheres[i].World.Decompose(out scale, out rot, out trans);

spheres[i].Radius = scale.Length();

And you can remove the custom MatrixDecompose method from the project.

We are only using the scale portion of the matrix to set the radius of our sphere. This isn't required for the demo to run, but it makes it look a little nicer.

Hope this helps and thanks for pointing this out!

Chad

Copyright © 2019 GlobalCove Technologies