diff options
author | Marc Sunet <jeannekamikaze@gmail.com> | 2012-09-03 19:02:57 +0200 |
---|---|---|
committer | Marc Sunet <jeannekamikaze@gmail.com> | 2012-09-03 19:02:57 +0200 |
commit | 7f691d48a462364c76edd302f797482cdc42820f (patch) | |
tree | ffd96425d8b43a7bbdac82354d77187a7badab40 | |
parent | fa3e0779521777b94aa1649b81222693150b5299 (diff) |
Added 2d bounding box functions
-rw-r--r-- | Spear.lkshs | 8 | ||||
-rw-r--r-- | Spear.lkshw | 2 | ||||
-rw-r--r-- | Spear/Collision.hs | 47 | ||||
-rw-r--r-- | Spear/Render/AnimatedModel.hs | 30 | ||||
-rw-r--r-- | Spear/Render/StaticModel.hs | 14 |
5 files changed, 81 insertions, 20 deletions
diff --git a/Spear.lkshs b/Spear.lkshs index 9d57ffa..92719f1 100644 --- a/Spear.lkshs +++ b/Spear.lkshs | |||
@@ -1,14 +1,14 @@ | |||
1 | Version of session file format: | 1 | Version of session file format: |
2 | 1 | 2 | 1 |
3 | Time of storage: | 3 | Time of storage: |
4 | "Mon Sep 3 00:01:24 CEST 2012" | 4 | "Mon Sep 3 14:06:47 CEST 2012" |
5 | Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 4, 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}) 346) 184),("Debug",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 1, detachedId = Nothing, detachedSize = Nothing}) 265)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 688) 954 | 5 | Layout: VerticalP (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 1, 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}) 344) 159),("Debug",HorizontalP (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Just TopP, currentPage = 1, detachedId = Nothing, detachedSize = Nothing}) 245)], paneTabs = Just BottomP, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) (TerminalP {paneGroups = fromList [], paneTabs = Nothing, currentPage = 0, detachedId = Nothing, detachedSize = Nothing}) 643) 954 |
6 | Population: [(Just (BreakpointsSt BreakpointsState),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP BottomP]),(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Scene/GameObject.hs" 217)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/Game/GameObject.hs" 2615)),[SplitP LeftP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/MatrixUtils.hs" 1873)),[SplitP LeftP]),(Just (ModulesSt (ModulesState 328 (PackageScope False,False) (Nothing,Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([[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/demos/simple-scene/Game/GameObject/Player.hs" 858)),[SplitP LeftP]),(Just (SearchSt (SearchState {searchString = "putStrLn", searchScope = PackageScope False, searchMode = Prefix {caseSense = False}})),[SplitP RightP,SplitP TopP]),(Just (TraceSt TraceState),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP BottomP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/Game/GameObject/Utils.hs" 471)),[SplitP LeftP]),(Just (VariablesSt VariablesState),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP BottomP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP]),(Just (BufferSt (BufferStateTrans "_Eval.hs" "\n" 0)),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/main.hs" 4657)),[SplitP LeftP])] | 6 | Population: [(Just (BreakpointsSt BreakpointsState),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP BottomP]),(Just (ErrorsSt ErrorsState),[SplitP RightP,SplitP TopP]),(Just (FilesSt FilesState),[SplitP RightP,SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Scene/GameObject.hs" 5628)),[SplitP LeftP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/Game/GameObject.hs" 2636)),[SplitP LeftP]),(Just (InfoSt (InfoState Nothing)),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP BottomP,SplitP BottomP]),(Just (LogSt LogState),[SplitP RightP,SplitP BottomP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/Spear/Math/MatrixUtils.hs" 615)),[SplitP LeftP]),(Just (ModulesSt (ModulesState 328 (PackageScope False,False) (Nothing,Nothing) (ExpanderState {packageExp = ([],[]), packageExpNoBlack = ([],[]), 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/demos/simple-scene/Game/GameObject/Player.hs" 4488)),[SplitP LeftP]),(Just (SearchSt (SearchState {searchString = "putStrLn", searchScope = PackageScope False, searchMode = Prefix {caseSense = False}})),[SplitP RightP,SplitP TopP]),(Just (TraceSt TraceState),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP BottomP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/Game/GameObject/Utils.hs" 1454)),[SplitP LeftP]),(Just (VariablesSt VariablesState),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP BottomP]),(Just (WorkspaceSt WorkspaceState),[SplitP RightP,SplitP TopP,GroupP "Browser",SplitP TopP]),(Just (BufferSt (BufferStateTrans "_Eval.hs" "\n" 0)),[SplitP RightP,SplitP TopP,GroupP "Debug",SplitP TopP]),(Just (BufferSt (BufferState "/home/jeanne/programming/haskell/Spear/demos/simple-scene/main.hs" 4562)),[SplitP LeftP])] |
7 | Window size: (1820,944) | 7 | Window size: (1820,944) |
8 | Completion size: | 8 | Completion size: |
9 | (750,399) | 9 | (750,399) |
10 | Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw" | 10 | Workspace: Just "/home/jeanne/programming/haskell/Spear/Spear.lkshw" |
11 | Active pane: Just "Utils.hs" | 11 | Active pane: Just "GameObject.hs(1)" |
12 | Toolbar visible: | 12 | Toolbar visible: |
13 | True | 13 | True |
14 | FindbarState: (False,FindState {entryStr = "asdad", entryHist = ["asdad","m[15]","m[14]","m[1]","m[13]","m[12]","m[11]","m[10]","m[9]","m[8]","m[7]","m[6]"], replaceStr = "a01", replaceHist = [], caseSensitive = True, entireWord = False, wrapAround = False, regex = False, lineNr = 1}) | 14 | FindbarState: (False,FindState {entryStr = "asdad", entryHist = ["asdad","m[15]","m[14]","m[1]","m[13]","m[12]","m[11]","m[10]","m[9]","m[8]","m[7]","m[6]"], replaceStr = "a01", replaceHist = [], caseSensitive = True, entireWord = False, wrapAround = False, regex = False, lineNr = 1}) |
diff --git a/Spear.lkshw b/Spear.lkshw index fce08ab..ba05db3 100644 --- a/Spear.lkshw +++ b/Spear.lkshw | |||
@@ -1,7 +1,7 @@ | |||
1 | Version of workspace file format: | 1 | Version of workspace file format: |
2 | 1 | 2 | 1 |
3 | Time of storage: | 3 | Time of storage: |
4 | "Mon Sep 3 11:26:24 CEST 2012" | 4 | "Mon Sep 3 18:57:03 CEST 2012" |
5 | Name of the workspace: | 5 | Name of the workspace: |
6 | "Spear" | 6 | "Spear" |
7 | File paths of contained packages: | 7 | File paths of contained packages: |
diff --git a/Spear/Collision.hs b/Spear/Collision.hs index fb8f11e..f0f5814 100644 --- a/Spear/Collision.hs +++ b/Spear/Collision.hs | |||
@@ -7,8 +7,10 @@ module Spear.Collision | |||
7 | , Collisioner(..) | 7 | , Collisioner(..) |
8 | -- ** Construction | 8 | -- ** Construction |
9 | , aabbCollisioner | 9 | , aabbCollisioner |
10 | , sphereCollisioner | 10 | , circleCollisioner |
11 | , boxFromCircle | ||
11 | , buildAABB | 12 | , buildAABB |
13 | , mkCols | ||
12 | -- ** Collision test | 14 | -- ** Collision test |
13 | , collide | 15 | , collide |
14 | -- ** Manipulation | 16 | -- ** Manipulation |
@@ -19,10 +21,13 @@ module Spear.Collision | |||
19 | where | 21 | where |
20 | 22 | ||
21 | 23 | ||
24 | import Spear.Assets.Model | ||
22 | import Spear.Math.AABB | 25 | import Spear.Math.AABB |
23 | import Spear.Math.Circle | 26 | import Spear.Math.Circle |
27 | import qualified Spear.Math.Matrix4 as M4 | ||
24 | import Spear.Math.Plane | 28 | import Spear.Math.Plane |
25 | import Spear.Math.Vector2 | 29 | import Spear.Math.Vector2 |
30 | import qualified Spear.Math.Vector3 as V3 | ||
26 | 31 | ||
27 | 32 | ||
28 | -- | Encodes several collision situations. | 33 | -- | Encodes several collision situations. |
@@ -48,12 +53,12 @@ instance Collisionable AABB where | |||
48 | | box2 `aabbpt` min1 && box2 `aabbpt` max1 = FullyContainedBy | 53 | | box2 `aabbpt` min1 && box2 `aabbpt` max1 = FullyContainedBy |
49 | | otherwise = Collision | 54 | | otherwise = Collision |
50 | 55 | ||
51 | collideCircle sphere@(Circle c r) aabb@(AABB min max) | 56 | collideCircle circle@(Circle c r) aabb@(AABB min max) |
52 | | test == FullyContains || test == FullyContainedBy = test | 57 | | test == FullyContains || test == FullyContainedBy = test |
53 | | normSq (c - boxC) > (l + r)^2 = NoCollision | 58 | | normSq (c - boxC) > (l + r)^2 = NoCollision |
54 | | otherwise = Collision | 59 | | otherwise = Collision |
55 | where | 60 | where |
56 | test = aabb `collideBox` aabbFromCircle sphere | 61 | test = aabb `collideBox` aabbFromCircle circle |
57 | boxC = min + (max-min)/2 | 62 | boxC = min + (max-min)/2 |
58 | l = norm $ min + (vec2 (x boxC) (y min)) - min | 63 | l = norm $ min + (vec2 (x boxC) (y min)) - min |
59 | 64 | ||
@@ -64,7 +69,7 @@ instance Collisionable AABB where | |||
64 | 69 | ||
65 | instance Collisionable Circle where | 70 | instance Collisionable Circle where |
66 | 71 | ||
67 | collideBox box sphere = case collideCircle sphere box of | 72 | collideBox box circle = case collideCircle circle box of |
68 | FullyContains -> FullyContainedBy | 73 | FullyContains -> FullyContainedBy |
69 | FullyContainedBy -> FullyContains | 74 | FullyContainedBy -> FullyContains |
70 | x -> x | 75 | x -> x |
@@ -117,7 +122,7 @@ aabbPoints (AABB min max) = [p1,p2,p3,p4,p5,p6,p7,p8] | |||
117 | data Collisioner | 122 | data Collisioner |
118 | -- | An axis-aligned bounding box. | 123 | -- | An axis-aligned bounding box. |
119 | = AABBCol {-# UNPACK #-} !AABB | 124 | = AABBCol {-# UNPACK #-} !AABB |
120 | -- | A bounding sphere. | 125 | -- | A bounding circle. |
121 | | CircleCol {-# UNPACK #-} !Circle | 126 | | CircleCol {-# UNPACK #-} !Circle |
122 | 127 | ||
123 | 128 | ||
@@ -127,18 +132,18 @@ aabbCollisioner = AABBCol | |||
127 | 132 | ||
128 | 133 | ||
129 | -- | Create a collisioner from the specified circle. | 134 | -- | Create a collisioner from the specified circle. |
130 | sphereCollisioner :: Circle -> Collisioner | 135 | circleCollisioner :: Circle -> Collisioner |
131 | sphereCollisioner = CircleCol | 136 | circleCollisioner = CircleCol |
137 | |||
138 | |||
139 | -- | Create the minimal AABB collisioner fully containing the specified circle. | ||
140 | boxFromCircle :: Circle -> Collisioner | ||
141 | boxFromCircle = AABBCol . aabbFromCircle | ||
132 | 142 | ||
133 | 143 | ||
134 | -- | Create the minimal AABB fully containing the specified collisioners. | 144 | -- | Create the minimal AABB fully containing the specified collisioners. |
135 | buildAABB :: [Collisioner] -> AABB | 145 | buildAABB :: [Collisioner] -> AABB |
136 | buildAABB cols = aabb $ generatePoints cols | 146 | buildAABB cols = aabb $ generatePoints cols |
137 | |||
138 | |||
139 | -- | Create the minimal AABB collisioner fully containing the specified circle. | ||
140 | boxFromSphere :: Circle -> Collisioner | ||
141 | boxFromSphere = AABBCol . aabbFromCircle | ||
142 | 147 | ||
143 | 148 | ||
144 | generatePoints :: [Collisioner] -> [Vector2] | 149 | generatePoints :: [Collisioner] -> [Vector2] |
@@ -161,6 +166,22 @@ generatePoints = foldr generate [] | |||
161 | p2 = c - unitx * (vec2 r r) | 166 | p2 = c - unitx * (vec2 r r) |
162 | p3 = c + unity * (vec2 r r) | 167 | p3 = c + unity * (vec2 r r) |
163 | p4 = c - unity * (vec2 r r) | 168 | p4 = c - unity * (vec2 r r) |
169 | |||
170 | |||
171 | -- | Compute collisioners in view space from the given 3D AABB. | ||
172 | mkCols :: M4.Matrix4 -- ^ Modelview matrix | ||
173 | -> Box | ||
174 | -> [Collisioner] | ||
175 | mkCols modelview (Box (Vec3 xmin ymin zmin) (Vec3 xmax ymax zmax)) = | ||
176 | let | ||
177 | toVec2 v = vec2 (V3.x v) (V3.y v) | ||
178 | p1 = toVec2 $ modelview `M4.mulp` V3.vec3 xmin ymin zmax | ||
179 | p2 = toVec2 $ modelview `M4.mulp` V3.vec3 xmax ymin zmin | ||
180 | p3 = toVec2 $ modelview `M4.mulp` V3.vec3 xmax ymax zmin | ||
181 | col1 = AABBCol $ AABB p1 p2 | ||
182 | col2 = AABBCol $ AABB p1 p3 | ||
183 | in | ||
184 | [col1, col2] | ||
164 | 185 | ||
165 | 186 | ||
166 | -- | Collide the given collisioners. | 187 | -- | Collide the given collisioners. |
diff --git a/Spear/Render/AnimatedModel.hs b/Spear/Render/AnimatedModel.hs index 3fe12fd..ae86cfe 100644 --- a/Spear/Render/AnimatedModel.hs +++ b/Spear/Render/AnimatedModel.hs | |||
@@ -23,16 +23,21 @@ module Spear.Render.AnimatedModel | |||
23 | -- * Rendering | 23 | -- * Rendering |
24 | , bind | 24 | , bind |
25 | , render | 25 | , render |
26 | -- * Collisions | ||
27 | , mkColsFromAnimated | ||
26 | ) | 28 | ) |
27 | where | 29 | where |
28 | 30 | ||
29 | 31 | ||
30 | import Spear.Assets.Model | 32 | import Spear.Assets.Model |
31 | import Spear.Render.Model | 33 | import Spear.Collision |
32 | import Spear.GLSL | 34 | import Spear.GLSL |
33 | import Spear.Math.AABB | 35 | import Spear.Math.AABB |
36 | import Spear.Math.Matrix4 (Matrix4) | ||
34 | import Spear.Math.Vector2 (vec2) | 37 | import Spear.Math.Vector2 (vec2) |
38 | import Spear.Math.Vector3 (vec3, x, y, z, scale) | ||
35 | import Spear.Render.Material | 39 | import Spear.Render.Material |
40 | import Spear.Render.Model | ||
36 | import Spear.Render.Program | 41 | import Spear.Render.Program |
37 | import Spear.Setup as Setup | 42 | import Spear.Setup as Setup |
38 | 43 | ||
@@ -230,3 +235,26 @@ render uniforms (AnimatedModelRenderer model _ _ _ curFrame fp _) = | |||
230 | glUniform1f (shiLoc uniforms) $ unsafeCoerce shi | 235 | glUniform1f (shiLoc uniforms) $ unsafeCoerce shi |
231 | glUniform1f (fpLoc uniforms) (unsafeCoerce fp) | 236 | glUniform1f (fpLoc uniforms) (unsafeCoerce fp) |
232 | drawArrays gl_TRIANGLES (n*curFrame) n | 237 | drawArrays gl_TRIANGLES (n*curFrame) n |
238 | |||
239 | |||
240 | -- | Compute collisioners in 2d virtual space. | ||
241 | mkColsFromAnimated | ||
242 | :: Int -- ^ Source frame | ||
243 | -> Int -- ^ Dest frame | ||
244 | -> Float -- ^ Frame progress | ||
245 | -> Matrix4 -- ^ Modelview matrix | ||
246 | -> AnimatedModelResource | ||
247 | -> [Collisioner] | ||
248 | mkColsFromAnimated f1 f2 fp modelview modelRes = | ||
249 | let | ||
250 | (Box (Vec3 xmin1 ymin1 zmin1) (Vec3 xmax1 ymax1 zmax1)) = box f1 modelRes | ||
251 | (Box (Vec3 xmin2 ymin2 zmin2) (Vec3 xmax2 ymax2 zmax2)) = box f2 modelRes | ||
252 | min1 = vec3 xmin1 ymin1 zmin1 | ||
253 | max1 = vec3 xmax1 ymax1 zmax1 | ||
254 | min2 = vec3 xmin2 ymin2 zmin2 | ||
255 | max2 = vec3 xmax2 ymax2 zmax2 | ||
256 | min = min1 + scale fp (min2 - min1) | ||
257 | max = max1 + scale fp (max2 - max1) | ||
258 | in | ||
259 | mkCols modelview | ||
260 | $ Box (Vec3 (x min) (y min) (z min)) (Vec3 (x max) (y max) (z max)) | ||
diff --git a/Spear/Render/StaticModel.hs b/Spear/Render/StaticModel.hs index 31acaa2..b4ad20e 100644 --- a/Spear/Render/StaticModel.hs +++ b/Spear/Render/StaticModel.hs | |||
@@ -13,16 +13,20 @@ module Spear.Render.StaticModel | |||
13 | -- * Rendering | 13 | -- * Rendering |
14 | , bind | 14 | , bind |
15 | , render | 15 | , render |
16 | -- * Collision | ||
17 | , mkColsFromStatic | ||
16 | ) | 18 | ) |
17 | where | 19 | where |
18 | 20 | ||
19 | 21 | ||
20 | import Spear.Assets.Model | 22 | import Spear.Assets.Model |
21 | import Spear.Render.Model | 23 | import Spear.Collision |
22 | import Spear.GLSL | 24 | import Spear.GLSL |
23 | import Spear.Math.AABB | 25 | import Spear.Math.AABB |
26 | import Spear.Math.Matrix4 (Matrix4) | ||
24 | import Spear.Math.Vector2 (vec2) | 27 | import Spear.Math.Vector2 (vec2) |
25 | import Spear.Render.Material | 28 | import Spear.Render.Material |
29 | import Spear.Render.Model | ||
26 | import Spear.Render.Program | 30 | import Spear.Render.Program |
27 | import Spear.Setup as Setup | 31 | import Spear.Setup as Setup |
28 | 32 | ||
@@ -142,3 +146,11 @@ render uniforms (StaticModelRenderer model) = | |||
142 | uniformVec4 (ksLoc uniforms) ks | 146 | uniformVec4 (ksLoc uniforms) ks |
143 | glUniform1f (shiLoc uniforms) $ unsafeCoerce shi | 147 | glUniform1f (shiLoc uniforms) $ unsafeCoerce shi |
144 | drawArrays gl_TRIANGLES 0 $ nVertices model | 148 | drawArrays gl_TRIANGLES 0 $ nVertices model |
149 | |||
150 | |||
151 | -- | Compute collisioners in 2d virtual space. | ||
152 | mkColsFromStatic | ||
153 | :: Matrix4 -- ^ Modelview matrix | ||
154 | -> StaticModelResource | ||
155 | -> [Collisioner] | ||
156 | mkColsFromStatic modelview modelRes = mkCols modelview (box 0 modelRes) | ||