diff options
-rw-r--r-- | Spear/Collision/Collision.hs | 1 | ||||
-rw-r--r-- | Spear/Collision/Collisioner.hs | 39 |
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 @@ | |||
1 | module Spear.Collision.Collision | 1 | module Spear.Collision.Collision |
2 | ( | 2 | ( |
3 | Collisionable(..) | 3 | Collisionable(..) |
4 | , collidePlane | ||
5 | , aabbFromSphere | 4 | , aabbFromSphere |
6 | ) | 5 | ) |
7 | where | 6 | where |
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 @@ | |||
1 | module Spear.Collision.Collisioner | 1 | module 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 | ||
13 | import Spear.Math.Vector3 as Vector | 13 | import Spear.Math.Vector3 as Vector |
14 | import qualified Spear.Collision.AABB as Box | 14 | import Spear.Collision.AABB as Box |
15 | import qualified Spear.Collision.Sphere as Sphere | 15 | import Spear.Collision.Sphere as Sphere |
16 | import Spear.Collision.Collision as C | 16 | import Spear.Collision.Collision as C |
17 | import Spear.Collision.Types | 17 | import 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. | ||
23 | data Collisioner | 21 | data 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'. |
32 | aabbCollisioner :: Box.AABB -> Collisioner | 29 | aabbCollisioner :: AABB -> Collisioner |
33 | aabbCollisioner = AABB | 30 | aabbCollisioner = AABBCol |
34 | 31 | ||
35 | 32 | ||
36 | -- | Create a 'Collisioner' from the specified 'BSphere'. | 33 | -- | Create a 'Collisioner' from the specified 'BSphere'. |
37 | sphereCollisioner :: Sphere.Sphere -> Collisioner | 34 | sphereCollisioner :: Sphere -> Collisioner |
38 | sphereCollisioner = Sphere | 35 | sphereCollisioner = 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. |
42 | buildAABB :: [Collisioner] -> Box.AABB | 39 | buildAABB :: [Collisioner] -> AABB |
43 | buildAABB cols = Box.aabb $ Spear.Collision.Collisioner.generatePoints cols | 40 | buildAABB 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'. |
47 | boxFromSphere :: Sphere.Sphere -> Collisioner | 44 | boxFromSphere :: Sphere.Sphere -> Collisioner |
48 | boxFromSphere = AABB . aabbFromSphere | 45 | boxFromSphere = AABBCol . aabbFromSphere |
49 | 46 | ||
50 | 47 | ||
51 | generatePoints :: [Collisioner] -> [Vector3] | 48 | generatePoints :: [Collisioner] -> [Vector3] |
52 | generatePoints = foldr generate [] | 49 | generatePoints = 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. |
76 | collide :: Collisioner -> Collisioner -> CollisionType | 73 | collide :: Collisioner -> Collisioner -> CollisionType |
77 | collide (AABB box1) (AABB box2) = collideBox box1 box2 | 74 | collide (AABBCol box1) (AABBCol box2) = collideBox box1 box2 |
78 | collide (Sphere s1) (Sphere s2) = collideSphere s1 s2 | 75 | collide (SphereCol s1) (SphereCol s2) = collideSphere s1 s2 |
79 | collide (AABB box) (Sphere sphere) = collideBox box sphere | 76 | collide (AABBCol box) (SphereCol sphere) = collideBox box sphere |
80 | collide (Sphere sphere) (AABB box) = collideSphere sphere box | 77 | collide (SphereCol sphere) (AABBCol box) = collideSphere sphere box |