Created
June 15, 2019 04:11
-
-
Save bvisness/b51e6eee61c79ed22d67e48780e552b4 to your computer and use it in GitHub Desktop.
Quick matrix transformation demo
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
Mat4 RotationMatrix(float radians) | |
{ | |
Mat4 result; | |
// assuming column-major order (first index is column, second is row) | |
result[0][0] = Math.Cos(radians); | |
result[0][1] = -Math.Sin(radians); | |
result[1][0] = Math.Sin(radians); | |
result[1][1] = Math.Cos(radians); | |
return result; | |
} | |
Mat4 MirrorXMatrix() | |
{ | |
Mat4 result; | |
result[0][0] = -1.0f; | |
result[0][1] = 0.0f; | |
result[1][0] = 0.0f; | |
result[1][1] = 1.0f; | |
return result; | |
} | |
Mat4 ScaleMatrix(float x, float y) | |
{ | |
Mat4 result; | |
result[0][0] = x; | |
result[0][1] = 0; | |
result[1][0] = 0; | |
result[1][1] = y; | |
return result; | |
} | |
int main() | |
{ | |
float heading = 0.2f; | |
float scaleX = 2.0f; | |
float scaleY = 3.0f; | |
// because the order of transformations goes right to left, this matrix will rotate, then mirror, then scale. | |
Mat4 shipMatrix = ScaleMatrix(scaleX, scaleY) * MirrorXMatrix() * RotateMatrix(heading); | |
// assume we have ship.Points or whatever | |
for (int i = 0; i < ship.Points.Length; i++) | |
{ | |
Vec2 transformedPoint = shipMatrix * ship.Points[i]; | |
// now do whatever with it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment