summaryrefslogtreecommitdiffstats
path: root/src/Collision.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Collision.hs')
-rw-r--r--src/Collision.hs93
1 files changed, 44 insertions, 49 deletions
diff --git a/src/Collision.hs b/src/Collision.hs
index 3138473..909e7f7 100644
--- a/src/Collision.hs
+++ b/src/Collision.hs
@@ -5,68 +5,63 @@ module Collision ( collisionTankBorder
import Tank
import Game
+import Transformable
-import Data.Fixed
-import Data.Ratio
+import qualified Vector as V
+import Data.VectorSpace
-tankWidth :: Micro
+tankWidth :: Coord
tankWidth = 0.4
-tankLength :: Micro
+tankLength :: Coord
tankLength = 0.95
-bulletDiameter :: Micro
+bulletDiameter :: Coord
bulletDiameter = 0.05
-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
+collisionTankBorder :: Coord -> Coord -> Tank -> Tank
+collisionTankBorder lw lh tank = (translate dx dy) >< tank
+ where
+ corners = [ V.Vector (tankLength/2) (tankWidth/2)
+ , V.Vector (-tankLength/2) (tankWidth/2)
+ , V.Vector (-tankLength/2) (-tankWidth/2)
+ , V.Vector (tankLength/2) (-tankWidth/2)
+ ]
+
+ rotp v = V.rotateV (tankDir tank) >< v
+ transp v = V.translateV v >< tankPos tank
+
+ points = map (transp . rotp) corners
+ minx = minimum $ map V.vertexX points
+ maxx = maximum $ map V.vertexX points
+ miny = minimum $ map V.vertexY points
+ maxy = maximum $ map V.vertexY points
+
+ 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
collisionBulletBullet :: (Bullet, Bullet) -> (Bullet, Bullet) -> Bool
collisionBulletBullet (b1, b1') (b2, b2') = distancesq < (bulletDiameter^2)
- where
- distancesq = (bulletX b1' - bulletX b2')^2 + (bulletY b1' - bulletY b2')^2
+ where
+ distancesq = (bulletX b1' - bulletX b2')^2 + (bulletY b1' - bulletY b2')^2
collisionBulletTank :: (Bullet, Bullet) -> (Tank, Tank) -> Bool
collisionBulletTank (b, b') (tank, tank') = (not ((between bx minx maxx) && (between by miny maxy))) && ((between bx' minx maxx) && (between by' miny maxy))
- where
- between x a b = x >= a && x <= b
-
- dir t = (fromRational . toRational . tankDir $ t)*pi/180
- cosd t = fromRational (round ((cos $ dir t)*1000000)%1000000)
- sind t = fromRational (round ((sin $ dir t)*1000000)%1000000)
+ where
+ between x a b = x >= a && x <= b
+
+ rotp t v = V.rotateV' (tankDir t) >< v
+ transp t v = V.diffV (tankPos t) v
- rotp t (x, y) = ((cosd t)*x + (sind t)*y, -(sind t)*x + (cosd t)*y)
- transp t (x, y) = (x - tankX t, y - tankY t)
+ V.Vector bx by = (rotp tank) . (transp tank) $ bulletPos b
+ V.Vector bx' by' = (rotp tank') . (transp tank') $ bulletPos b'
- (bx, by) = (rotp tank) . (transp tank) $ (bulletX b, bulletY b)
- (bx', by') = (rotp tank') . (transp tank') $ (bulletX b', bulletY b')
-
- minx = -tankLength/2
- maxx = tankLength/2
- miny = -tankWidth/2
- maxy = tankWidth/2
+ minx = -tankLength/2
+ maxx = tankLength/2
+ miny = -tankWidth/2
+ maxy = tankWidth/2
+
+collisionTankTank :: ((Tank, Tank), (Tank, Tank)) -> ((Tank, Tank), (Tank, Tank))
+collisionTankTank ((t1, t1'), (t2, t2')) = ((t1, t1'), (t2, t2'))
+-- where
+ \ No newline at end of file