You can use the below class draw line function to view the vectors in the matrix i added a method to draw the matrix orientations as lines at a position on screen you can choose.
Initialize this in load after you created a spritebatch pass it a dot texture you can get one from the class itself or a texture to use for the lines.
Call the following in draw between spritebatch begin end calls.
float t = 0f;
protected override void Draw(GameTime gameTime)
{
this.GraphicsDevice.Clear(Color.CornflowerBlue);
var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
t += elapsed / 5f;
if (t > 1f)
t = 0;
_spriteBatch.Begin();
var scale = 50f;
var offset1 = 100;
var offset2 = 200;
var offset3 = 300;
var axis = Vector3.One;
DrawHelpers.Draw2DVectorAxis(axis, scale, new Vector2(offset1, offset1));
DrawHelpers.Draw2DMatrixOrientationWithAxis(Matrix.Identity, axis, scale, new Vector2(offset1, offset1));
Matrix curr = Matrix.CreateWorld(Vector3.Zero, axis, Vector3.Up);
DrawHelpers.Draw2DMatrixOrientationWithAxis(curr, axis, scale, new Vector2(offset2, offset1));
var axis2 = curr.Up;
DrawHelpers.Draw2DVectorAxis(axis2, scale, new Vector2(offset1, offset2));
Matrix next = Matrix.CreateFromAxisAngle(axis2, t * 6.28f);
DrawHelpers.Draw2DMatrixOrientationWithAxis(next, axis2, scale, new Vector2(offset2, offset2));
Matrix final1 = next * curr;
var axis3 = final1.Up;
DrawHelpers.Draw2DMatrixOrientationWithAxis(final1, axis3, scale, new Vector2(offset1, offset3));
Matrix final2 = curr * next;
axis3 = final2.Up;
DrawHelpers.Draw2DMatrixOrientationWithAxis(final2, axis3, scale, new Vector2(offset2, offset3));
_spriteBatch.End();
base.Draw(gameTime);
}
…
public static class DrawHelpers
{
static SpriteBatch spriteBatch;
static Texture2D dot;
/// <summary>
/// Flips atan direction to xna spritebatch rotational alignment defaults to true.
/// </summary>
public static bool SpriteBatchAtan2 = true;
public static void Initialize(SpriteBatch spriteBatch, Texture2D dot)
{
DrawHelpers.spriteBatch = spriteBatch;
if(DrawHelpers.dot == null)
DrawHelpers.dot = dot;
}
public static void DrawRectangleOutline(Rectangle r, int lineThickness, Color c)
{
DrawSquareBorder(r, lineThickness, c);
}
public static void DrawSquareBorder(Rectangle r, int lineThickness, Color c)
{
Rectangle TLtoR = new Rectangle(r.Left, r.Top, r.Width, lineThickness);
Rectangle BLtoR = new Rectangle(r.Left, r.Bottom - lineThickness, r.Width, lineThickness);
Rectangle LTtoB = new Rectangle(r.Left, r.Top, lineThickness, r.Height);
Rectangle RTtoB = new Rectangle(r.Right - lineThickness, r.Top, lineThickness, r.Height);
spriteBatch.Draw(dot, TLtoR, c);
spriteBatch.Draw(dot, BLtoR, c);
spriteBatch.Draw(dot, LTtoB, c);
spriteBatch.Draw(dot, RTtoB, c);
}
public static void DrawBasicLine(Vector2 s, Vector2 e, int thickness, Color linecolor)
{
Rectangle screendrawrect = new Rectangle((int)s.X, (int)s.Y, thickness, (int)Vector2.Distance(e, s));
float rot = (float)Atan2Xna(e.X - s.X, e.Y - s.Y);
spriteBatch.Draw(dot, screendrawrect, new Rectangle(0, 0, 1, 1), linecolor, rot, Vector2.Zero, SpriteEffects.None, 0);
}
public static void DrawBasicPoint(Vector2 p, Color c)
{
Rectangle screendrawrect = new Rectangle((int)p.X, (int)p.Y, 2, 2);
spriteBatch.Draw(dot, screendrawrect, new Rectangle(0, 0, 1, 1), c, 0.0f, Vector2.One, SpriteEffects.None, 0);
}
public static float Atan2Xna(float difx, float dify)
{
if (SpriteBatchAtan2)
return (float)System.Math.Atan2(difx, dify) * -1f;
else
return (float)System.Math.Atan2(difx, dify);
}
public static void Draw2DVectorAxis(Vector3 axis, float scale, Vector2 postion)
{
var a = new Vector2(axis.X, axis.Y) * scale + postion;
var n = new Vector2(-axis.X, -axis.Y) * (scale * .25f) + postion;
DrawBasicLine(postion, a, 1, Color.White);
DrawBasicLine(postion, n, 1, Color.Gray);
}
public static void Draw2DMatrixOrientation(Matrix m, float scale, Vector2 postion)
{
var f = new Vector2(m.Forward.X, m.Forward.Y) * scale + postion;
var r = new Vector2(m.Right.X, m.Right.Y) * scale + postion;
var u = new Vector2(m.Up.X, m.Up.Y) * scale + postion;
DrawBasicLine(postion, f, 1, Color.Green);
DrawBasicLine(postion, u, 1, Color.Red);
DrawBasicLine(postion, r, 1, Color.Blue);
}
public static void Draw2DMatrixOrientationWithAxis(Matrix m, Vector3 axis, float scale, Vector2 postion)
{
var f = new Vector2(m.Forward.X, m.Forward.Y) * scale + postion;
var r = new Vector2(m.Right.X, m.Right.Y) * scale + postion;
var u = new Vector2(m.Up.X, m.Up.Y) * scale + postion;
var a = new Vector2(axis.X, axis.Y) * scale + postion;
var n = new Vector2(-axis.X, -axis.Y) * (scale * .25f) + postion;
DrawBasicLine(postion, a, 1, Color.White);
DrawBasicLine(postion, n, 1, Color.Gray);
DrawBasicLine(postion, f, 1, Color.Green);
DrawBasicLine(postion, u, 1, Color.Red);
DrawBasicLine(postion, r, 1, Color.Blue);
}
public static Vector2 MatrixSpriteBatchOut(SpriteBatch spriteBatch, SpriteFont font, Vector2 textPosition, Color col, Matrix m, string name)
{
var textPos = textPosition;
spriteBatch.DrawString(font, name, textPos, col);
textPos.Y += font.LineSpacing;
float spacing = 110;
spriteBatch.DrawString(font, " M11: " + m.M11.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M12: " + m.M12.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M13: " + m.M13.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M14: " + m.M14.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " Right ", textPos, col); textPos.X += spacing;
textPos.X = textPosition.X; textPos.Y += font.LineSpacing;
spriteBatch.DrawString(font, " M21: " + m.M21.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M22: " + m.M22.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M23: " + m.M23.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M24: " + m.M24.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " Up ", textPos, col); textPos.X += spacing;
textPos.X = textPosition.X; textPos.Y += font.LineSpacing;
spriteBatch.DrawString(font, " M31: " + m.M31.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M32: " + m.M32.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M33: " + m.M33.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M34: " + m.M34.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " Forward ", textPos, col); textPos.X += spacing;
textPos.X = textPosition.X; textPos.Y += font.LineSpacing;
spriteBatch.DrawString(font, " M41: " + m.M41.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M42: " + m.M42.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M43: " + m.M43.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " M44: " + m.M44.ToString("#0.000"), textPos, col); textPos.X += spacing;
spriteBatch.DrawString(font, " Position ", textPos, col); textPos.X += spacing;
textPos.X = textPosition.X; textPos.Y += font.LineSpacing;
return textPos;
}
public static string MatrixToString(Matrix m, string name)
{
return
name +
"\n Right: " + m.Right.ToString() + " M41: " + m.M41.ToString() +
"\n Up: " + m.Up.ToString() + " M42: " + m.M42.ToString() +
"\n Forward: " + m.Forward.ToString() + " M43: " + m.M43.ToString() +
"\n Position: " + m.Translation.ToString() + " M44: " + m.M44.ToString();
}
}