Matrix Util : Matrix « 2D Graphics « C# / C Sharp






Matrix Util

       
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

public class Utils
{
    public static Vector3 Gravity = new Vector3(0.0f, -9.81f, 0.0f);

    public static void AddScaledVector(ref Vector3 vector, float scale, ref Vector3 result)
    {
        Vector3 temp;
        Vector3.Multiply(ref vector, scale, out temp);
        Vector3.Add(ref result, ref temp, out result);
    }

    public static void AddScaledVector(ref Vector3 vector, float scale, ref Quaternion result)
    {
        Quaternion q = new Quaternion(vector, scale);
        Quaternion.Multiply(ref q, ref result, out q);
        result.W += q.W * 0.5f;
        result.X += q.X * 0.5f;
        result.Y += q.Y * 0.5f;
        result.Z += q.Z * 0.5f;
    }

    public static Vector3 TransformInverseDirection(ref Vector3 vector, ref Matrix tensor)
    {
        return new Vector3(
            vector.X * tensor.M11 + vector.Y * tensor.M21 + vector.Z * tensor.M31,
            vector.X * tensor.M12 + vector.Y * tensor.M22 + vector.Z * tensor.M32,
            vector.X * tensor.M13 + vector.Y * tensor.M23 + vector.Z * tensor.M33
            );
    }

    public static void LinearInterpolation(ref Matrix a, ref Matrix b, float prop, out Matrix result)
    {
        // TODO: Optimize if needed!!
        Matrix t1;
        Matrix.Multiply(ref a, prop, out t1);

        Matrix t2;
        Matrix.Multiply(ref b, (1 - prop), out t2);

        Matrix.Add(ref t1, ref t1, out result);
    }

    public static Matrix CreateMatrix(float m11, float m12, float m13, float m21, float m22, float m23, float m31, float m32, float m33)
    {
        Matrix m = Matrix.Identity;
        m.M11 = m11; m.M12 = m12; m.M13 = m13;
        m.M21 = m21; m.M22 = m22; m.M23 = m23;
        m.M31 = m31; m.M32 = m32; m.M33 = m33;
        return m;
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.new Matrix(0.707f, 0.707f, -0.707f, 0.707f, 0, 0)
2.Text direction (Matrix Rotate)
3.Matrix DemoMatrix Demo
4.Matrix DrawMatrix Draw
5.Multiply two Matrixes