This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
htanks/src/Collision.hs
2010-03-09 22:36:22 +01:00

41 lines
1.2 KiB
Haskell

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