diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
| -rw-r--r-- | Spear/Math/MatrixUtils.hs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 88ad3b1..e6e498f 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
| @@ -7,8 +7,11 @@ where | |||
| 7 | 7 | ||
| 8 | import Spear.Math.Matrix3 as M3 | 8 | import Spear.Math.Matrix3 as M3 |
| 9 | import Spear.Math.Matrix4 as M4 | 9 | import Spear.Math.Matrix4 as M4 |
| 10 | import Spear.Math.Vector2 as V2 | ||
| 11 | import Spear.Math.Vector3 as V3 | ||
| 10 | 12 | ||
| 11 | 13 | ||
| 14 | -- | Compute the normal matrix of the given matrix. | ||
| 12 | fastNormalMatrix :: Matrix4 -> Matrix3 | 15 | fastNormalMatrix :: Matrix4 -> Matrix3 |
| 13 | fastNormalMatrix m = | 16 | fastNormalMatrix m = |
| 14 | let m' = M4.transpose . M4.inverseTransform $ m | 17 | let m' = M4.transpose . M4.inverseTransform $ m |
| @@ -16,3 +19,44 @@ fastNormalMatrix m = | |||
| 16 | (M4.m00 m') (M4.m10 m') (M4.m20 m') | 19 | (M4.m00 m') (M4.m10 m') (M4.m20 m') |
| 17 | (M4.m01 m') (M4.m11 m') (M4.m21 m') | 20 | (M4.m01 m') (M4.m11 m') (M4.m21 m') |
| 18 | (M4.m02 m') (M4.m12 m') (M4.m22 m') | 21 | (M4.m02 m') (M4.m12 m') (M4.m22 m') |
| 22 | |||
| 23 | |||
| 24 | -- | Compute the inverse transform of the given transformation matrix. | ||
| 25 | -- | ||
| 26 | -- This function maps an object's transform in 2D to the object's inverse in 3D. | ||
| 27 | -- | ||
| 28 | -- The XY plane in 2D translates to the X(-Z) plane in 3D. | ||
| 29 | -- | ||
| 30 | -- Use this in games such as RPGs and RTSs. | ||
| 31 | rpgInverse :: Float -- ^ Height above the ground. | ||
| 32 | -> Matrix3 -> Matrix4 | ||
| 33 | rpgInverse h mat = | ||
| 34 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
| 35 | u = V3.unity | ||
| 36 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') | ||
| 37 | t = let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) | ||
| 38 | in mat4 | ||
| 39 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) | ||
| 40 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) | ||
| 41 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) | ||
| 42 | 0 0 0 1 | ||
| 43 | |||
| 44 | |||
| 45 | -- | Compute the inverse transform of the given transformation matrix. | ||
| 46 | -- | ||
| 47 | -- This function maps an object's transform in 2D to the object's inverse in 3D. | ||
| 48 | -- | ||
| 49 | -- The XY plane in 2D translates to the XY plane in 3D. | ||
| 50 | -- | ||
| 51 | -- Use this in games like platformers and space invaders style games. | ||
| 52 | pltInverse :: Matrix3 -> Matrix4 | ||
| 53 | pltInverse mat = | ||
| 54 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
| 55 | u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 | ||
| 56 | f = V3.unitz | ||
| 57 | t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 | ||
| 58 | in mat4 | ||
| 59 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) | ||
| 60 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) | ||
| 61 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) | ||
| 62 | 0 0 0 1 | ||
