/// <summary> /// Given a displacement and a gravity, returns the smallest launch required launch velocity for a projectile. /// <para/> /// <b>NB:</b> If gravity is 0, the return vector will be 0. /// </summary> /// <param name="dx">The horizontal displacement. finalPosition.x - startPosition.x</param> /// <param name="dy">The vertical displacement. finalPosition.y - startPosition.y</param> /// <param name="g">The 2D gravity.</param> /// <returns>The smallest launch required launch velocity.</returns> public static fix2 SmallestLaunchVelocity(fix dx, fix dy, fix2 g) { // No gravity ? return 0 if (lengthsq(g) < global::fix.Epsilon) { return(new fix2(0, 0)); } // Gravity already 1D ? Don't rotate if (g.x == 0) { return(SmallestLaunchVelocity(dx, dy, g.y)); } // Rotate all the values so the gravity points downward fix gravityAngleAdjustment = angle2d(g) + global::fix.PiOver2; fix2x2 rot = fix2x2.Rotate(-gravityAngleAdjustment); fix2 d = new fix2(dx, dy); d = mul(rot, d); g = mul(rot, g); fix2 result = SmallestLaunchVelocity(d.x, d.y, g.y); // Rotate the result in opposite direction return(mul(inverse(rot), result)); }
/// <summary> /// Multiplies the two matrices. /// </summary> /// <param name="a">First matrix to multiply.</param> /// <param name="b">Second matrix to multiply.</param> /// <param name="result">Product of the multiplication.</param> public static void Multiply(ref fix2x3 a, ref fix3x2 b, out fix2x2 result) { result.M11 = a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31; result.M12 = a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32; result.M21 = a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31; result.M22 = a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32; }
/// <summary> /// Constructs a uniform scaling matrix. /// </summary> /// <param name="scale">Value to use in the diagonal.</param> /// <param name="matrix">Scaling matrix.</param> public static void CreateScale(fix scale, out fix2x2 matrix) { matrix.M11 = scale; matrix.M22 = scale; matrix.M12 = F64.C0; matrix.M21 = F64.C0; }
/// <summary> /// Computes the transposed matrix of a matrix. /// </summary> /// <param name="matrix">FixMatrix to transpose.</param> /// <param name="result">Transposed matrix.</param> public static void Transpose(ref fix2x2 matrix, out fix2x2 result) { fix m21 = matrix.M12; result.M11 = matrix.M11; result.M12 = matrix.M21; result.M21 = m21; result.M22 = matrix.M22; }
/// <summary> /// Transforms the vector by the matrix. /// </summary> /// <param name="v">FixVector2 to transform.</param> /// <param name="matrix">FixMatrix to use as the transformation.</param> /// <param name="result">Product of the transformation.</param> public static void Transform(ref fix2 v, ref fix2x2 matrix, out fix2 result) { fix vX = v.x; fix vY = v.y; #if !WINDOWS result = new fix2(); #endif result.x = vX * matrix.M11 + vY * matrix.M21; result.y = vX * matrix.M12 + vY * matrix.M22; }
/// <summary> /// Adds the two matrices together on a per-element basis. /// </summary> /// <param name="a">First matrix to add.</param> /// <param name="b">Second matrix to add.</param> /// <param name="result">Sum of the two matrices.</param> public static void Add(ref fix4x4 a, ref fix2x2 b, out fix2x2 result) { fix m11 = a.M11 + b.M11; fix m12 = a.M21 + b.M12; fix m21 = a.M12 + b.M21; fix m22 = a.M22 + b.M22; result.M11 = m11; result.M12 = m12; result.M21 = m21; result.M22 = m22; }
/// <summary> /// Subtracts the two matrices from each other on a per-element basis. /// </summary> /// <param name="a">First matrix to subtract.</param> /// <param name="b">Second matrix to subtract.</param> /// <param name="result">Difference of the two matrices.</param> public static void Subtract(ref fix2x2 a, ref fix2x2 b, out fix2x2 result) { fix m11 = a.M11 - b.M11; fix m12 = a.M12 - b.M12; fix m21 = a.M21 - b.M21; fix m22 = a.M22 - b.M22; result.M11 = m11; result.M12 = m12; result.M21 = m21; result.M22 = m22; }
/// <summary> /// Multiplies the two matrices. /// </summary> /// <param name="a">First matrix to multiply.</param> /// <param name="b">Second matrix to multiply.</param> /// <param name="result">Product of the multiplication.</param> public static void Multiply(ref fix4x4 a, ref fix2x2 b, out fix2x2 result) { fix resultM11 = a.M11 * b.M11 + a.M21 * b.M21; fix resultM12 = a.M11 * b.M12 + a.M21 * b.M22; fix resultM21 = a.M12 * b.M11 + a.M22 * b.M21; fix resultM22 = a.M12 * b.M12 + a.M22 * b.M22; result.M11 = resultM11; result.M12 = resultM12; result.M21 = resultM21; result.M22 = resultM22; }
/// <summary> /// Negates every element in the matrix. /// </summary> /// <param name="matrix">FixMatrix to negate.</param> /// <param name="result">Negated matrix.</param> public static void Negate(ref fix2x2 matrix, out fix2x2 result) { fix m11 = -matrix.M11; fix m12 = -matrix.M12; fix m21 = -matrix.M21; fix m22 = -matrix.M22; result.M11 = m11; result.M12 = m12; result.M21 = m21; result.M22 = m22; }
/// <summary> /// Inverts the given matix. /// </summary> /// <param name="matrix">FixMatrix to be inverted.</param> /// <param name="result">Inverted matrix.</param> public static void Invert(ref fix2x2 matrix, out fix2x2 result) { fix determinantInverse = F64.C1 / (matrix.M11 * matrix.M22 - matrix.M12 * matrix.M21); fix m11 = matrix.M22 * determinantInverse; fix m12 = -matrix.M12 * determinantInverse; fix m21 = -matrix.M21 * determinantInverse; fix m22 = matrix.M11 * determinantInverse; result.M11 = m11; result.M12 = m12; result.M21 = m21; result.M22 = m22; }