diff options
Diffstat (limited to 'Spear/Math/MatrixUtils.hs')
| -rw-r--r-- | Spear/Math/MatrixUtils.hs | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index 68ad6cd..2c1d083 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | module Spear.Math.MatrixUtils | 1 | module Spear.Math.MatrixUtils |
| 2 | ( | 2 | ( |
| 3 | fastNormalMatrix | 3 | Rotation(..) |
| 4 | , fastNormalMatrix | ||
| 4 | , rpgTransform | 5 | , rpgTransform |
| 5 | , pltTransform | 6 | , pltTransform |
| 6 | , rpgInverse | 7 | , rpgInverse |
| @@ -15,6 +16,9 @@ import Spear.Math.Vector2 as V2 | |||
| 15 | import Spear.Math.Vector3 as V3 | 16 | import Spear.Math.Vector3 as V3 |
| 16 | 17 | ||
| 17 | 18 | ||
| 19 | data Rotation = Yaw | Pitch | Roll deriving Eq | ||
| 20 | |||
| 21 | |||
| 18 | -- | Compute the normal matrix of the given matrix. | 22 | -- | Compute the normal matrix of the given matrix. |
| 19 | fastNormalMatrix :: Matrix4 -> Matrix3 | 23 | fastNormalMatrix :: Matrix4 -> Matrix3 |
| 20 | fastNormalMatrix m = | 24 | fastNormalMatrix m = |
| @@ -26,13 +30,26 @@ fastNormalMatrix m = | |||
| 26 | 30 | ||
| 27 | 31 | ||
| 28 | -- | Maps the given 2D transformation matrix to a 3D transformation matrix. | 32 | -- | Maps the given 2D transformation matrix to a 3D transformation matrix. |
| 29 | rpgTransform :: Float -- ^ The height above the ground. | 33 | rpgTransform |
| 30 | -> Matrix3 -> Matrix4 | 34 | :: Float -- ^ The height above the ground. |
| 31 | rpgTransform h mat = | 35 | -> Float -- ^ Angle of rotation. |
| 32 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | 36 | -> Rotation -- ^ How the 2D rotation should be interpreted in 3D. |
| 37 | -> Matrix3 | ||
| 38 | -> Matrix4 | ||
| 39 | rpgTransform h a rtype mat = | ||
| 40 | {-let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
| 33 | u = V3.unity | 41 | u = V3.unity |
| 34 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (V2.y f') | 42 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (V2.y f') |
| 35 | t = (vec3 0 h 0) + let t' = M3.position mat in vec3 (V2.x t') 0 (V2.y t') | 43 | t = (vec3 0 h 0) + let t' = M3.position mat in vec3 (V2.x t') 0 (V2.y t')-} |
| 44 | let rot = case rtype of | ||
| 45 | Yaw -> rotY | ||
| 46 | Pitch -> rotX | ||
| 47 | Roll -> rotZ | ||
| 48 | mat' = rot a | ||
| 49 | r = M4.right mat' | ||
| 50 | u = M4.up mat' | ||
| 51 | f = M4.forward mat' | ||
| 52 | t = vec3 0 h 0 + let t' = M3.position mat in vec3 (V2.x t') 0 (V2.y t') | ||
| 36 | in mat4 | 53 | in mat4 |
| 37 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) | 54 | (V3.x r) (V3.x u) (V3.x f) (V3.x t) |
| 38 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) | 55 | (V3.y r) (V3.y u) (V3.y f) (V3.y t) |
| @@ -61,18 +78,13 @@ pltTransform mat = | |||
| 61 | -- The XY plane in 2D translates to the X(-Z) plane in 3D. | 78 | -- The XY plane in 2D translates to the X(-Z) plane in 3D. |
| 62 | -- | 79 | -- |
| 63 | -- Use this in games such as RPGs and RTSs. | 80 | -- Use this in games such as RPGs and RTSs. |
| 64 | rpgInverse :: Float -- ^ Height above the ground. | 81 | rpgInverse |
| 65 | -> Matrix3 -> Matrix4 | 82 | :: Float -- ^ The height above the ground. |
| 66 | rpgInverse h mat = | 83 | -> Float -- ^ Angle of rotation. |
| 67 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | 84 | -> Rotation -- ^ How the 2D rotation should be interpreted in 3D. |
| 68 | u = V3.unity | 85 | -> Matrix3 |
| 69 | f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') | 86 | -> Matrix4 |
| 70 | t = (vec3 0 h 0) + let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) | 87 | rpgInverse h a rot = M4.inverseTransform . rpgTransform h a rot |
| 71 | in mat4 | ||
| 72 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) | ||
| 73 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) | ||
| 74 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) | ||
| 75 | 0 0 0 1 | ||
| 76 | 88 | ||
| 77 | 89 | ||
| 78 | -- | Compute the inverse transform of the given transformation matrix. | 90 | -- | Compute the inverse transform of the given transformation matrix. |
| @@ -83,13 +95,4 @@ rpgInverse h mat = | |||
| 83 | -- | 95 | -- |
| 84 | -- Use this in games like platformers and space invaders style games. | 96 | -- Use this in games like platformers and space invaders style games. |
| 85 | pltInverse :: Matrix3 -> Matrix4 | 97 | pltInverse :: Matrix3 -> Matrix4 |
| 86 | pltInverse mat = | 98 | pltInverse = M4.inverseTransform . pltTransform |
| 87 | let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 | ||
| 88 | u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 | ||
| 89 | f = V3.unitz | ||
| 90 | t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 | ||
| 91 | in mat4 | ||
| 92 | (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) | ||
| 93 | (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) | ||
| 94 | (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) | ||
| 95 | 0 0 0 1 | ||
