aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeanne-Kamikaze <jeannekamikaze@gmail.com>2013-02-21 19:24:47 +0100
committerJeanne-Kamikaze <jeannekamikaze@gmail.com>2013-02-21 19:24:47 +0100
commitfc802a58cff52083327b343ba04c9e67331d9d5a (patch)
tree710492e605c4567fb061fe53bd99cceb8ca02bd8
parent44a2fc13f57454ed54223d028e287e053609971a (diff)
Added Vector and Vector.Class modules
-rw-r--r--Spear.cabal6
-rw-r--r--Spear/Collision.hs123
-rw-r--r--Spear/GLSL.hs22
-rw-r--r--Spear/Math/AABB.hs2
-rw-r--r--Spear/Math/Camera.hs2
-rw-r--r--Spear/Math/Circle.hs2
-rw-r--r--Spear/Math/Entity.hs2
-rw-r--r--Spear/Math/Matrix3.hs48
-rw-r--r--Spear/Math/Matrix4.hs97
-rw-r--r--Spear/Math/MatrixUtils.hs49
-rw-r--r--Spear/Math/Plane.hs2
-rw-r--r--Spear/Math/Quad.hs2
-rw-r--r--Spear/Math/QuadTree.hs2
-rw-r--r--Spear/Math/Quaternion.hs43
-rw-r--r--Spear/Math/Ray.hs2
-rw-r--r--Spear/Math/Segment.hs2
-rw-r--r--Spear/Math/Spatial2.hs2
-rw-r--r--Spear/Math/Spatial3.hs4
-rw-r--r--Spear/Math/Triangle.hs2
-rw-r--r--Spear/Math/Utils.hs11
-rw-r--r--Spear/Math/Vector.hs13
-rw-r--r--Spear/Math/Vector/Class.hs43
-rw-r--r--Spear/Math/Vector/Vector2.hs (renamed from Spear/Math/Vector2.hs)99
-rw-r--r--Spear/Math/Vector/Vector3.hs (renamed from Spear/Math/Vector3.hs)104
-rw-r--r--Spear/Math/Vector/Vector4.hs (renamed from Spear/Math/Vector4.hs)118
-rw-r--r--Spear/Physics/Rigid.hs22
-rw-r--r--Spear/Physics/Types.hs2
-rw-r--r--Spear/Render/AnimatedModel.hs3
-rw-r--r--Spear/Render/Material.hs2
-rw-r--r--Spear/Render/StaticModel.hs2
-rw-r--r--Spear/Scene/GameObject.hs21
-rw-r--r--Spear/Scene/Light.hs3
-rw-r--r--Spear/Scene/Loader.hs26
-rw-r--r--Spear/Scene/SceneResources.hs2
34 files changed, 430 insertions, 455 deletions
diff --git a/Spear.cabal b/Spear.cabal
index 051d955..f7d0536 100644
--- a/Spear.cabal
+++ b/Spear.cabal
@@ -22,13 +22,15 @@ library
22 Spear.Math.AABB Spear.Math.Circle Spear.Math.Triangle Spear.Game 22 Spear.Math.AABB Spear.Math.Circle Spear.Math.Triangle Spear.Game
23 Spear.GLSL Spear.Math.Camera Spear.Math.Entity Spear.Math.Matrix3 23 Spear.GLSL Spear.Math.Camera Spear.Math.Entity Spear.Math.Matrix3
24 Spear.Math.Matrix4 Spear.Math.MatrixUtils Spear.Math.Plane 24 Spear.Math.Matrix4 Spear.Math.MatrixUtils Spear.Math.Plane
25 Spear.Math.Quaternion Spear.Math.Vector3 Spear.Math.Vector4 25 Spear.Math.Quaternion Spear.Math.Vector Spear.Math.Vector.Class
26 Spear.Math.Vector.Vector3 Spear.Math.Vector.Vector4
27 Spear.Math.Vector.Vector2
26 Spear.Physics Spear.Physics.Rigid Spear.Render.AnimatedModel 28 Spear.Physics Spear.Physics.Rigid Spear.Render.AnimatedModel
27 Spear.Render.Material Spear.Render.Model Spear.Render.Program 29 Spear.Render.Material Spear.Render.Model Spear.Render.Program
28 Spear.Render.StaticModel Spear.Scene.Graph Spear.Scene.Light 30 Spear.Render.StaticModel Spear.Scene.Graph Spear.Scene.Light
29 Spear.Scene.Loader Spear.Scene.Scene Spear.Scene.SceneResources 31 Spear.Scene.Loader Spear.Scene.Scene Spear.Scene.SceneResources
30 Spear.Setup Spear.Sys.Timer Spear.Sys.Store Spear.Sys.Store.ID 32 Spear.Setup Spear.Sys.Timer Spear.Sys.Store Spear.Sys.Store.ID
31 Spear.Math.Vector2 Spear.Math.Quad Spear.Math.Ray 33 Spear.Math.Quad Spear.Math.Ray
32 Spear.Math.Segment Spear.Math.Utils Spear.Math.Spatial2 34 Spear.Math.Segment Spear.Math.Utils Spear.Math.Spatial2
33 Spear.Math.Spatial3 35 Spear.Math.Spatial3
34 exposed: True 36 exposed: True
diff --git a/Spear/Collision.hs b/Spear/Collision.hs
index 8c334c1..3b80696 100644
--- a/Spear/Collision.hs
+++ b/Spear/Collision.hs
@@ -5,13 +5,13 @@ module Spear.Collision
5, Collisionable(..) 5, Collisionable(..)
6 -- * Collisioners 6 -- * Collisioners
7, Collisioner(..) 7, Collisioner(..)
8 -- ** Construction 8 -- ** Construction
9, aabbCollisioner 9, aabbCollisioner
10, circleCollisioner 10, circleCollisioner
11, boxFromCircle 11, boxFromCircle
12, buildAABB 12, buildAABB
13, mkCols 13, mkCols
14 -- ** Collision test 14 -- ** Collision test
15, collide 15, collide
16 -- ** Manipulation 16 -- ** Manipulation
17, move 17, move
@@ -26,8 +26,7 @@ import Spear.Math.AABB
26import Spear.Math.Circle 26import Spear.Math.Circle
27import qualified Spear.Math.Matrix4 as M4 27import qualified Spear.Math.Matrix4 as M4
28import Spear.Math.Plane 28import Spear.Math.Plane
29import Spear.Math.Vector2 29import Spear.Math.Vector
30import qualified Spear.Math.Vector3 as V3
31 30
32 31
33-- | Encodes several collision situations. 32-- | Encodes several collision situations.
@@ -118,54 +117,54 @@ aabbPoints (AABB min max) = [p1,p2,p3,p4,p5,p6,p7,p8]
118 p8 = vec2 (x max) (y max) 117 p8 = vec2 (x max) (y max)
119 118
120 119
121-- | A collisioner component. 120-- | A collisioner component.
122data Collisioner 121data Collisioner
123 -- | An axis-aligned bounding box. 122 -- | An axis-aligned bounding box.
124 = AABBCol {-# UNPACK #-} !AABB 123 = AABBCol {-# UNPACK #-} !AABB
125 -- | A bounding circle. 124 -- | A bounding circle.
126 | CircleCol {-# UNPACK #-} !Circle 125 | CircleCol {-# UNPACK #-} !Circle
127 126
128 127
129-- | Create a collisioner from the specified box. 128-- | Create a collisioner from the specified box.
130aabbCollisioner :: AABB -> Collisioner 129aabbCollisioner :: AABB -> Collisioner
131aabbCollisioner = AABBCol 130aabbCollisioner = AABBCol
132 131
133 132
134-- | Create a collisioner from the specified circle. 133-- | Create a collisioner from the specified circle.
135circleCollisioner :: Circle -> Collisioner 134circleCollisioner :: Circle -> Collisioner
136circleCollisioner = CircleCol 135circleCollisioner = CircleCol
137 136
138 137
139-- | Create the minimal AABB collisioner fully containing the specified circle. 138-- | Create the minimal AABB collisioner fully containing the specified circle.
140boxFromCircle :: Circle -> Collisioner 139boxFromCircle :: Circle -> Collisioner
141boxFromCircle = AABBCol . aabbFromCircle 140boxFromCircle = AABBCol . aabbFromCircle
142 141
143 142
144-- | Create the minimal AABB fully containing the specified collisioners. 143-- | Create the minimal AABB fully containing the specified collisioners.
145buildAABB :: [Collisioner] -> AABB 144buildAABB :: [Collisioner] -> AABB
146buildAABB cols = aabb $ generatePoints cols 145buildAABB cols = aabb $ generatePoints cols
147 146
148 147
149generatePoints :: [Collisioner] -> [Vector2] 148generatePoints :: [Collisioner] -> [Vector2]
150generatePoints = foldr generate [] 149generatePoints = foldr generate []
151 where 150 where
152 generate (AABBCol (AABB min max)) acc = p1:p2:p3:p4:p5:p6:p7:p8:acc 151 generate (AABBCol (AABB pmin pmax)) acc = p1:p2:p3:p4:p5:p6:p7:p8:acc
153 where 152 where
154 p1 = vec2 (x min) (y min) 153 p1 = vec2 (x pmin) (y pmin)
155 p2 = vec2 (x min) (y min) 154 p2 = vec2 (x pmin) (y pmin)
156 p3 = vec2 (x min) (y max) 155 p3 = vec2 (x pmin) (y pmax)
157 p4 = vec2 (x min) (y max) 156 p4 = vec2 (x pmin) (y pmax)
158 p5 = vec2 (x max) (y min) 157 p5 = vec2 (x pmax) (y pmin)
159 p6 = vec2 (x max) (y min) 158 p6 = vec2 (x pmax) (y pmin)
160 p7 = vec2 (x max) (y max) 159 p7 = vec2 (x pmax) (y pmax)
161 p8 = vec2 (x max) (y max) 160 p8 = vec2 (x pmax) (y pmax)
162 161
163 generate (CircleCol (Circle c r)) acc = p1:p2:p3:p4:acc 162 generate (CircleCol (Circle c r)) acc = p1:p2:p3:p4:acc
164 where 163 where
165 p1 = c + unitx * (vec2 r r) 164 p1 = c + unitx2 * (vec2 r r)
166 p2 = c - unitx * (vec2 r r) 165 p2 = c - unitx2 * (vec2 r r)
167 p3 = c + unity * (vec2 r r) 166 p3 = c + unity2 * (vec2 r r)
168 p4 = c - unity * (vec2 r r) 167 p4 = c - unity2 * (vec2 r r)
169 168
170 169
171-- | Compute AABB collisioners in view space from the given 3D AABB. 170-- | Compute AABB collisioners in view space from the given 3D AABB.
@@ -174,22 +173,22 @@ mkCols :: M4.Matrix4 -- ^ Modelview matrix
174 -> [Collisioner] 173 -> [Collisioner]
175mkCols modelview (Box (Vec3 xmin ymin zmin) (Vec3 xmax ymax zmax)) = 174mkCols modelview (Box (Vec3 xmin ymin zmin) (Vec3 xmax ymax zmax)) =
176 let 175 let
177 toVec2 v = vec2 (V3.x v) (V3.y v) 176 toVec2 v = vec2 (x v) (y v)
178 p1 = toVec2 $ modelview `M4.mulp` V3.vec3 xmin ymin zmax 177 p1 = toVec2 $ modelview `M4.mulp` vec3 xmin ymin zmax
179 p2 = toVec2 $ modelview `M4.mulp` V3.vec3 xmax ymin zmin 178 p2 = toVec2 $ modelview `M4.mulp` vec3 xmax ymin zmin
180 p3 = toVec2 $ modelview `M4.mulp` V3.vec3 xmax ymax zmin 179 p3 = toVec2 $ modelview `M4.mulp` vec3 xmax ymax zmin
181 col1 = AABBCol $ AABB p1 p2 180 col1 = AABBCol $ AABB p1 p2
182 col2 = AABBCol $ AABB p1 p3 181 col2 = AABBCol $ AABB p1 p3
183 in 182 in
184 [col1, col2] 183 [col1, col2]
185 184
186 185
187-- | Collide the given collisioners. 186-- | Collide the given collisioners.
188collide :: Collisioner -> Collisioner -> CollisionType 187collide :: Collisioner -> Collisioner -> CollisionType
189collide (AABBCol box1) (AABBCol box2) = collideBox box1 box2 188collide (AABBCol box1) (AABBCol box2) = collideBox box1 box2
190collide (CircleCol s1) (CircleCol s2) = collideCircle s1 s2 189collide (CircleCol s1) (CircleCol s2) = collideCircle s1 s2
191collide (AABBCol box) (CircleCol circle) = collideBox box circle 190collide (AABBCol box) (CircleCol circle) = collideBox box circle
192collide (CircleCol circle) (AABBCol box) = collideCircle circle box 191collide (CircleCol circle) (AABBCol box) = collideCircle circle box
193 192
194 193
195-- | Move the collisioner. 194-- | Move the collisioner.
diff --git a/Spear/GLSL.hs b/Spear/GLSL.hs
index c00582d..2947515 100644
--- a/Spear/GLSL.hs
+++ b/Spear/GLSL.hs
@@ -93,9 +93,7 @@ where
93import Spear.Assets.Image 93import Spear.Assets.Image
94import Spear.Math.Matrix3 (Matrix3) 94import Spear.Math.Matrix3 (Matrix3)
95import Spear.Math.Matrix4 (Matrix4) 95import Spear.Math.Matrix4 (Matrix4)
96import Spear.Math.Vector2 as V2 96import Spear.Math.Vector
97import Spear.Math.Vector3 as V3
98import Spear.Math.Vector4 as V4
99import Spear.Setup 97import Spear.Setup
100 98
101import Control.Monad 99import Control.Monad
@@ -371,25 +369,25 @@ readSource' file = do
371-- | Load a 2D vector. 369-- | Load a 2D vector.
372uniformVec2 :: GLint -> Vector2 -> IO () 370uniformVec2 :: GLint -> Vector2 -> IO ()
373uniformVec2 loc v = glUniform2f loc x' y' 371uniformVec2 loc v = glUniform2f loc x' y'
374 where x' = unsafeCoerce $ V2.x v 372 where x' = unsafeCoerce $ x v
375 y' = unsafeCoerce $ V2.y v 373 y' = unsafeCoerce $ y v
376 374
377 375
378-- | Load a 3D vector. 376-- | Load a 3D vector.
379uniformVec3 :: GLint -> Vector3 -> IO () 377uniformVec3 :: GLint -> Vector3 -> IO ()
380uniformVec3 loc v = glUniform3f loc x' y' z' 378uniformVec3 loc v = glUniform3f loc x' y' z'
381 where x' = unsafeCoerce $ V3.x v 379 where x' = unsafeCoerce $ x v
382 y' = unsafeCoerce $ V3.y v 380 y' = unsafeCoerce $ y v
383 z' = unsafeCoerce $ V3.z v 381 z' = unsafeCoerce $ z v
384 382
385 383
386-- | Load a 4D vector. 384-- | Load a 4D vector.
387uniformVec4 :: GLint -> Vector4 -> IO () 385uniformVec4 :: GLint -> Vector4 -> IO ()
388uniformVec4 loc v = glUniform4f loc x' y' z' w' 386uniformVec4 loc v = glUniform4f loc x' y' z' w'
389 where x' = unsafeCoerce $ V4.x v 387 where x' = unsafeCoerce $ x v
390 y' = unsafeCoerce $ V4.y v 388 y' = unsafeCoerce $ y v
391 z' = unsafeCoerce $ V4.z v 389 z' = unsafeCoerce $ z v
392 w' = unsafeCoerce $ V4.w v 390 w' = unsafeCoerce $ w v
393 391
394 392
395-- | Load a 3x3 matrix. 393-- | Load a 3x3 matrix.
diff --git a/Spear/Math/AABB.hs b/Spear/Math/AABB.hs
index cd945a6..0dacfa4 100644
--- a/Spear/Math/AABB.hs
+++ b/Spear/Math/AABB.hs
@@ -7,7 +7,7 @@ module Spear.Math.AABB
7where 7where
8 8
9 9
10import Spear.Math.Vector2 10import Spear.Math.Vector
11 11
12 12
13-- | An axis-aligned bounding box. 13-- | An axis-aligned bounding box.
diff --git a/Spear/Math/Camera.hs b/Spear/Math/Camera.hs
index acde7d0..e22f3c2 100644
--- a/Spear/Math/Camera.hs
+++ b/Spear/Math/Camera.hs
@@ -4,7 +4,7 @@ where
4 4
5import qualified Spear.Math.Matrix4 as M 5import qualified Spear.Math.Matrix4 as M
6import qualified Spear.Math.Spatial3 as S 6import qualified Spear.Math.Spatial3 as S
7import Spear.Math.Vector3 7import Spear.Math.Vector
8 8
9 9
10data Camera = Camera 10data Camera = Camera
diff --git a/Spear/Math/Circle.hs b/Spear/Math/Circle.hs
index daaafc5..ab256a4 100644
--- a/Spear/Math/Circle.hs
+++ b/Spear/Math/Circle.hs
@@ -7,7 +7,7 @@ module Spear.Math.Circle
7where 7where
8 8
9 9
10import Spear.Math.Vector2 10import Spear.Math.Vector
11 11
12 12
13-- | A bounding volume. 13-- | A bounding volume.
diff --git a/Spear/Math/Entity.hs b/Spear/Math/Entity.hs
index 2dc6965..4fc3d87 100644
--- a/Spear/Math/Entity.hs
+++ b/Spear/Math/Entity.hs
@@ -7,7 +7,7 @@ where
7 7
8import qualified Spear.Math.Matrix3 as M 8import qualified Spear.Math.Matrix3 as M
9import qualified Spear.Math.Spatial2 as S 9import qualified Spear.Math.Spatial2 as S
10import qualified Spear.Math.Vector2 as V 10import qualified Spear.Math.Vector as V
11 11
12 12
13-- | An entity in 2D space. 13-- | An entity in 2D space.
diff --git a/Spear/Math/Matrix3.hs b/Spear/Math/Matrix3.hs
index d5e46e9..497cb4e 100644
--- a/Spear/Math/Matrix3.hs
+++ b/Spear/Math/Matrix3.hs
@@ -40,8 +40,7 @@ module Spear.Math.Matrix3
40where 40where
41 41
42 42
43import Spear.Math.Vector2 as V2 43import Spear.Math.Vector
44import Spear.Math.Vector3 as V3
45 44
46import Foreign.Storable 45import Foreign.Storable
47 46
@@ -142,9 +141,9 @@ mat3 = Matrix3
142-- | Build a matrix from three vectors in 3D. 141-- | Build a matrix from three vectors in 3D.
143mat3fromVec :: Vector3 -> Vector3 -> Vector3 -> Matrix3 142mat3fromVec :: Vector3 -> Vector3 -> Vector3 -> Matrix3
144mat3fromVec v0 v1 v2 = Matrix3 143mat3fromVec v0 v1 v2 = Matrix3
145 (V3.x v0) (V3.x v1) (V3.x v2) 144 (x v0) (x v1) (x v2)
146 (V3.y v0) (V3.y v1) (V3.y v2) 145 (y v0) (y v1) (y v2)
147 (V3.z v0) (V3.z v1) (V3.z v2) 146 (z v0) (z v1) (z v2)
148 147
149 148
150-- | Build a transformation matrix. 149-- | Build a transformation matrix.
@@ -154,8 +153,8 @@ transform :: Vector2 -- ^ Right vector
154 -> Matrix3 -- ^ Transform 153 -> Matrix3 -- ^ Transform
155 154
156transform r f p = mat3 155transform r f p = mat3
157 (V2.x r) (V2.x f) (V2.x p) 156 (x r) (x f) (x p)
158 (V2.y r) (V2.y f) (V2.y p) 157 (y r) (y f) (y p)
159 0 0 1 158 0 0 1
160 159
161 160
@@ -205,8 +204,8 @@ transl tx ty = mat3
205-- | Create a translation matrix. 204-- | Create a translation matrix.
206translv :: Vector2 -> Matrix3 205translv :: Vector2 -> Matrix3
207translv v = mat3 206translv v = mat3
208 1 0 (V2.x v) 207 1 0 (x v)
209 0 1 (V2.y v) 208 0 1 (y v)
210 0 0 1 209 0 0 1
211 210
212 211
@@ -238,9 +237,9 @@ scalev v = mat3
238 0 sy 0 237 0 sy 0
239 0 0 sz 238 0 0 sz
240 where 239 where
241 sx = V3.x v 240 sx = x v
242 sy = V3.y v 241 sy = y v
243 sz = V3.z v 242 sz = z v
244 243
245 244
246-- | Create an X reflection matrix. 245-- | Create an X reflection matrix.
@@ -279,9 +278,9 @@ transpose m = mat3
279mulp :: Matrix3 -> Vector2 -> Vector2 278mulp :: Matrix3 -> Vector2 -> Vector2
280mulp m v = vec2 x' y' 279mulp m v = vec2 x' y'
281 where 280 where
282 v' = vec3 (V2.x v) (V2.y v) 1 281 v' = vec3 (x v) (y v) 1
283 x' = row0 m `V3.dot` v' 282 x' = row0 m `dot` v'
284 y' = row1 m `V3.dot` v' 283 y' = row1 m `dot` v'
285 284
286 285
287 286
@@ -289,19 +288,19 @@ mulp m v = vec2 x' y'
289muld :: Matrix3 -> Vector2 -> Vector2 288muld :: Matrix3 -> Vector2 -> Vector2
290muld m v = vec2 x' y' 289muld m v = vec2 x' y'
291 where 290 where
292 v' = vec3 (V2.x v) (V2.y v) 0 291 v' = vec3 (x v) (y v) 0
293 x' = row0 m `V3.dot` v' 292 x' = row0 m `dot` v'
294 y' = row1 m `V3.dot` v' 293 y' = row1 m `dot` v'
295 294
296 295
297-- | Transform the given vector in 3D space with the given matrix. 296-- | Transform the given vector in 3D space with the given matrix.
298mul :: Matrix3 -> Vector3 -> Vector3 297mul :: Matrix3 -> Vector3 -> Vector3
299mul m v = vec3 x' y' z' 298mul m v = vec3 x' y' z'
300 where 299 where
301 v' = vec3 (V3.x v) (V3.y v) (V3.z v) 300 v' = vec3 (x v) (y v) (z v)
302 x' = row0 m `V3.dot` v' 301 x' = row0 m `dot` v'
303 y' = row1 m `V3.dot` v' 302 y' = row1 m `dot` v'
304 z' = row2 m `V3.dot` v' 303 z' = row2 m `dot` v'
305 304
306 305
307-- | Zip two 'Matrix3' together with the specified function. 306-- | Zip two 'Matrix3' together with the specified function.
@@ -327,11 +326,10 @@ inverseTransform mat =
327 f = forward mat 326 f = forward mat
328 t = -(position mat) 327 t = -(position mat)
329 in mat3 328 in mat3
330 (V2.x r) (V2.y r) (t `V2.dot` r) 329 (x r) (y r) (t `dot` r)
331 (V2.x f) (V2.y f) (t `V2.dot` f) 330 (x f) (y f) (t `dot` f)
332 0 0 1 331 0 0 1
333 332
334 333
335fromDeg :: (Floating a) => a -> a 334fromDeg :: (Floating a) => a -> a
336fromDeg = (*pi) . (/180) 335fromDeg = (*pi) . (/180)
337
diff --git a/Spear/Math/Matrix4.hs b/Spear/Math/Matrix4.hs
index 41bfadd..1424b28 100644
--- a/Spear/Math/Matrix4.hs
+++ b/Spear/Math/Matrix4.hs
@@ -51,8 +51,7 @@ module Spear.Math.Matrix4
51where 51where
52 52
53 53
54import Spear.Math.Vector3 as V3 54import Spear.Math.Vector
55import Spear.Math.Vector4 as V4
56 55
57import Foreign.Storable 56import Foreign.Storable
58 57
@@ -171,10 +170,10 @@ mat4 = Matrix4
171-- | Build a matrix from four vectors in 4D. 170-- | Build a matrix from four vectors in 4D.
172mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4 171mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4
173mat4fromVec v0 v1 v2 v3 = Matrix4 172mat4fromVec v0 v1 v2 v3 = Matrix4
174 (V4.x v0) (V4.x v1) (V4.x v2) (V4.x v3) 173 (x v0) (x v1) (x v2) (x v3)
175 (V4.y v0) (V4.y v1) (V4.y v2) (V4.y v3) 174 (y v0) (y v1) (y v2) (y v3)
176 (V4.z v0) (V4.z v1) (V4.z v2) (V4.z v3) 175 (z v0) (z v1) (z v2) (z v3)
177 (V4.w v0) (V4.w v1) (V4.w v2) (V4.w v3) 176 (w v0) (w v1) (w v2) (w v3)
178 177
179 178
180-- | Build a transformation 'Matrix4' from the given vectors. 179-- | Build a transformation 'Matrix4' from the given vectors.
@@ -185,9 +184,9 @@ transform :: Vector3 -- ^ Right vector.
185 -> Matrix4 184 -> Matrix4
186 185
187transform right up fwd pos = mat4 186transform right up fwd pos = mat4
188 (V3.x right) (V3.x up) (V3.x fwd) (V3.x pos) 187 (x right) (x up) (x fwd) (x pos)
189 (V3.y right) (V3.y up) (V3.y fwd) (V3.y pos) 188 (y right) (y up) (y fwd) (y pos)
190 (V3.z right) (V3.z up) (V3.z fwd) (V3.z pos) 189 (z right) (z up) (z fwd) (z pos)
191 0 0 0 1 190 0 0 0 1
192 191
193 192
@@ -225,8 +224,8 @@ lookAt :: Vector3 -- ^ Eye position.
225 -> Matrix4 224 -> Matrix4
226 225
227lookAt pos target = 226lookAt pos target =
228 let fwd = V3.normalise $ target - pos 227 let fwd = normalise $ target - pos
229 r = fwd `cross` V3.unity 228 r = fwd `cross` unity3
230 u = r `cross` fwd 229 u = r `cross` fwd
231 in 230 in
232 transform r u (-fwd) pos 231 transform r u (-fwd) pos
@@ -271,9 +270,9 @@ transl x y z = mat4
271-- | Create a translation matrix. 270-- | Create a translation matrix.
272translv :: Vector3 -> Matrix4 271translv :: Vector3 -> Matrix4
273translv v = mat4 272translv v = mat4
274 1 0 0 (V3.x v) 273 1 0 0 (x v)
275 0 1 0 (V3.y v) 274 0 1 0 (y v)
276 0 0 1 (V3.z v) 275 0 0 1 (z v)
277 0 0 0 1 276 0 0 0 1
278 277
279 278
@@ -320,22 +319,22 @@ rotZ angle = mat4
320-- The given angle must be in degrees. 319-- The given angle must be in degrees.
321axisAngle :: Vector3 -> Float -> Matrix4 320axisAngle :: Vector3 -> Float -> Matrix4
322axisAngle v angle = mat4 321axisAngle v angle = mat4
323 (c + omc*x^2) (omc*xy-sz) (omc*xz+sy) 0 322 (c + omc*ax^2) (omc*xy-sz) (omc*xz+sy) 0
324 (omc*xy+sz) (c+omc*y^2) (omc*yz-sx) 0 323 (omc*xy+sz) (c+omc*ay^2) (omc*yz-sx) 0
325 (omc*xz-sy) (omc*yz+sx) (c+omc*z^2) 0 324 (omc*xz-sy) (omc*yz+sx) (c+omc*az^2) 0
326 0 0 0 1 325 0 0 0 1
327 where 326 where
328 x = V3.x v 327 ax = x v
329 y = V3.y v 328 ay = y v
330 z = V3.z v 329 az = z v
331 s = sin . toRAD $ angle 330 s = sin . toRAD $ angle
332 c = cos . toRAD $ angle 331 c = cos . toRAD $ angle
333 xy = x*y 332 xy = ax*ay
334 xz = x*z 333 xz = ax*az
335 yz = y*z 334 yz = ay*az
336 sx = s*x 335 sx = s*ax
337 sy = s*y 336 sy = s*ay
338 sz = s*z 337 sz = s*az
339 omc = 1 - c 338 omc = 1 - c
340 339
341 340
@@ -356,9 +355,9 @@ scalev v = mat4
356 0 0 sz 0 355 0 0 sz 0
357 0 0 0 1 356 0 0 0 1
358 where 357 where
359 sx = V3.x v 358 sx = x v
360 sy = V3.y v 359 sy = y v
361 sz = V3.z v 360 sz = z v
362 361
363 362
364-- | Create an X reflection matrix. 363-- | Create an X reflection matrix.
@@ -430,13 +429,13 @@ planeProj :: Vector3 -- ^ Plane normal
430 -> Vector3 -- ^ Projection direction 429 -> Vector3 -- ^ Projection direction
431 -> Matrix4 430 -> Matrix4
432planeProj n d l = 431planeProj n d l =
433 let c = n `V3.dot` l 432 let c = n `dot` l
434 nx = V3.x n 433 nx = x n
435 ny = V3.y n 434 ny = y n
436 nz = V3.z n 435 nz = z n
437 lx = V3.x l 436 lx = x l
438 ly = V3.y l 437 ly = y l
439 lz = V3.z l 438 lz = z l
440 in mat4 439 in mat4
441 (d + c - nx*lx) (-ny*lx) (-nz*lx) (-lx*d) 440 (d + c - nx*lx) (-ny*lx) (-nz*lx) (-lx*d)
442 (-nx*ly) (d + c - ny*ly) (-nz*ly) (-ly*d) 441 (-nx*ly) (d + c - ny*ly) (-nz*ly) (-ly*d)
@@ -463,9 +462,9 @@ inverseTransform mat =
463 t = position mat 462 t = position mat
464 in 463 in
465 mat4 464 mat4
466 (V3.x r) (V3.y r) (V3.z r) (-t `V3.dot` r) 465 (x r) (y r) (z r) (-t `dot` r)
467 (V3.x u) (V3.y u) (V3.z u) (-t `V3.dot` u) 466 (x u) (y u) (z u) (-t `dot` u)
468 (V3.x f) (V3.y f) (V3.z f) (-t `V3.dot` f) 467 (x f) (y f) (z f) (-t `dot` f)
469 0 0 0 1 468 0 0 0 1
470 469
471 470
@@ -618,10 +617,10 @@ inverse mat =
618mul :: Float -> Matrix4 -> Vector3 -> Vector3 617mul :: Float -> Matrix4 -> Vector3 -> Vector3
619mul w m v = vec3 x' y' z' 618mul w m v = vec3 x' y' z'
620 where 619 where
621 v' = vec4 (V3.x v) (V3.y v) (V3.z v) w 620 v' = vec4 (x v) (y v) (z v) w
622 x' = row0 m `V4.dot` v' 621 x' = row0 m `dot` v'
623 y' = row1 m `V4.dot` v' 622 y' = row1 m `dot` v'
624 z' = row2 m `V4.dot` v' 623 z' = row2 m `dot` v'
625 624
626 625
627-- | Transform the given point vector in 3D space with the given matrix. 626-- | Transform the given point vector in 3D space with the given matrix.
@@ -641,11 +640,11 @@ muld = mul 0
641mul' :: Float -> Matrix4 -> Vector3 -> Vector3 640mul' :: Float -> Matrix4 -> Vector3 -> Vector3
642mul' w m v = vec3 (x'/w') (y'/w') (z'/w') 641mul' w m v = vec3 (x'/w') (y'/w') (z'/w')
643 where 642 where
644 v' = vec4 (V3.x v) (V3.y v) (V3.z v) w 643 v' = vec4 (x v) (y v) (z v) w
645 x' = row0 m `V4.dot` v' 644 x' = row0 m `dot` v'
646 y' = row1 m `V4.dot` v' 645 y' = row1 m `dot` v'
647 z' = row2 m `V4.dot` v' 646 z' = row2 m `dot` v'
648 w' = row3 m `V4.dot` v' 647 w' = row3 m `dot` v'
649 648
650 649
651toRAD = (*pi) . (/180) 650toRAD = (*pi) . (/180)
diff --git a/Spear/Math/MatrixUtils.hs b/Spear/Math/MatrixUtils.hs
index 629b73c..79bd049 100644
--- a/Spear/Math/MatrixUtils.hs
+++ b/Spear/Math/MatrixUtils.hs
@@ -15,8 +15,7 @@ where
15import Spear.Math.Camera as Cam 15import Spear.Math.Camera as Cam
16import Spear.Math.Matrix3 as M3 16import Spear.Math.Matrix3 as M3
17import Spear.Math.Matrix4 as M4 17import Spear.Math.Matrix4 as M4
18import Spear.Math.Vector2 as V2 18import Spear.Math.Vector as V
19import Spear.Math.Vector3 as V3
20 19
21 20
22-- | Compute the normal matrix of the given matrix. 21-- | Compute the normal matrix of the given matrix.
@@ -39,14 +38,14 @@ unproject :: Matrix4 -- ^ Inverse projection matrix
39 -> Float -- ^ Window x 38 -> Float -- ^ Window x
40 -> Float -- ^ Window y 39 -> Float -- ^ Window y
41 -> Float -- ^ Window z 40 -> Float -- ^ Window z
42 -> V3.Vector3 41 -> Vector3
43unproject projI modelviewI vpx vpy w h x y z = 42unproject projI modelviewI vpx vpy w h x y z =
44 let 43 let
45 xmouse = 2*(x-vpx)/w - 1 44 xmouse = 2*(x-vpx)/w - 1
46 ymouse = 2*(y-vpy)/h - 1 45 ymouse = 2*(y-vpy)/h - 1
47 zmouse = 2*z - 1 46 zmouse = 2*z - 1
48 in 47 in
49 (modelviewI * projI) `M4.mulp` V3.vec3 xmouse ymouse zmouse 48 (modelviewI * projI) `M4.mulp` vec3 xmouse ymouse zmouse
50 49
51 50
52-- | Transform the given point in window coordinates to 2d coordinates. 51-- | Transform the given point in window coordinates to 2d coordinates.
@@ -63,14 +62,14 @@ rpgUnproject
63 -> Float -- ^ Window x 62 -> Float -- ^ Window x
64 -> Float -- ^ Window y 63 -> Float -- ^ Window y
65 -> Vector2 64 -> Vector2
66rpgUnproject projI viewI vpx vpy w h x y = 65rpgUnproject projI viewI vpx vpy w h wx wy =
67 let 66 let
68 p1 = unproject projI viewI vpx vpy w h x y 0 67 p1 = unproject projI viewI vpx vpy w h wx wy 0
69 p2 = unproject projI viewI vpx vpy w h x y (-1) 68 p2 = unproject projI viewI vpx vpy w h wx wy (-1)
70 lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) 69 lambda = (y p1 / (y p1 - y p2))
71 p' = p1 + V3.scale lambda (p2 - p1) 70 p' = p1 + V.scale lambda (p2 - p1)
72 in 71 in
73 vec2 (V3.x p') (-V3.z p') 72 vec2 (x p') (-(z p'))
74 73
75 74
76-- | Map an object's transform in view space to world space. 75-- | Map an object's transform in view space to world space.
@@ -82,33 +81,33 @@ rpgTransform
82 -> Matrix4 -- ^ Inverse view matrix 81 -> Matrix4 -- ^ Inverse view matrix
83 -> Matrix4 82 -> Matrix4
84rpgTransform h a axis pos viewI = 83rpgTransform h a axis pos viewI =
85 let p1 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) 0) 84 let p1 = viewI `M4.mulp` (vec3 (x pos) (y pos) 0)
86 p2 = viewI `M4.mulp` (vec3 (V2.x pos) (V2.y pos) (-1)) 85 p2 = viewI `M4.mulp` (vec3 (x pos) (y pos) (-1))
87 lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) 86 lambda = (y p1 / (y p1 - y p2))
88 p = p1 + V3.scale lambda (p2 - p1) 87 p = p1 + V.scale lambda (p2 - p1)
89 mat' = axisAngle axis a 88 mat' = axisAngle axis a
90 r = M4.right mat' 89 r = M4.right mat'
91 u = M4.up mat' 90 u = M4.up mat'
92 f = M4.forward mat' 91 f = M4.forward mat'
93 t = p + vec3 0 h 0 92 t = p + vec3 0 h 0
94 in mat4 93 in mat4
95 (V3.x r) (V3.x u) (V3.x f) (V3.x t) 94 (x r) (x u) (x f) (x t)
96 (V3.y r) (V3.y u) (V3.y f) (V3.y t) 95 (y r) (y u) (y f) (y t)
97 (V3.z r) (V3.z u) (V3.z f) (V3.z t) 96 (z r) (z u) (z f) (z t)
98 0 0 0 1 97 0 0 0 1
99 98
100 99
101-- | Map an object's transform in view space to world space. 100-- | Map an object's transform in view space to world space.
102pltTransform :: Matrix3 -> Matrix4 101pltTransform :: Matrix3 -> Matrix4
103pltTransform mat = 102pltTransform mat =
104 let r = let r' = M3.right mat in vec3 (V2.x r') (V2.y r') 0 103 let r = let r' = M3.right mat in vec3 (x r') (y r') 0
105 u = let u' = M3.up mat in vec3 (V2.x u') (V2.y u') 0 104 u = let u' = M3.up mat in vec3 (x u') (y u') 0
106 f = V3.unitz 105 f = unitz3
107 t = let t' = M3.position mat in vec3 (V2.x t') (V2.y t') 0 106 t = let t' = M3.position mat in vec3 (x t') (y t') 0
108 in mat4 107 in mat4
109 (V3.x r) (V3.x u) (V3.x f) (V3.x t) 108 (x r) (x u) (x f) (x t)
110 (V3.y r) (V3.y u) (V3.y f) (V3.y t) 109 (y r) (y u) (y f) (y t)
111 (V3.z r) (V3.z u) (V3.z f) (V3.z t) 110 (z r) (z u) (z f) (z t)
112 0 0 0 1 111 0 0 0 1
113 112
114 113
@@ -147,4 +146,4 @@ objToClip cam model p =
147 proj = Cam.projection cam 146 proj = Cam.projection cam
148 p' = (proj * view * model) `M4.mulp` p 147 p' = (proj * view * model) `M4.mulp` p
149 in 148 in
150 vec2 (V3.x p') (V3.y p') 149 vec2 (x p') (y p')
diff --git a/Spear/Math/Plane.hs b/Spear/Math/Plane.hs
index 6fabbec..b20740c 100644
--- a/Spear/Math/Plane.hs
+++ b/Spear/Math/Plane.hs
@@ -7,7 +7,7 @@ module Spear.Math.Plane
7where 7where
8 8
9 9
10import Spear.Math.Vector3 10import Spear.Math.Vector
11 11
12 12
13data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show) 13data PointPlanePos = Front | Back | Contained deriving (Eq, Ord, Show)
diff --git a/Spear/Math/Quad.hs b/Spear/Math/Quad.hs
index e75607c..6b6215c 100644
--- a/Spear/Math/Quad.hs
+++ b/Spear/Math/Quad.hs
@@ -8,7 +8,7 @@ where
8 8
9import Spear.Math.Segment 9import Spear.Math.Segment
10import Spear.Math.Utils 10import Spear.Math.Utils
11import Spear.Math.Vector2 11import Spear.Math.Vector
12 12
13 13
14data Quad = Quad 14data Quad = Quad
diff --git a/Spear/Math/QuadTree.hs b/Spear/Math/QuadTree.hs
index e553c88..d6b6353 100644
--- a/Spear/Math/QuadTree.hs
+++ b/Spear/Math/QuadTree.hs
@@ -11,7 +11,7 @@ where
11 11
12import Spear.Collision 12import Spear.Collision
13import Spear.Math.AABB 13import Spear.Math.AABB
14import Spear.Math.Vector2 14import Spear.Math.Vector
15 15
16import Control.Applicative ((<*>)) 16import Control.Applicative ((<*>))
17import Data.List 17import Data.List
diff --git a/Spear/Math/Quaternion.hs b/Spear/Math/Quaternion.hs
index 4eb88d7..cfc6cd2 100644
--- a/Spear/Math/Quaternion.hs
+++ b/Spear/Math/Quaternion.hs
@@ -10,15 +10,14 @@ module Spear.Math.Quaternion
10, qmul 10, qmul
11, qconj 11, qconj
12, qinv 12, qinv
13, Spear.Math.Quaternion.normalise 13, qnormalise
14, Spear.Math.Quaternion.norm 14, qnorm
15, qrot 15, qrot
16) 16)
17where 17where
18 18
19 19
20import qualified Spear.Math.Vector3 as V3 20import Spear.Math.Vector
21import Spear.Math.Vector4 as V4
22 21
23 22
24newtype Quaternion = Quaternion { getVec :: Vector4 } 23newtype Quaternion = Quaternion { getVec :: Vector4 }
@@ -39,23 +38,23 @@ qvec4 = Quaternion
39 38
40 39
41-- | Build a 'Quaternion' from the given 'Vector3' and w. 40-- | Build a 'Quaternion' from the given 'Vector3' and w.
42qvec3 :: V3.Vector3 -> Float -> Quaternion 41qvec3 :: Vector3 -> Float -> Quaternion
43qvec3 v w = Quaternion $ vec4 (V3.x v) (V3.y v) (V3.z v) w 42qvec3 v w = Quaternion $ vec4 (x v) (y v) (z v) w
44 43
45 44
46-- | Build a 'Quaternion' representing the given rotation. 45-- | Build a 'Quaternion' representing the given rotation.
47qAxisAngle :: V3.Vector3 -> Float -> Quaternion 46qAxisAngle :: Vector3 -> Float -> Quaternion
48qAxisAngle axis angle = 47qAxisAngle axis angle =
49 let s' = V3.norm axis 48 let s' = norm axis
50 s = if s' == 0 then 1 else s' 49 s = if s' == 0 then 1 else s'
51 a = angle * toRAD * 0.5 50 a = angle * toRAD * 0.5
52 sa = sin a 51 sa = sin a
53 w = cos a 52 qw = cos a
54 x = V3.x axis * sa * s 53 qx = x axis * sa * s
55 y = V3.y axis * sa * s 54 qy = y axis * sa * s
56 z = V3.z axis * sa * s 55 qz = z axis * sa * s
57 in 56 in
58 Quaternion $ vec4 x y z w 57 Quaternion $ vec4 qx qy qz qw
59 58
60 59
61-- | Compute the product of the given two quaternions. 60-- | Compute the product of the given two quaternions.
@@ -90,19 +89,19 @@ qinv (Quaternion q) =
90 89
91 90
92-- | Normalise the given 'Quaternion'. 91-- | Normalise the given 'Quaternion'.
93normalise :: Quaternion -> Quaternion 92qnormalise :: Quaternion -> Quaternion
94normalise = Quaternion . V4.normalise . getVec 93qnormalise = Quaternion . normalise . getVec
95 94
96 95
97-- | Compute the norm of the given 'Quaternion'. 96-- | Compute the norm of the given 'Quaternion'.
98norm :: Quaternion -> Float 97qnorm :: Quaternion -> Float
99norm = V4.norm . getVec 98qnorm = norm . getVec
100 99
101 100
102-- | Rotate the given 'Vector3'. 101-- | Rotate the given 'Vector3'.
103qrot :: Quaternion -> V3.Vector3 -> V3.Vector3 102qrot :: Quaternion -> Vector3 -> Vector3
104qrot q v = toVec3 $ q `qmul` qvec3 v 0 `qmul` qconj q 103qrot q v = toVec3 $ q `qmul` qvec3 v 0 `qmul` qconj q
105 where toVec3 (Quaternion q) = V3.vec3 (x q) (y q) (z q) 104 where toVec3 (Quaternion q) = vec3 (x q) (y q) (z q)
106 105
107 106
108toRAD = pi / 180 107toRAD = pi / 180
diff --git a/Spear/Math/Ray.hs b/Spear/Math/Ray.hs
index 697d609..b0359a1 100644
--- a/Spear/Math/Ray.hs
+++ b/Spear/Math/Ray.hs
@@ -8,7 +8,7 @@ where
8 8
9 9
10import Spear.Math.Utils 10import Spear.Math.Utils
11import Spear.Math.Vector2 11import Spear.Math.Vector
12 12
13 13
14data Ray = Ray 14data Ray = Ray
diff --git a/Spear/Math/Segment.hs b/Spear/Math/Segment.hs
index a89ee05..c632838 100644
--- a/Spear/Math/Segment.hs
+++ b/Spear/Math/Segment.hs
@@ -7,7 +7,7 @@ where
7 7
8 8
9import Spear.Math.Utils 9import Spear.Math.Utils
10import Spear.Math.Vector2 10import Spear.Math.Vector
11 11
12 12
13-- | A line segment in 2D space. 13-- | A line segment in 2D space.
diff --git a/Spear/Math/Spatial2.hs b/Spear/Math/Spatial2.hs
index 49ff3b7..341282b 100644
--- a/Spear/Math/Spatial2.hs
+++ b/Spear/Math/Spatial2.hs
@@ -2,7 +2,7 @@ module Spear.Math.Spatial2
2where 2where
3 3
4 4
5import Spear.Math.Vector2 5import Spear.Math.Vector
6import Spear.Math.Matrix3 as M 6import Spear.Math.Matrix3 as M
7 7
8 8
diff --git a/Spear/Math/Spatial3.hs b/Spear/Math/Spatial3.hs
index 0ee680f..6db3853 100644
--- a/Spear/Math/Spatial3.hs
+++ b/Spear/Math/Spatial3.hs
@@ -2,7 +2,7 @@ module Spear.Math.Spatial3
2where 2where
3 3
4 4
5import Spear.Math.Vector3 5import Spear.Math.Vector
6import Spear.Math.Matrix4 as M 6import Spear.Math.Matrix4 as M
7 7
8 8
@@ -57,7 +57,7 @@ class Spatial3 s where
57 lookAt pt s = 57 lookAt pt s =
58 let position = pos s 58 let position = pos s
59 fwd = normalise $ pt - position 59 fwd = normalise $ pt - position
60 r = fwd `cross` unity 60 r = fwd `cross` unity3
61 u = r `cross` fwd 61 u = r `cross` fwd
62 in 62 in
63 setTransform (M.transform r u (-fwd) position) s 63 setTransform (M.transform r u (-fwd) position) s
diff --git a/Spear/Math/Triangle.hs b/Spear/Math/Triangle.hs
index 3c30ea6..96cfa1a 100644
--- a/Spear/Math/Triangle.hs
+++ b/Spear/Math/Triangle.hs
@@ -5,7 +5,7 @@ module Spear.Math.Triangle
5where 5where
6 6
7 7
8import Spear.Math.Vector3 8import Spear.Math.Vector
9 9
10import Foreign.C.Types 10import Foreign.C.Types
11import Foreign.Storable 11import Foreign.Storable
diff --git a/Spear/Math/Utils.hs b/Spear/Math/Utils.hs
index 1296f25..90ebda9 100644
--- a/Spear/Math/Utils.hs
+++ b/Spear/Math/Utils.hs
@@ -9,8 +9,7 @@ where
9 9
10 10
11import Spear.Math.Matrix4 as M4 11import Spear.Math.Matrix4 as M4
12import Spear.Math.Vector2 as V2 12import Spear.Math.Vector as V
13import qualified Spear.Math.Vector3 as V3
14 13
15 14
16data Side = L | R deriving (Eq, Show) 15data Side = L | R deriving (Eq, Show)
@@ -30,10 +29,10 @@ viewToWorld2d :: Vector2 -- ^ Point in view space
30 -> Vector2 -- ^ Projection of the given point 29 -> Vector2 -- ^ Projection of the given point
31viewToWorld2d p viewI = 30viewToWorld2d p viewI =
32 let 31 let
33 p1' = V3.vec3 (V2.x p) (V2.y p) 0 32 p1' = vec3 (x p) (y p) 0
34 p1 = viewI `mulp` p1' 33 p1 = viewI `mulp` p1'
35 p2 = p1 - M4.forward viewI 34 p2 = p1 - M4.forward viewI
36 lambda = (V3.y p1 / (V3.y p1 - V3.y p2)) 35 lambda = (y p1 / (y p1 - y p2))
37 p' = p1 + V3.scale lambda (p2 - p1) 36 p' = p1 + V.scale lambda (p2 - p1)
38 in 37 in
39 vec2 (V3.x p') (-V3.z p') 38 vec2 (x p') (-z p')
diff --git a/Spear/Math/Vector.hs b/Spear/Math/Vector.hs
new file mode 100644
index 0000000..a1cb9e8
--- /dev/null
+++ b/Spear/Math/Vector.hs
@@ -0,0 +1,13 @@
1module Spear.Math.Vector
2(
3 module Spear.Math.Vector.Vector2
4, module Spear.Math.Vector.Vector3
5, module Spear.Math.Vector.Vector4
6, module Spear.Math.Vector.Class
7)
8where
9
10import Spear.Math.Vector.Vector2
11import Spear.Math.Vector.Vector3
12import Spear.Math.Vector.Vector4
13import Spear.Math.Vector.Class
diff --git a/Spear/Math/Vector/Class.hs b/Spear/Math/Vector/Class.hs
new file mode 100644
index 0000000..05a7206
--- /dev/null
+++ b/Spear/Math/Vector/Class.hs
@@ -0,0 +1,43 @@
1module Spear.Math.Vector.Class
2where
3
4class (Fractional a, Ord a) => VectorClass a where
5 -- | Create a vector from the given list.
6 fromList :: [Float] -> a
7
8 -- | Return the vector's x coordinate.
9 x :: a -> Float
10 x _ = 0
11
12 -- | Return the vector's y coordinate.
13 y :: a -> Float
14 y _ = 0
15
16 -- | Return the vector's z coordinate.
17 z :: a -> Float
18 z _ = 0
19
20 -- | Return the vector's w coordinate.
21 w :: a -> Float
22 w _ = 0
23
24 -- | Return the vector's ith coordinate.
25 (!) :: a -> Int -> Float
26
27 -- | Compute the given vectors' dot product.
28 dot :: a -> a -> Float
29
30 -- | Compute the given vector's squared norm.
31 normSq :: a -> Float
32
33 -- | Compute the given vector's norm.
34 norm :: a -> Float
35
36 -- | Multiply the given vector with the given scalar.
37 scale :: Float -> a -> a
38
39 -- | Negate the given vector.
40 neg :: a -> a
41
42 -- | Normalise the given vector.
43 normalise :: a -> a \ No newline at end of file
diff --git a/Spear/Math/Vector2.hs b/Spear/Math/Vector/Vector2.hs
index 581a64f..7aaece5 100644
--- a/Spear/Math/Vector2.hs
+++ b/Spear/Math/Vector/Vector2.hs
@@ -1,27 +1,20 @@
1module Spear.Math.Vector2 1module Spear.Math.Vector.Vector2
2( 2(
3 Vector2 3 Vector2
4 -- * Accessors
5, x
6, y
7 -- * Construction 4 -- * Construction
8, unitx 5, unitx2
9, unity 6, unity2
10, zero 7, zero2
11, fromList
12, vec2 8, vec2
13 -- * Operations 9 -- * Operations
14, perp 10, perp
15, dot
16, normSq
17, norm
18, scale
19, neg
20, normalise
21) 11)
22where 12where
23 13
24 14
15import Spear.Math.Vector.Class
16
17
25import Foreign.C.Types (CFloat) 18import Foreign.C.Types (CFloat)
26import Foreign.Storable 19import Foreign.Storable
27 20
@@ -53,6 +46,33 @@ instance Ord Vector2 where
53 min (Vector2 ax ay) (Vector2 bx by) = Vector2 (Prelude.min ax bx) (Prelude.min ay by) 46 min (Vector2 ax ay) (Vector2 bx by) = Vector2 (Prelude.min ax bx) (Prelude.min ay by)
54 47
55 48
49instance VectorClass Vector2 where
50 fromList (ax:ay:_) = Vector2 ax ay
51
52 x (Vector2 ax _) = ax
53
54 y (Vector2 _ ay) = ay
55
56 (Vector2 ax _) ! 0 = ax
57 (Vector2 _ ay) ! 1 = ay
58 _ ! _ = 0
59
60 Vector2 ax ay `dot` Vector2 bx by = ax*bx + ay*by
61
62 normSq (Vector2 ax ay) = ax*ax + ay*ay
63
64 norm = sqrt . normSq
65
66 scale s (Vector2 ax ay) = Vector2 (s*ax) (s*ay)
67
68 neg (Vector2 ax ay) = Vector2 (-ax) (-ay)
69
70 normalise v =
71 let n' = norm v
72 n = if n' == 0 then 1 else n'
73 in scale (1.0 / n) v
74
75
56sizeFloat = sizeOf (undefined :: CFloat) 76sizeFloat = sizeOf (undefined :: CFloat)
57 77
58 78
@@ -71,31 +91,19 @@ instance Storable Vector2 where
71 91
72 92
73-- | Get the vector's x coordinate. 93-- | Get the vector's x coordinate.
74x (Vector2 ax _) = ax
75
76 94
77-- | Get the vector's y coordinate.
78y (Vector2 _ ay) = ay
79 95
80 96
81-- | Unit vector along the X axis. 97-- | Unit vector along the X axis.
82unitx :: Vector2 98unitx2 = Vector2 1 0
83unitx = Vector2 1 0
84 99
85 100
86-- | Unit vector along the Y axis. 101-- | Unit vector along the Y axis.
87unity :: Vector2 102unity2 = Vector2 0 1
88unity = Vector2 0 1
89 103
90 104
91-- | Zero vector. 105-- | Zero vector.
92zero :: Vector2 106zero2 = Vector2 0 0
93zero = Vector2 0 0
94
95
96-- | Create a vector from the given list.
97fromList :: [Float] -> Vector2
98fromList (ax:ay:_) = Vector2 ax ay
99 107
100 108
101-- | Create a vector from the given values. 109-- | Create a vector from the given values.
@@ -110,36 +118,3 @@ vec2 ax ay = Vector2 ax ay
110-- perp (Vector2 1 0) = Vector2 0 (-1) 118-- perp (Vector2 1 0) = Vector2 0 (-1)
111perp :: Vector2 -> Vector2 119perp :: Vector2 -> Vector2
112perp (Vector2 x y) = Vector2 y (-x) 120perp (Vector2 x y) = Vector2 y (-x)
113
114
115-- | Compute the given vectors' dot product.
116dot :: Vector2 -> Vector2 -> Float
117Vector2 ax ay `dot` Vector2 bx by = ax*bx + ay*by
118
119
120-- | Compute the given vector's squared norm.
121normSq :: Vector2 -> Float
122normSq (Vector2 ax ay) = ax*ax + ay*ay
123
124
125-- | Compute the given vector's norm.
126norm :: Vector2 -> Float
127norm = sqrt . normSq
128
129
130-- | Multiply the given vector with the given scalar.
131scale :: Float -> Vector2 -> Vector2
132scale s (Vector2 ax ay) = Vector2 (s*ax) (s*ay)
133
134
135-- | Negate the given vector.
136neg :: Vector2 -> Vector2
137neg (Vector2 ax ay) = Vector2 (-ax) (-ay)
138
139
140-- | Normalise the given vector.
141normalise :: Vector2 -> Vector2
142normalise v =
143 let n' = norm v
144 n = if n' == 0 then 1 else n'
145 in scale (1.0 / n) v
diff --git a/Spear/Math/Vector3.hs b/Spear/Math/Vector/Vector3.hs
index d280811..c19b7c7 100644
--- a/Spear/Math/Vector3.hs
+++ b/Spear/Math/Vector/Vector3.hs
@@ -1,30 +1,21 @@
1module Spear.Math.Vector3 1module Spear.Math.Vector.Vector3
2( 2(
3 Vector3 3 Vector3
4 -- * Accessors
5, x
6, y
7, z
8 -- * Construction 4 -- * Construction
9, unitx 5, unitx3
10, unity 6, unity3
11, unitz 7, unitz3
12, zero 8, zero3
13, fromList
14, vec3 9, vec3
15, orbit 10, orbit
16 -- * Operations 11 -- * Operations
17, dot
18, cross 12, cross
19, normSq
20, norm
21, scale
22, neg
23, normalise
24) 13)
25where 14where
26 15
27 16
17import Spear.Math.Vector.Class
18
28import Foreign.C.Types (CFloat) 19import Foreign.C.Types (CFloat)
29import Foreign.Storable 20import Foreign.Storable
30 21
@@ -77,6 +68,36 @@ instance Ord Vector3 where
77 min (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.min ax bx) (Prelude.min ay by) (Prelude.min az bz) 68 min (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.min ax bx) (Prelude.min ay by) (Prelude.min az bz)
78 69
79 70
71instance VectorClass Vector3 where
72 fromList (ax:ay:az:_) = Vector3 ax ay az
73
74 x (Vector3 ax _ _ ) = ax
75
76 y (Vector3 _ ay _ ) = ay
77
78 z (Vector3 _ _ az) = az
79
80 (Vector3 ax _ _) ! 0 = ax
81 (Vector3 _ ay _) ! 1 = ay
82 (Vector3 _ _ az) ! 2 = az
83 _ ! _ = 0
84
85 Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz
86
87 normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az
88
89 norm = sqrt . normSq
90
91 scale s (Vector3 ax ay az) = Vector3 (s*ax) (s*ay) (s*az)
92
93 neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az)
94
95 normalise v =
96 let n' = norm v
97 n = if n' == 0 then 1 else n'
98 in scale (1.0 / n) v
99
100
80sizeFloat = sizeOf (undefined :: CFloat) 101sizeFloat = sizeOf (undefined :: CFloat)
81 102
82 103
@@ -96,34 +117,20 @@ instance Storable Vector3 where
96 pokeByteOff ptr (2*sizeFloat) az 117 pokeByteOff ptr (2*sizeFloat) az
97 118
98 119
99x (Vector3 ax _ _ ) = ax
100y (Vector3 _ ay _ ) = ay
101z (Vector3 _ _ az) = az
102
103
104-- | Unit vector along the X axis. 120-- | Unit vector along the X axis.
105unitx :: Vector3 121unitx3 = Vector3 1 0 0
106unitx = Vector3 1 0 0
107 122
108 123
109-- | Unit vector along the Y axis. 124-- | Unit vector along the Y axis.
110unity :: Vector3 125unity3 = Vector3 0 1 0
111unity = Vector3 0 1 0
112 126
113 127
114-- | Unit vector along the Z axis. 128-- | Unit vector along the Z axis.
115unitz :: Vector3 129unitz3 = Vector3 0 0 1
116unitz = Vector3 0 0 1
117 130
118 131
119-- | Zero vector. 132-- | Zero vector.
120zero :: Vector3 133zero3 = Vector3 0 0 0
121zero = Vector3 0 0 0
122
123
124-- | Create a vector from the given list.
125fromList :: [Float] -> Vector3
126fromList (ax:ay:az:_) = Vector3 ax ay az
127 134
128 135
129-- | Create a 3D vector from the given values. 136-- | Create a 3D vector from the given values.
@@ -152,36 +159,7 @@ orbit center radius anglex angley =
152 vec3 px py pz 159 vec3 px py pz
153 160
154 161
155-- | Compute the given vectors' dot product.
156dot :: Vector3 -> Vector3 -> Float
157Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz
158
159
160-- | Compute the given vectors' cross product. 162-- | Compute the given vectors' cross product.
161cross :: Vector3 -> Vector3 -> Vector3 163cross :: Vector3 -> Vector3 -> Vector3
162(Vector3 ax ay az) `cross` (Vector3 bx by bz) = 164(Vector3 ax ay az) `cross` (Vector3 bx by bz) =
163 Vector3 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) 165 Vector3 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx)
164
165
166-- | Compute the given vector's squared norm.
167normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az
168
169
170-- | Compute the given vector's norm.
171norm = sqrt . normSq
172
173
174-- | Multiply the given vector with the given scalar.
175scale s (Vector3 ax ay az) = Vector3 (s*ax) (s*ay) (s*az)
176
177
178-- | Negate the given vector.
179neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az)
180
181
182-- | Normalise the given vector.
183normalise v =
184 let n' = norm v
185 n = if n' == 0 then 1 else n'
186 in scale (1.0 / n) v
187
diff --git a/Spear/Math/Vector4.hs b/Spear/Math/Vector/Vector4.hs
index 554fb27..1f5494d 100644
--- a/Spear/Math/Vector4.hs
+++ b/Spear/Math/Vector/Vector4.hs
@@ -1,28 +1,19 @@
1module Spear.Math.Vector4 1module Spear.Math.Vector.Vector4
2( 2(
3 Vector4 3 Vector4
4 -- * Accessors
5, x
6, y
7, z
8, w
9 -- * Construction 4 -- * Construction
10, unitX 5, unitx4
11, unitY 6, unity4
12, unitZ 7, unitz4
13, fromList
14, vec4 8, vec4
15 -- * Operations 9 -- * Operations
16, dot 10, cross'
17, normSq
18, norm
19, scale
20, neg
21, normalise
22) 11)
23where 12where
24 13
25 14
15import Spear.Math.Vector.Class
16
26import Foreign.C.Types (CFloat) 17import Foreign.C.Types (CFloat)
27import Foreign.Storable 18import Foreign.Storable
28 19
@@ -82,6 +73,39 @@ instance Ord Vector4 where
82 Vector4 (Prelude.max ax bx) (Prelude.max ay by) (Prelude.max az bz) (Prelude.min aw bw) 73 Vector4 (Prelude.max ax bx) (Prelude.max ay by) (Prelude.max az bz) (Prelude.min aw bw)
83 74
84 75
76instance VectorClass Vector4 where
77 fromList (ax:ay:az:aw:_) = Vector4 ax ay az aw
78
79 x (Vector4 ax _ _ _ ) = ax
80
81 y (Vector4 _ ay _ _ ) = ay
82
83 z (Vector4 _ _ az _ ) = az
84
85 w (Vector4 _ _ _ aw) = aw
86
87 (Vector4 ax _ _ _) ! 0 = ax
88 (Vector4 _ ay _ _) ! 1 = ay
89 (Vector4 _ _ az _) ! 2 = az
90 (Vector4 _ _ _ aw) ! 3 = aw
91 _ ! _ = 0
92
93 Vector4 ax ay az aw `dot` Vector4 bx by bz bw = ax*bx + ay*by + az*bz + aw*bw
94
95 normSq (Vector4 ax ay az aw) = ax*ax + ay*ay + az*az + aw*aw
96
97 norm = sqrt . normSq
98
99 scale s (Vector4 ax ay az aw) = Vector4 (s*ax) (s*ay) (s*az) (s*aw)
100
101 neg (Vector4 ax ay az aw) = Vector4 (-ax) (-ay) (-az) (-aw)
102
103 normalise v =
104 let n' = norm v
105 n = if n' == 0 then 1 else n'
106 in scale (1.0 / n) v
107
108
85sizeFloat = sizeOf (undefined :: CFloat) 109sizeFloat = sizeOf (undefined :: CFloat)
86 110
87 111
@@ -103,30 +127,19 @@ instance Storable Vector4 where
103 pokeByteOff ptr (3 * sizeFloat) aw 127 pokeByteOff ptr (3 * sizeFloat) aw
104 128
105 129
106x (Vector4 ax _ _ _ ) = ax
107y (Vector4 _ ay _ _ ) = ay
108z (Vector4 _ _ az _ ) = az
109w (Vector4 _ _ _ aw) = aw
110
111
112-- | Unit vector along the X axis. 130-- | Unit vector along the X axis.
113unitX :: Vector4 131unitx4 = Vector4 1 0 0 0
114unitX = Vector4 1 0 0 0
115 132
116 133
117-- | Unit vector along the Y axis. 134-- | Unit vector along the Y axis.
118unitY :: Vector4 135unity4 = Vector4 0 1 0 0
119unitY = Vector4 0 1 0 0
120 136
121 137
122-- | Unit vector along the Z axis. 138-- | Unit vector along the Z axis.
123unitZ :: Vector4 139unitz4 = Vector4 0 0 1 0
124unitZ = Vector4 0 0 1 0
125 140
126 141-- | Unit vector along the W axis.
127-- | Create a vector from the given list. 142unitw4 = Vector4 0 0 0 1
128fromList :: [Float] -> Vector4
129fromList (ax:ay:az:aw:_) = Vector4 ax ay az aw
130 143
131 144
132-- | Create a 4D vector from the given values. 145-- | Create a 4D vector from the given values.
@@ -134,43 +147,8 @@ vec4 :: Float -> Float -> Float -> Float -> Vector4
134vec4 ax ay az aw = Vector4 ax ay az aw 147vec4 ax ay az aw = Vector4 ax ay az aw
135 148
136 149
137-- | Compute the given vectors' dot product.
138dot :: Vector4 -> Vector4 -> Float
139Vector4 ax ay az aw `dot` Vector4 bx by bz bw = ax*bx + ay*by + az*bz + aw*bw
140
141
142-- | Compute the given vectors' cross product. 150-- | Compute the given vectors' cross product.
143-- The vectors are projected to 3D space. The resulting vector is the cross product of the vectors' projections with w=0. 151-- The vectors are projected to 3D space. The resulting vector is the cross product of the vectors' projections with w=0.
144cross :: Vector4 -> Vector4 -> Vector4 152cross' :: Vector4 -> Vector4 -> Vector4
145(Vector4 ax ay az _) `cross` (Vector4 bx by bz _) = 153(Vector4 ax ay az _) `cross'` (Vector4 bx by bz _) =
146 Vector4 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) 0 154 Vector4 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx) 0
147
148
149-- | Compute the given vector's squared norm.
150normSq :: Vector4 -> Float
151normSq (Vector4 ax ay az aw) = ax*ax + ay*ay + az*az + aw*aw
152
153
154-- | Compute the given vector's norm.
155norm :: Vector4 -> Float
156norm = sqrt . normSq
157
158
159-- | Multiply the given vector with the given scalar.
160scale :: Float -> Vector4 -> Vector4
161scale s (Vector4 ax ay az aw) = Vector4 (s*ax) (s*ay) (s*az) (s*aw)
162
163
164-- | Negate the given vector.
165neg :: Vector4 -> Vector4
166neg (Vector4 ax ay az aw) = Vector4 (-ax) (-ay) (-az) (-aw)
167
168
169-- | Normalise the given vector.
170normalise :: Vector4 -> Vector4
171normalise v =
172 let n' = norm v
173 n = if n' == 0 then 1 else n'
174 in
175 scale (1.0 / n) v
176
diff --git a/Spear/Physics/Rigid.hs b/Spear/Physics/Rigid.hs
index 9147553..99a9d5a 100644
--- a/Spear/Physics/Rigid.hs
+++ b/Spear/Physics/Rigid.hs
@@ -12,7 +12,7 @@ where
12 12
13import qualified Spear.Math.Matrix3 as M3 13import qualified Spear.Math.Matrix3 as M3
14import Spear.Math.Spatial2 14import Spear.Math.Spatial2
15import Spear.Math.Vector2 15import Spear.Math.Vector
16import Spear.Physics.Types 16import Spear.Physics.Types
17 17
18import Data.List (foldl') 18import Data.List (foldl')
@@ -31,13 +31,13 @@ instance Spatial2 RigidBody where
31 31
32 move v body = body { position = v + position body } 32 move v body = body { position = v + position body }
33 33
34 moveFwd speed body = body { position = position body + scale speed unity } 34 moveFwd speed body = body { position = position body + scale speed unity2 }
35 35
36 moveBack speed body = body { position = position body + scale (-speed) unity } 36 moveBack speed body = body { position = position body + scale (-speed) unity2 }
37 37
38 strafeLeft speed body = body { position = position body + scale (-speed) unitx } 38 strafeLeft speed body = body { position = position body + scale (-speed) unitx2 }
39 39
40 strafeRight speed body = body { position = position body + scale speed unitx } 40 strafeRight speed body = body { position = position body + scale speed unitx2 }
41 41
42 rotate angle = id 42 rotate angle = id
43 43
@@ -45,13 +45,13 @@ instance Spatial2 RigidBody where
45 45
46 pos = position 46 pos = position
47 47
48 fwd _ = unity 48 fwd _ = unity2
49 49
50 up _ = unity 50 up _ = unity2
51 51
52 right _ = unitx 52 right _ = unitx2
53 53
54 transform body = M3.transform unitx unity $ position body 54 transform body = M3.transform unitx2 unity2 $ position body
55 55
56 setTransform transf body = body { position = M3.position transf } 56 setTransform transf body = body { position = M3.position transf }
57 57
@@ -60,13 +60,13 @@ instance Spatial2 RigidBody where
60 60
61-- | Build a 'RigidBody'. 61-- | Build a 'RigidBody'.
62rigidBody :: Mass -> Position -> RigidBody 62rigidBody :: Mass -> Position -> RigidBody
63rigidBody m x = RigidBody m x zero zero 63rigidBody m x = RigidBody m x zero2 zero2
64 64
65 65
66-- | Update the given 'RigidBody'. 66-- | Update the given 'RigidBody'.
67update :: [Force] -> Dt -> RigidBody -> RigidBody 67update :: [Force] -> Dt -> RigidBody -> RigidBody
68update forces dt body = 68update forces dt body =
69 let netforce = foldl' (+) zero forces 69 let netforce = foldl' (+) zero2 forces
70 m = mass body 70 m = mass body
71 r1 = position body 71 r1 = position body
72 v1 = velocity body 72 v1 = velocity body
diff --git a/Spear/Physics/Types.hs b/Spear/Physics/Types.hs
index de889ee..62e0c04 100644
--- a/Spear/Physics/Types.hs
+++ b/Spear/Physics/Types.hs
@@ -2,7 +2,7 @@ module Spear.Physics.Types
2where 2where
3 3
4 4
5import Spear.Math.Vector2 5import Spear.Math.Vector
6 6
7 7
8type Dt = Float 8type Dt = Float
diff --git a/Spear/Render/AnimatedModel.hs b/Spear/Render/AnimatedModel.hs
index 76e9e7f..dfaadfd 100644
--- a/Spear/Render/AnimatedModel.hs
+++ b/Spear/Render/AnimatedModel.hs
@@ -34,8 +34,7 @@ import Spear.Collision
34import Spear.GLSL 34import Spear.GLSL
35import Spear.Math.AABB 35import Spear.Math.AABB
36import Spear.Math.Matrix4 (Matrix4) 36import Spear.Math.Matrix4 (Matrix4)
37import Spear.Math.Vector2 (vec2) 37import Spear.Math.Vector
38import Spear.Math.Vector3 (vec3, x, y, z, scale)
39import Spear.Render.Material 38import Spear.Render.Material
40import Spear.Render.Model 39import Spear.Render.Model
41import Spear.Render.Program 40import Spear.Render.Program
diff --git a/Spear/Render/Material.hs b/Spear/Render/Material.hs
index f504036..83d8742 100644
--- a/Spear/Render/Material.hs
+++ b/Spear/Render/Material.hs
@@ -4,7 +4,7 @@ module Spear.Render.Material
4where 4where
5 5
6 6
7import Spear.Math.Vector4 7import Spear.Math.Vector
8 8
9 9
10data Material = Material 10data Material = Material
diff --git a/Spear/Render/StaticModel.hs b/Spear/Render/StaticModel.hs
index c67405a..ed8d065 100644
--- a/Spear/Render/StaticModel.hs
+++ b/Spear/Render/StaticModel.hs
@@ -24,7 +24,7 @@ import Spear.Collision
24import Spear.GLSL 24import Spear.GLSL
25import Spear.Math.AABB 25import Spear.Math.AABB
26import Spear.Math.Matrix4 (Matrix4) 26import Spear.Math.Matrix4 (Matrix4)
27import Spear.Math.Vector2 (vec2) 27import Spear.Math.Vector
28import Spear.Render.Material 28import Spear.Render.Material
29import Spear.Render.Model 29import Spear.Render.Model
30import Spear.Render.Program 30import Spear.Render.Program
diff --git a/Spear/Scene/GameObject.hs b/Spear/Scene/GameObject.hs
index d98299c..37f9260 100644
--- a/Spear/Scene/GameObject.hs
+++ b/Spear/Scene/GameObject.hs
@@ -40,8 +40,7 @@ import qualified Spear.Math.Matrix4 as M4
40import Spear.Math.MatrixUtils 40import Spear.Math.MatrixUtils
41import qualified Spear.Math.Spatial2 as S2 41import qualified Spear.Math.Spatial2 as S2
42import Spear.Math.Utils 42import Spear.Math.Utils
43import Spear.Math.Vector2 as V2 43import Spear.Math.Vector
44import Spear.Math.Vector3 as V3
45import qualified Spear.Render.AnimatedModel as AM 44import qualified Spear.Render.AnimatedModel as AM
46import Spear.Render.Program 45import Spear.Render.Program
47import Spear.Render.StaticModel as SM 46import Spear.Render.StaticModel as SM
@@ -90,7 +89,7 @@ instance S2.Spatial2 GameObject where
90 89
91 moveFwd s go = 90 moveFwd s go =
92 let m = transform go 91 let m = transform go
93 v = V2.scale s $ M3.forward m 92 v = scale s $ M3.forward m
94 in go 93 in go
95 { collisioners = fmap (Col.move v) $ collisioners go 94 { collisioners = fmap (Col.move v) $ collisioners go
96 , transform = M3.translv v * m 95 , transform = M3.translv v * m
@@ -98,7 +97,7 @@ instance S2.Spatial2 GameObject where
98 97
99 moveBack s go = 98 moveBack s go =
100 let m = transform go 99 let m = transform go
101 v = V2.scale (-s) $ M3.forward m 100 v = scale (-s) $ M3.forward m
102 in go 101 in go
103 { collisioners = fmap (Col.move v) $ collisioners go 102 { collisioners = fmap (Col.move v) $ collisioners go
104 , transform = M3.translv v * m 103 , transform = M3.translv v * m
@@ -106,7 +105,7 @@ instance S2.Spatial2 GameObject where
106 105
107 strafeLeft s go = 106 strafeLeft s go =
108 let m = transform go 107 let m = transform go
109 v = V2.scale (-s) $ M3.right m 108 v = scale (-s) $ M3.right m
110 in go 109 in go
111 { collisioners = fmap (Col.move v) $ collisioners go 110 { collisioners = fmap (Col.move v) $ collisioners go
112 , transform = M3.translv v * m 111 , transform = M3.translv v * m
@@ -114,7 +113,7 @@ instance S2.Spatial2 GameObject where
114 113
115 strafeRight s go = 114 strafeRight s go =
116 let m = transform go 115 let m = transform go
117 v = V2.scale s $ M3.right m 116 v = scale s $ M3.right m
118 in go 117 in go
119 { collisioners = fmap (Col.move v) $ collisioners go 118 { collisioners = fmap (Col.move v) $ collisioners go
120 , transform = M3.translv v * m 119 , transform = M3.translv v * m
@@ -150,20 +149,20 @@ instance S2.Spatial2 GameObject where
150 149
151 lookAt p go = 150 lookAt p go =
152 let position = S2.pos go 151 let position = S2.pos go
153 fwd = V2.normalise $ p - position 152 fwd = normalise $ p - position
154 r = perp fwd 153 r = perp fwd
155 toDeg = (*(180/pi)) 154 toDeg = (*(180/pi))
156 viewI = viewInv . window $ go 155 viewI = viewInv . window $ go
157 p1 = viewToWorld2d position viewI 156 p1 = viewToWorld2d position viewI
158 p2 = viewToWorld2d (position + fwd) viewI 157 p2 = viewToWorld2d (position + fwd) viewI
159 f = V2.normalise $ p2 - p1 158 f = normalise $ p2 - p1
160 in 159 in
161 go 160 go
162 { transform = M3.transform r fwd position 161 { transform = M3.transform r fwd position
163 , angle = 180 - 162 , angle = 180 -
164 if V2.x f > 0 163 if x f > 0
165 then toDeg . acos $ f `V2.dot` V2.unity 164 then toDeg . acos $ f `dot` unity2
166 else (+180) . toDeg . acos $ f `V2.dot` (-V2.unity) 165 else (+180) . toDeg . acos $ f `dot` (-unity2)
167 } 166 }
168 167
169 168
diff --git a/Spear/Scene/Light.hs b/Spear/Scene/Light.hs
index f482560..5f43b19 100644
--- a/Spear/Scene/Light.hs
+++ b/Spear/Scene/Light.hs
@@ -7,8 +7,7 @@ where
7 7
8import qualified Spear.Math.Matrix4 as M 8import qualified Spear.Math.Matrix4 as M
9import qualified Spear.Math.Spatial3 as S 9import qualified Spear.Math.Spatial3 as S
10import Spear.Math.Vector3 10import Spear.Math.Vector
11import qualified Spear.Math.Vector4 as V4
12 11
13 12
14data Light 13data Light
diff --git a/Spear/Scene/Loader.hs b/Spear/Scene/Loader.hs
index 3bc29fa..07d4f05 100644
--- a/Spear/Scene/Loader.hs
+++ b/Spear/Scene/Loader.hs
@@ -24,9 +24,7 @@ import qualified Spear.GLSL as GLSL
24import Spear.Math.Matrix3 as M3 24import Spear.Math.Matrix3 as M3
25import Spear.Math.Matrix4 as M4 25import Spear.Math.Matrix4 as M4
26import Spear.Math.MatrixUtils (fastNormalMatrix) 26import Spear.Math.MatrixUtils (fastNormalMatrix)
27import Spear.Math.Vector2 as V2 27import Spear.Math.Vector
28import Spear.Math.Vector3 as V3
29import Spear.Math.Vector4 as V4
30import Spear.Render.AnimatedModel as AM 28import Spear.Render.AnimatedModel as AM
31import Spear.Render.Material 29import Spear.Render.Material
32import Spear.Render.Program 30import Spear.Render.Program
@@ -204,27 +202,27 @@ loadModel' file rotation scale = do
204 (case scale of 202 (case scale of
205 Nothing -> Prelude.id 203 Nothing -> Prelude.id
206 Just s -> flip Model.transformVerts $ 204 Just s -> flip Model.transformVerts $
207 \(Vec3 x' y' z') -> Vec3 (V3.x s * x') (V3.y s * y') (V3.z s * z')) 205 \(Vec3 x' y' z') -> Vec3 (x s * x') (y s * y') (z s * z'))
208 206
209 (fmap transform $ Model.loadModel file) >>= setupIO . toGround 207 (fmap transform $ Model.loadModel file) >>= setupIO . toGround
210 208
211 209
212rotateModel :: Rotation -> Model -> Model 210rotateModel :: Rotation -> Model -> Model
213rotateModel (Rotation x y z order) model = 211rotateModel (Rotation ax ay az order) model =
214 let mat = case order of 212 let mat = case order of
215 XYZ -> rotZ z * rotY y * rotX x 213 XYZ -> rotZ az * rotY ay * rotX ax
216 XZY -> rotY y * rotZ z * rotX x 214 XZY -> rotY ay * rotZ az * rotX ax
217 YXZ -> rotZ z * rotX x * rotY y 215 YXZ -> rotZ az * rotX ax * rotY ay
218 YZX -> rotX x * rotZ z * rotY y 216 YZX -> rotX ax * rotZ az * rotY ay
219 ZXY -> rotY y * rotX x * rotZ z 217 ZXY -> rotY ay * rotX ax * rotZ az
220 ZYX -> rotX x * rotY y * rotZ z 218 ZYX -> rotX ax * rotY ay * rotZ az
221 normalMat = fastNormalMatrix mat 219 normalMat = fastNormalMatrix mat
222 220
223 vTransform (Vec3 x' y' z') = 221 vTransform (Vec3 x' y' z') =
224 let v = mat `M4.mulp` (vec3 x' y' z') in Vec3 (V3.x v) (V3.y v) (V3.z v) 222 let v = mat `M4.mulp` (vec3 x' y' z') in Vec3 (x v) (y v) (z v)
225 223
226 nTransform (Vec3 x' y' z') = 224 nTransform (Vec3 x' y' z') =
227 let v = normalMat `M3.mul` (vec3 x' y' z') in Vec3 (V3.x v) (V3.y v) (V3.z v) 225 let v = normalMat `M3.mul` (vec3 x' y' z') in Vec3 (x v) (y v) (z v)
228 in 226 in
229 flip Model.transformVerts vTransform . flip Model.transformNormals nTransform $ model 227 flip Model.transformVerts vTransform . flip Model.transformNormals nTransform $ model
230 228
@@ -404,7 +402,7 @@ newObject' newGO sceneRes nid props = do
404 402
405vectors :: Maybe Vector2 -> (Vector2, Vector2) 403vectors :: Maybe Vector2 -> (Vector2, Vector2)
406vectors up = case up of 404vectors up = case up of
407 Nothing -> (V2.unitx, V2.unity) 405 Nothing -> (unitx2, unity2)
408 Just u -> (perp u, u) 406 Just u -> (perp u, u)
409 407
410 408
diff --git a/Spear/Scene/SceneResources.hs b/Spear/Scene/SceneResources.hs
index ab96dc6..c2dabcf 100644
--- a/Spear/Scene/SceneResources.hs
+++ b/Spear/Scene/SceneResources.hs
@@ -20,7 +20,7 @@ where
20 20
21import Spear.Assets.Model as Model 21import Spear.Assets.Model as Model
22import Spear.GLSL as GLSL 22import Spear.GLSL as GLSL
23import Spear.Math.Vector3 23import Spear.Math.Vector
24import Spear.Render.AnimatedModel 24import Spear.Render.AnimatedModel
25import Spear.Render.Material 25import Spear.Render.Material
26import Spear.Render.Program 26import Spear.Render.Program