summaryrefslogtreecommitdiffstats
path: root/src/Collision.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Collision.hs')
-rw-r--r--src/Collision.hs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/Collision.hs b/src/Collision.hs
index 9ad898b..a69dc10 100644
--- a/src/Collision.hs
+++ b/src/Collision.hs
@@ -1,5 +1,6 @@
module Collision ( collisionTankBorder
, collisionBulletBullet
+ , collisionBulletTank
) where
import Tank
@@ -49,3 +50,23 @@ 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
+
+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)
+
+ 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)
+
+ (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