From 83f0606ea9dfd6b493097dc97330055dff4a2867 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 24 Jun 2011 21:50:32 +0200 Subject: Added Transformable class to simplify collision calculation --- src/Transformable.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Transformable.hs (limited to 'src/Transformable.hs') diff --git a/src/Transformable.hs b/src/Transformable.hs new file mode 100644 index 0000000..2fd64fe --- /dev/null +++ b/src/Transformable.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE TypeOperators, TypeSynonymInstances #-} + +module Transformable ( Coord + , Vector3 + , Transform + , Transformable(..) + , translate + , rotate + , scale + ) where + +import Data.LinearMap + +type Coord = Double + +type Vector3 = (Coord, Coord, Coord) +type Transform = Vector3 :-* Vector3 + +class Transformable a where + (><) :: Transform -> a -> a + +instance Transformable Transform where + t1 >< t2 = t1 *.* t2 + +instance Transformable Vector3 where + t >< v = t `lapply` v + +translate :: Coord -> Coord -> Transform +translate dx dy = linear $ \(x, y, w) -> (x + w*dx, y + w*dy, w) + +rotate :: Coord -> Transform +rotate a = linear $ \(x, y, w) -> (c*x - s*y, s*x + c*y, w) where + c = cos a + s = sin a + +scale :: Coord -> Transform +scale s = linear $ \(x, y, w) -> (s*y, s*y, w) -- cgit v1.2.3