From 1fe4f552e0edb8d454dfe3a86c8f310f451f977e Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Wed, 29 Aug 2012 12:20:16 +0200 Subject: Added rpg and plt transformers. Fixed height not being taken into account --- Spear/Math/MatrixUtils.hs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs index e6e498f..434e36a 100644 --- a/Spear/Math/MatrixUtils.hs +++ b/Spear/Math/MatrixUtils.hs @@ -1,6 +1,10 @@ module Spear.Math.MatrixUtils ( fastNormalMatrix +, rpgTransform +, pltTransform +, rpgInverse +, pltInverse ) where @@ -21,6 +25,35 @@ fastNormalMatrix m = (M4.m02 m') (M4.m12 m') (M4.m22 m') +-- | Maps the given 2D transformation matrix to a 3D transformation matrix. +rpgTransform :: Float -- ^ The height above the ground. + -> Matrix3 -> Matrix4 +rpgTransform h mat = + let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 + u = V3.unity + f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') + t = (vec3 0 h 0) + let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) + in mat4 + (V3.x r) (V3.x u) (V3.x f) (V3.x t) + (V3.y r) (V3.y u) (V3.y f) (V3.y t) + (V3.z r) (V3.z u) (V3.z f) (V3.z t) + 0 0 0 1 + + +-- | Maps the given 2D transformation matrix to a 3D transformation matrix. +pltTransform :: Matrix3 -> Matrix4 +pltTransform mat = + let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 + u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 + f = V3.unitz + t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 + in mat4 + (V3.x r) (V3.x u) (V3.x f) (V3.x t) + (V3.y r) (V3.y u) (V3.y f) (V3.y t) + (V3.z r) (V3.z u) (V3.z f) (V3.z t) + 0 0 0 1 + + -- | Compute the inverse transform of the given transformation matrix. -- -- This function maps an object's transform in 2D to the object's inverse in 3D. @@ -34,12 +67,12 @@ rpgInverse h mat = let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 u = V3.unity f = let f' = M3.forward mat in vec3 (V2.x f') 0 (-V2.y f') - t = let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) + t = (vec3 0 h 0) + let t' = M3.position mat in -(vec3 (V2.x t') 0 (-V2.y t')) in mat4 (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) - 0 0 0 1 + 0 0 0 1 -- | Compute the inverse transform of the given transformation matrix. @@ -59,4 +92,4 @@ pltInverse mat = (V3.x r) (V3.y r) (V3.z r) (t `V3.dot` r) (V3.x u) (V3.y u) (V3.z u) (t `V3.dot` u) (V3.x f) (V3.y f) (V3.z f) (t `V3.dot` f) - 0 0 0 1 + 0 0 0 1 -- cgit v1.2.3