From 31c3186bf2c9e91d55fe803201177534dd980cb4 Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Wed, 29 Aug 2012 10:44:53 +0200 Subject: Added fast, rpg and plt inverses --- Spear.lkshs | 4 ++-- Spear/Math/Matrix3.hs | 20 ++++++++++++++------ Spear/Math/MatrixUtils.hs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Spear.lkshs b/Spear.lkshs index 698ee98..6df61b6 100644 --- a/Spear.lkshs +++ b/Spear.lkshs @@ -1,8 +1,8 @@ Version of session file format: 1 Time of storage: - "Tue Aug 28 23:41:10 CEST 2012" -Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 2, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [("Browser",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 306) 200)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 692) 954 + "Wed Aug 29 00:16:28 CEST 2012" +Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 2, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [("Browser",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 318) 208)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 718) 954 Population: [(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/Camera.hs" 2313)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/Entity.hs" 1072)),[SplitP LeftP]),(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (ModulesSt (ModulesState 328 (PackageScope False,False) (Just (ModuleName ["Spear","Physics","Types"]),Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([[0,5],[0]],[]), packageDExp = ([],[]), packageDExpNoBlack = ([],[]), workspaceExp = ([],[]), workspaceExpNoBlack = ([],[]), workspaceDExp = ([],[]), workspaceDExpNoBlack = ([],[]), systemExp = ([],[]), systemExpNoBlack = ([],[])}))),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Physics/Rigid.hs" 1395)),[SplitP LeftP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP])] Window size: (1820,939) Completion size: diff --git a/Spear/Math/Matrix3.hs b/Spear/Math/Matrix3.hs index fe3162e..e24a3e5 100644 --- a/Spear/Math/Matrix3.hs +++ b/Spear/Math/Matrix3.hs @@ -7,7 +7,7 @@ module Spear.Math.Matrix3 , m20, m21, m22 , col0, col1, col2 , row0, row1, row2 -, right, forward, position +, right, up, forward, position -- * Construction , mat3 , mat3fromVec @@ -28,10 +28,10 @@ module Spear.Math.Matrix3 , reflectZ -- * Operations , transpose -, mul +, mul +, inverseTransform , Spear.Math.Matrix3.zipWith , Spear.Math.Matrix3.map ---, inverse ) where @@ -126,6 +126,7 @@ row2 (Matrix3 _ _ _ _ _ _ a02 a12 a22) = vec3 a02 a12 a22 right (Matrix3 a00 _ _ a01 _ _ _ _ _) = vec2 a00 a01 +up (Matrix3 _ a10 _ _ a11 _ _ _ _) = vec2 a10 a11 forward (Matrix3 _ a10 _ _ a11 _ _ _ _) = vec2 a10 a11 position (Matrix3 _ _ a20 _ _ a21 _ _ _) = vec2 a20 a21 @@ -278,9 +279,16 @@ map f m = Matrix3 (f . m02 $ m) (f . m12 $ m) (f . m22 $ m) --- | Invert the given 'Matrix3'. -{-inverse :: Matrix3 -> Matrix3 -inverse mat = -} +-- | Compute the inverse transform of the given transformation matrix. +inverseTransform :: Matrix3 -> Matrix3 +inverseTransform mat = + let r = right mat + f = forward mat + t = -(position mat) + in mat3 + (V2.x r) (V2.y r) (t `V2.dot` r) + (V2.x f) (V2.y f) (t `V2.dot` f) + 0 0 1 fromDeg :: (Floating a) => a -> a 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 import Spear.Math.Matrix3 as M3 import Spear.Math.Matrix4 as M4 +import Spear.Math.Vector2 as V2 +import Spear.Math.Vector3 as V3 +-- | Compute the normal matrix of the given matrix. fastNormalMatrix :: Matrix4 -> Matrix3 fastNormalMatrix m = let m' = M4.transpose . M4.inverseTransform $ m @@ -16,3 +19,44 @@ fastNormalMatrix m = (M4.m00 m') (M4.m10 m') (M4.m20 m') (M4.m01 m') (M4.m11 m') (M4.m21 m') (M4.m02 m') (M4.m12 m') (M4.m22 m') + + +-- | 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. +-- +-- The XY plane in 2D translates to the X(-Z) plane in 3D. +-- +-- Use this in games such as RPGs and RTSs. +rpgInverse :: Float -- ^ Height above the ground. + -> Matrix3 -> Matrix4 +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')) + 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 + + +-- | 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. +-- +-- The XY plane in 2D translates to the XY plane in 3D. +-- +-- Use this in games like platformers and space invaders style games. +pltInverse :: Matrix3 -> Matrix4 +pltInverse 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.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 -- cgit v1.2.3