Skip to content

Instantly share code, notes, and snippets.

@JimBobSquarePants
Last active November 12, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimBobSquarePants/640baa97f4ac3b96f6e62fc0affdf50e to your computer and use it in GitHub Desktop.
Save JimBobSquarePants/640baa97f4ac3b96f6e62fc0affdf50e to your computer and use it in GitHub Desktop.
Demo of how matrix transforms differ
void Main()
{
Matrix4x4 yawPitchRoll = Matrix4x4.CreateFromYawPitchRoll(25, 32, 67);
Matrix4x4 translateM4 = Matrix4x4.CreateTranslation(new Vector3(3, 6, 0));
Matrix3x2 translate = Matrix3x2.CreateTranslation(new Vector2(3, 6));
Vector2 target = Vector2.Zero;
// All three of the following methods yield the correct transform vector <3,6>
Vector2.Transform(target, translate).Dump();
Vector2.Transform(target, translateM4).Dump();
TransformCorrect(target, translateM4).Dump();
// The following gives different values.
Vector2.Transform(target, yawPitchRoll).Dump(); // <0,0> WRONG!
TransformCorrect(target, yawPitchRoll).Dump(); // <-0.1335264, -0.6668726> CORRECT!
}
// Define other methods and classes here
public static Vector2 TransformCorrect(Vector2 vector, Matrix4x4 matrix)
{
const float e = .0000001F;
Vector3 v3 = Vector3.Transform(new Vector3(vector, 1F), matrix);
return new Vector2(v3.X, v3.Y) / Math.Max(v3.Z, e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment