aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Sunet <jeannekamikaze@gmail.com>2012-08-06 13:22:23 +0200
committerMarc Sunet <jeannekamikaze@gmail.com>2012-08-06 13:22:23 +0200
commit20b09101a88f0e2ea102df3e484ff215177cb290 (patch)
tree0e8974fefee1a5c110c032a3cdc0fc1e72a8440b
parent5ce7918bc35c2bad9fad75cd7eacd9ff181fb43b (diff)
Tweaked collision API.
· Collisioner is now an open type. · Renamed Collisioner constructors.
-rw-r--r--Spear/Collision/Collision.hs1
-rw-r--r--Spear/Collision/Collisioner.hs39
2 files changed, 18 insertions, 22 deletions
diff --git a/Spear/Collision/Collision.hs b/Spear/Collision/Collision.hs
index 50be0d7..d59cbc2 100644
--- a/Spear/Collision/Collision.hs
+++ b/Spear/Collision/Collision.hs
@@ -1,7 +1,6 @@
1module Spear.Collision.Collision 1module Spear.Collision.Collision
2( 2(
3 Collisionable(..) 3 Collisionable(..)
4, collidePlane
5, aabbFromSphere 4, aabbFromSphere
6) 5)
7where 6where
diff --git a/Spear/Collision/Collisioner.hs b/Spear/Collision/Collisioner.hs
index c0194bd..94a0d63 100644
--- a/Spear/Collision/Collisioner.hs
+++ b/Spear/Collision/Collisioner.hs
@@ -1,6 +1,6 @@
1module Spear.Collision.Collisioner 1module Spear.Collision.Collisioner
2( 2(
3 Collisioner 3 Collisioner(..)
4, CollisionType(..) 4, CollisionType(..)
5, aabbCollisioner 5, aabbCollisioner
6, sphereCollisioner 6, sphereCollisioner
@@ -11,47 +11,44 @@ where
11 11
12 12
13import Spear.Math.Vector3 as Vector 13import Spear.Math.Vector3 as Vector
14import qualified Spear.Collision.AABB as Box 14import Spear.Collision.AABB as Box
15import qualified Spear.Collision.Sphere as Sphere 15import Spear.Collision.Sphere as Sphere
16import Spear.Collision.Collision as C 16import Spear.Collision.Collision as C
17import Spear.Collision.Types 17import Spear.Collision.Types
18 18
19 19
20-- | A collisioner component. 20-- | A collisioner component.
21-- Wraps collision primitives so that one can collide them without being aware of
22-- the underlying type.
23data Collisioner 21data Collisioner
24 -- | An axis-aligned bounding box. 22 -- | An axis-aligned bounding box.
25 = AABB {getBox :: !(Box.AABB)} 23 = AABBCol { getBox :: !AABB }
26 -- | A bounding sphere. 24 -- | A bounding sphere.
27 | Sphere {getSphere :: !(Sphere.Sphere) 25 | SphereCol { getSphere :: !Sphere }
28 }
29 26
30 27
31-- | Create a 'Collisioner' from the specified 'AABB'. 28-- | Create a 'Collisioner' from the specified 'AABB'.
32aabbCollisioner :: Box.AABB -> Collisioner 29aabbCollisioner :: AABB -> Collisioner
33aabbCollisioner = AABB 30aabbCollisioner = AABBCol
34 31
35 32
36-- | Create a 'Collisioner' from the specified 'BSphere'. 33-- | Create a 'Collisioner' from the specified 'BSphere'.
37sphereCollisioner :: Sphere.Sphere -> Collisioner 34sphereCollisioner :: Sphere -> Collisioner
38sphereCollisioner = Sphere 35sphereCollisioner = SphereCol
39 36
40 37
41-- | Create the minimal 'AABB' fully containing the specified collisioners. 38-- | Create the minimal 'AABB' fully containing the specified collisioners.
42buildAABB :: [Collisioner] -> Box.AABB 39buildAABB :: [Collisioner] -> AABB
43buildAABB cols = Box.aabb $ Spear.Collision.Collisioner.generatePoints cols 40buildAABB cols = aabb $ Spear.Collision.Collisioner.generatePoints cols
44 41
45 42
46-- | Create the minimal 'AABB' collisioner fully containing the specified 'BSphere'. 43-- | Create the minimal 'AABB' collisioner fully containing the specified 'BSphere'.
47boxFromSphere :: Sphere.Sphere -> Collisioner 44boxFromSphere :: Sphere.Sphere -> Collisioner
48boxFromSphere = AABB . aabbFromSphere 45boxFromSphere = AABBCol . aabbFromSphere
49 46
50 47
51generatePoints :: [Collisioner] -> [Vector3] 48generatePoints :: [Collisioner] -> [Vector3]
52generatePoints = foldr generate [] 49generatePoints = foldr generate []
53 where 50 where
54 generate (AABB (Box.AABB min max)) acc = p1:p2:p3:p4:p5:p6:p7:p8:acc 51 generate (AABBCol (AABB min max)) acc = p1:p2:p3:p4:p5:p6:p7:p8:acc
55 where 52 where
56 p1 = vec3 (x min) (y min) (z min) 53 p1 = vec3 (x min) (y min) (z min)
57 p2 = vec3 (x min) (y min) (z max) 54 p2 = vec3 (x min) (y min) (z max)
@@ -62,7 +59,7 @@ generatePoints = foldr generate []
62 p7 = vec3 (x max) (y max) (z min) 59 p7 = vec3 (x max) (y max) (z min)
63 p8 = vec3 (x max) (y max) (z max) 60 p8 = vec3 (x max) (y max) (z max)
64 61
65 generate (Sphere (Sphere.Sphere c r)) acc = p1:p2:p3:p4:p5:p6:acc 62 generate (SphereCol (Sphere c r)) acc = p1:p2:p3:p4:p5:p6:acc
66 where 63 where
67 p1 = c + unitX * (vec3 r r r) 64 p1 = c + unitX * (vec3 r r r)
68 p2 = c - unitX * (vec3 r r r) 65 p2 = c - unitX * (vec3 r r r)
@@ -74,7 +71,7 @@ generatePoints = foldr generate []
74 71
75-- | Collide the given collisioners. 72-- | Collide the given collisioners.
76collide :: Collisioner -> Collisioner -> CollisionType 73collide :: Collisioner -> Collisioner -> CollisionType
77collide (AABB box1) (AABB box2) = collideBox box1 box2 74collide (AABBCol box1) (AABBCol box2) = collideBox box1 box2
78collide (Sphere s1) (Sphere s2) = collideSphere s1 s2 75collide (SphereCol s1) (SphereCol s2) = collideSphere s1 s2
79collide (AABB box) (Sphere sphere) = collideBox box sphere 76collide (AABBCol box) (SphereCol sphere) = collideBox box sphere
80collide (Sphere sphere) (AABB box) = collideSphere sphere box 77collide (SphereCol sphere) (AABBCol box) = collideSphere sphere box