light 0.2.0.0 → 0.2.0.1
raw patch · 3 files changed
+27/−13 lines, 3 filesdep +QuickCheckPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: QuickCheck
API changes (from Hackage documentation)
- Physics.Light: [_acceleration] :: Object2D -> V3 Double
- Physics.Light: acceleration :: Lens' Object2D (V3 Double)
+ Physics.Light: localToWorld :: V3 Double -> V3 Double -> V3 Double
+ Physics.Light: updateObject2D :: Double -> Object2D -> Object2D
+ Physics.Light: worldToLocal :: V3 Double -> V3 Double -> V3 Double
- Physics.Light: Object2D :: V3 Double -> V3 Double -> V3 Double -> [Convex] -> Object2D
+ Physics.Light: Object2D :: V3 Double -> V3 Double -> [Convex] -> Object2D
Files
- light.cabal +2/−1
- src/Physics/Light.hs +12/−7
- test/Spec.hs +13/−5
light.cabal view
@@ -1,5 +1,5 @@ name: light-version: 0.2.0.0+version: 0.2.0.1 synopsis: a simple physics engine description: A simple physics engine(dynamics only) with collision dection homepage: https://github.com/suzumiyasmith/light#readme@@ -34,6 +34,7 @@ , linear , lens , containers+ , QuickCheck ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010
src/Physics/Light.hs view
@@ -5,6 +5,7 @@ import GJK import Control.Lens+import Control.Arrow import Control.Monad.Writer.Lazy import Data.Fixed import Data.Function@@ -15,14 +16,13 @@ data Object2D = Object2D { _position :: V3 Double , _velocity :: V3 Double- , _acceleration :: V3 Double , _shape :: [Convex] } makeLenses ''Object2D instance Show Object2D where- show o = show (o ^. position, o ^. velocity, o ^. acceleration)+ show o = show (o ^. position, o ^. velocity) type PhysicsWorld = (IM.IntMap Object2D, Int) @@ -37,12 +37,11 @@ addObjects [] w = w update :: Double -> IM.IntMap Object2D -> IM.IntMap Object2D-update dt = fmap w- where- w :: Object2D -> Object2D- w o = o+update dt = fmap $ updateObject2D dt++updateObject2D :: Double -> Object2D -> Object2D+updateObject2D dt o = o & position %~ (+ o ^. velocity ^* dt)- & velocity %~ (+ o ^. acceleration ^* dt) & position . _z %~ (`mod'` (2 * pi)) detectCollision :: IM.IntMap Object2D -> [(Int, Int)]@@ -81,3 +80,9 @@ rotateMatrix2D :: Double -> V2 (V2 Double) rotateMatrix2D r = V2 (V2 (cos r) (-sin r)) (V2 (sin r) (cos r))++worldToLocal :: V3 Double -> V3 Double -> V3 Double+worldToLocal o = (flip (-) o) >>> (_xy %~ (rotateMatrix2D (o ^. _z) !*))++localToWorld :: V3 Double -> V3 Double -> V3 Double+localToWorld o = (+ o) <<< (_xy %~ (rotateMatrix2D (- o ^. _z) !*))
test/Spec.hs view
@@ -1,24 +1,32 @@ import Control.Lens import Control.Arrow-import qualified Data.IntMap.Lazy as IM+import Control.Monad import Linear import Physics.Light+import Test.QuickCheck main :: IO () main = do print $ (detectCollision . fst) <$> (updateTest <$> [1 .. 1000] <*> [testWorld1, testWorld2])+ quickCheck (\(o, w) -> 0.00001 > norm (worldToLocal o (localToWorld o w) - w)) +instance Arbitrary a => Arbitrary (V3 a) where+ arbitrary = liftM3 V3 arbitrary arbitrary arbitrary+ shrink v = [ v & _x .~ x' | x' <- shrink $ v ^. _x ]+ ++ [ v & _y .~ y' | y' <- shrink $ v ^. _y ]+ ++ [ v & _z .~ z' | z' <- shrink $ v ^. _z ]+ testShape1 = [circle 1, ellipse 0.5 3] testShape2 = polygon $ V2 <$> [-1, 1] <*> [-1, 1] -o1 = Object2D 0 (V3 0 0 1) 0 testShape1+o1 = Object2D 0 (V3 0 0 1) testShape1 -o2 = Object2D (V3 1.5 0 0) (V3 0 0 0) 0 [testShape2]+o2 = Object2D (V3 1.5 0 0) (V3 0 0 0) [testShape2] -o3 = Object2D (V3 (-1.5) 0 0) (V3 0 0 0) 0 [testShape2]+o3 = Object2D (V3 (-1.5) 0 0) (V3 0 0 0) [testShape2] -o4 = Object2D (V3 (-1.5) 0 0) (V3 1 0 0) 0 [testShape2]+o4 = Object2D (V3 (-1.5) 0 0) (V3 1 0 0) [testShape2] testWorld1 = newPhysicsWorld & addObjects [o1, o2, o3] testWorld2 = newPhysicsWorld & addObjects [o1, o3, o4]