diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2010-03-09 22:36:22 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2010-03-09 22:36:22 +0100 |
commit | e85dc20a4869c91faa3869695d2d19bfe07f9abc (patch) | |
tree | ec4c2ae2d5d40c96065782a46fb4f45fc11dbfe1 /src/Collision.hs | |
parent | f3d9814ad5e7efe7aa883b1c58b854a574c0cf61 (diff) | |
download | htanks-e85dc20a4869c91faa3869695d2d19bfe07f9abc.tar htanks-e85dc20a4869c91faa3869695d2d19bfe07f9abc.zip |
Nice tank-border collision detection
Diffstat (limited to 'src/Collision.hs')
-rw-r--r-- | src/Collision.hs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/Collision.hs b/src/Collision.hs new file mode 100644 index 0000000..d29738a --- /dev/null +++ b/src/Collision.hs @@ -0,0 +1,41 @@ +module Collision ( collisionTankBorder + ) where + +import Game + +import Data.Fixed +import Data.Ratio + +tankWidth :: Micro +tankWidth = 0.95 + +tankLength :: Micro +tankLength = 0.95 + +collisionTankBorder :: Micro -> Micro -> Tank -> Tank +collisionTankBorder lw lh tank = tank {tankX = newx, tankY = newy} + where + dir = (fromRational . toRational . tankDir $ tank)*pi/180 + cosd = fromRational (round ((cos dir)*1000000)%1000000) + sind = fromRational (round ((sin dir)*1000000)%1000000) + + points = [ (tankLength/2, tankWidth/2) + , (-tankLength/2, tankWidth/2) + , (-tankLength/2, -tankWidth/2) + , (tankLength/2, -tankWidth/2) + ] + + rotp (x, y) = (cosd*x - sind*y, sind*x + cosd*y) + transp (x, y) = (x + tankX tank, y + tankY tank) + + pointst = map (transp . rotp) points + minx = minimum $ map fst pointst + maxx = maximum $ map fst pointst + miny = minimum $ map snd pointst + maxy = maximum $ map snd pointst + + dx = if minx < 0 then (-minx) else if maxx > lw then (-maxx+lw) else 0 + dy = if miny < 0 then (-miny) else if maxy > lh then (-maxy+lh) else 0 + + newx = (tankX tank) + dx + newy = (tankY tank) + dy |