aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Spear.lkshs4
-rw-r--r--Spear/Math/Matrix3.hs20
-rw-r--r--Spear/Math/MatrixUtils.hs44
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 @@
1Version of session file format: 1Version of session file format:
2 1 2 1
3Time of storage: 3Time of storage:
4 "Tue Aug 28 23:41:10 CEST 2012" 4 "Wed Aug 29 00:16:28 CEST 2012"
5Layout: 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 5Layout: 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
6Population: [(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])] 6Population: [(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])]
7Window size: (1820,939) 7Window size: (1820,939)
8Completion size: 8Completion 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
7, m20, m21, m22 7, m20, m21, m22
8, col0, col1, col2 8, col0, col1, col2
9, row0, row1, row2 9, row0, row1, row2
10, right, forward, position 10, right, up, forward, position
11 -- * Construction 11 -- * Construction
12, mat3 12, mat3
13, mat3fromVec 13, mat3fromVec
@@ -28,10 +28,10 @@ module Spear.Math.Matrix3
28, reflectZ 28, reflectZ
29 -- * Operations 29 -- * Operations
30, transpose 30, transpose
31, mul 31, mul
32, inverseTransform
32, Spear.Math.Matrix3.zipWith 33, Spear.Math.Matrix3.zipWith
33, Spear.Math.Matrix3.map 34, Spear.Math.Matrix3.map
34--, inverse
35) 35)
36where 36where
37 37
@@ -126,6 +126,7 @@ row2 (Matrix3 _ _ _ _ _ _ a02 a12 a22) = vec3 a02 a12 a22
126 126
127 127
128right (Matrix3 a00 _ _ a01 _ _ _ _ _) = vec2 a00 a01 128right (Matrix3 a00 _ _ a01 _ _ _ _ _) = vec2 a00 a01
129up (Matrix3 _ a10 _ _ a11 _ _ _ _) = vec2 a10 a11
129forward (Matrix3 _ a10 _ _ a11 _ _ _ _) = vec2 a10 a11 130forward (Matrix3 _ a10 _ _ a11 _ _ _ _) = vec2 a10 a11
130position (Matrix3 _ _ a20 _ _ a21 _ _ _) = vec2 a20 a21 131position (Matrix3 _ _ a20 _ _ a21 _ _ _) = vec2 a20 a21
131 132
@@ -278,9 +279,16 @@ map f m = Matrix3
278 (f . m02 $ m) (f . m12 $ m) (f . m22 $ m) 279 (f . m02 $ m) (f . m12 $ m) (f . m22 $ m)
279 280
280 281
281-- | Invert the given 'Matrix3'. 282-- | Compute the inverse transform of the given transformation matrix.
282{-inverse :: Matrix3 -> Matrix3 283inverseTransform :: Matrix3 -> Matrix3
283inverse mat = -} 284inverseTransform mat =
285 let r = right mat
286 f = forward mat
287 t = -(position mat)
288 in mat3
289 (V2.x r) (V2.y r) (t `V2.dot` r)
290 (V2.x f) (V2.y f) (t `V2.dot` f)
291 0 0 1
284 292
285 293
286fromDeg :: (Floating a) => a -> a 294fromDeg :: (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
7 7
8import Spear.Math.Matrix3 as M3 8import Spear.Math.Matrix3 as M3
9import Spear.Math.Matrix4 as M4 9import Spear.Math.Matrix4 as M4
10import Spear.Math.Vector2 as V2
11import Spear.Math.Vector3 as V3
10 12
11 13
14-- | Compute the normal matrix of the given matrix.
12fastNormalMatrix :: Matrix4 -> Matrix3 15fastNormalMatrix :: Matrix4 -> Matrix3
13fastNormalMatrix m = 16fastNormalMatrix 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.
31rpgInverse :: Float -- ^ Height above the ground.
32 -> Matrix3 -> Matrix4
33rpgInverse 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.
52pltInverse :: Matrix3 -> Matrix4
53pltInverse 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