aboutsummaryrefslogtreecommitdiff
path: root/Spear/Physics/Rigid.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/Physics/Rigid.hs')
-rw-r--r--Spear/Physics/Rigid.hs52
1 files changed, 23 insertions, 29 deletions
diff --git a/Spear/Physics/Rigid.hs b/Spear/Physics/Rigid.hs
index 6d3c4d7..396cae4 100644
--- a/Spear/Physics/Rigid.hs
+++ b/Spear/Physics/Rigid.hs
@@ -10,9 +10,9 @@ module Spear.Physics.Rigid
10where 10where
11 11
12 12
13import qualified Spear.Math.Matrix4 as M4 13import qualified Spear.Math.Matrix3 as M3
14import Spear.Math.Spatial 14import Spear.Math.Spatial2
15import Spear.Math.Vector3 as V3 15import Spear.Math.Vector2
16import Spear.Physics.Types 16import Spear.Physics.Types
17 17
18import Data.List (foldl') 18import Data.List (foldl')
@@ -20,55 +20,49 @@ import Control.Monad.State
20 20
21 21
22data RigidBody = RigidBody 22data RigidBody = RigidBody
23 { mass :: !Float 23 { mass :: {-# UNPACK #-} !Float
24 , position :: !Vector3 24 , position :: {-# UNPACK #-} !Vector2
25 , velocity :: !Vector3 25 , velocity :: {-# UNPACK #-} !Vector2
26 , acceleration :: !Vector3 26 , acceleration :: {-# UNPACK #-} !Vector2
27 } 27 }
28 28
29 29
30instance Spatial RigidBody where 30instance 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) unitZ } 34 moveFwd speed body = body { position = position body + scale speed unity }
35 35
36 moveBack speed body = body { position = position body + scale speed unitZ } 36 moveBack speed body = body { position = position body + scale (-speed) unity }
37 37
38 strafeLeft speed body = body { position = position body + scale (-speed) unitX } 38 strafeLeft speed body = body { position = position body + scale (-speed) unitx }
39 39
40 strafeRight speed body = body { position = position body + scale speed unitX } 40 strafeRight speed body = body { position = position body + scale speed unitx }
41 41
42 pitch angle = id 42 rotate angle = id
43
44 yaw angle = id
45
46 roll angle = id
47 43
48 pos = position 44 pos = position
49 45
50 fwd _ = unitZ 46 fwd _ = unity
51
52 up _ = unitY
53 47
54 right _ = unitX 48 right _ = unitx
55 49
56 transform body = M4.transform unitX unitY unitZ $ position body 50 transform body = M3.transform unitx unity $ position body
57 51
58 setTransform transf body = body { position = M4.position transf } 52 setTransform transf body = body { position = M3.position transf }
59 53
60 setPos p body = body { position = p } 54 setPos p body = body { position = p }
61 55
62 56
63-- | Build a 'RigidBody'. 57-- | Build a 'RigidBody'.
64rigidBody :: Mass -> Position -> RigidBody 58rigidBody :: Mass -> Position -> RigidBody
65rigidBody m x = RigidBody m x V3.zero V3.zero 59rigidBody m x = RigidBody m x zero zero
66 60
67 61
68-- | Update the given 'RigidBody'. 62-- | Update the given 'RigidBody'.
69update :: [Force] -> Dt -> RigidBody -> RigidBody 63update :: [Force] -> Dt -> RigidBody -> RigidBody
70update forces dt body = 64update forces dt body =
71 let netforce = foldl' (+) V3.zero forces 65 let netforce = foldl' (+) zero forces
72 m = mass body 66 m = mass body
73 r1 = position body 67 r1 = position body
74 v1 = velocity body 68 v1 = velocity body
@@ -92,8 +86,8 @@ setAcceleration a body = body { acceleration = a }
92 86
93 87
94-- test 88-- test
95gravity = vec3 0 (-10) 0 89gravity = vec2 0 (-10)
96b0 = rigidBody 50 $ vec3 0 1000 0 90b0 = rigidBody 50 $ vec2 0 1000
97 91
98 92
99debug :: IO () 93debug :: IO ()
@@ -110,7 +104,7 @@ debug' = do
110 step $ update [gravity*50] 1 104 step $ update [gravity*50] 1
111 step $ update [gravity*50] 1 105 step $ update [gravity*50] 1
112 lift . putStrLn $ "Jumping" 106 lift . putStrLn $ "Jumping"
113 step $ update [gravity*50, vec3 0 9000 0] 1 107 step $ update [gravity*50, vec2 0 9000] 1
114 lift . putStrLn $ "Falling..." 108 lift . putStrLn $ "Falling..."
115 step $ update [gravity*50] 1 109 step $ update [gravity*50] 1
116 step $ update [gravity*50] 1 110 step $ update [gravity*50] 1
@@ -131,4 +125,4 @@ show' body =
131 ", acceleration " ++ (showVec $ acceleration body) 125 ", acceleration " ++ (showVec $ acceleration body)
132 126
133 127
134showVec v = (show $ x v) ++ ", " ++ (show $ y v) ++ ", " ++ (show $ z v) 128showVec v = (show $ x v) ++ ", " ++ (show $ y v)