Last active
November 12, 2018 11:33
-
-
Save JimBobSquarePants/640baa97f4ac3b96f6e62fc0affdf50e to your computer and use it in GitHub Desktop.
Demo of how matrix transforms differ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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