2021-01-02 12:32:20 +00:00
|
|
|
{-# LANGUAGE AllowAmbiguousTypes #-}
|
2020-12-27 04:19:51 +00:00
|
|
|
module Classes.Physics.Collidible where
|
|
|
|
|
|
|
|
import Linear
|
|
|
|
|
2020-12-27 04:30:57 +00:00
|
|
|
-- internal imports
|
|
|
|
|
|
|
|
import Classes.Physics.Mass
|
|
|
|
|
2020-12-27 04:19:51 +00:00
|
|
|
-- | Typeclass for implementing collision results on objects.
|
2021-01-02 12:32:20 +00:00
|
|
|
class (Show c, Mass c) => Collidible c where
|
2020-12-27 04:19:51 +00:00
|
|
|
|
2021-01-02 12:32:20 +00:00
|
|
|
-- | returns the bottom left and top right corners relative to the objects
|
|
|
|
-- positional vector of the axis aligned bounding box (AABB) serving here
|
|
|
|
-- as collision boundaries.
|
2020-12-27 04:19:51 +00:00
|
|
|
boundary
|
2021-01-02 12:32:20 +00:00
|
|
|
:: c -- ^ Object
|
|
|
|
-> ( V2 Double -- ^ Bottom left corner of AABB relative to position
|
|
|
|
, V2 Double -- ^ Top right corner of AABB relative to position
|
2020-12-27 04:19:51 +00:00
|
|
|
)
|
2021-01-02 12:32:20 +00:00
|
|
|
|
|
|
|
collisionCheck
|
|
|
|
:: (Collidible other)
|
2021-01-02 22:21:01 +00:00
|
|
|
=> Double -- ^ Time step length
|
2021-01-02 21:46:46 +00:00
|
|
|
-> c -- ^ First object
|
|
|
|
-> other -- ^ second object
|
|
|
|
-> Bool -- ^ Do the objects collide?
|
|
|
|
collisionCheck dt m1 m2 =
|
|
|
|
let (V2 m1x1 m1y1) = position m1 + fst (boundary m1) + delta1
|
|
|
|
(V2 m1x2 m1y2) = position m1 + snd (boundary m1) + delta1
|
|
|
|
(V2 m2x1 m2y1) = position m2 + fst (boundary m2) + delta2
|
|
|
|
(V2 m2x2 m2y2) = position m2 + snd (boundary m2) + delta2
|
|
|
|
delta1@(V2 vx1 vy1) = (dt *) <$> velocity m1
|
2021-01-03 00:43:37 +00:00
|
|
|
delta2 = (dt *) <$> velocity m2
|
2021-01-02 21:46:46 +00:00
|
|
|
dtx 0 = dt
|
|
|
|
dtx vx =
|
2021-01-02 22:17:53 +00:00
|
|
|
if vx > 0
|
2021-01-02 21:46:46 +00:00
|
|
|
then (m2x1 - m1x2) / vx
|
|
|
|
else (m1x1 - m2x2) / (-vx)
|
|
|
|
dty 0 = dt
|
|
|
|
dty vy =
|
2021-01-02 22:17:53 +00:00
|
|
|
if vy > 0
|
2021-01-02 21:46:46 +00:00
|
|
|
then (m2y1 - m1y2) / vy
|
|
|
|
else (m1y1 - m2y2) / (-vy)
|
|
|
|
posColl =
|
|
|
|
or
|
|
|
|
[ m1x1 < m2x2 && m1x1 > m2x1
|
|
|
|
, m1x2 < m2x2 && m1x2 > m2x1
|
|
|
|
, m2x1 < m1x2 && m2x1 > m1x1
|
|
|
|
, m2x2 < m1x2 && m2x2 > m1x1
|
|
|
|
] && or
|
|
|
|
[ m1y1 < m2y2 && m1y1 > m2y1
|
|
|
|
, m1y2 < m2y2 && m1y2 > m2y1
|
|
|
|
, m2y1 < m1y2 && m2y1 > m1y1
|
|
|
|
, m2y2 < m1y2 && m2y2 > m1y1
|
|
|
|
]
|
2021-01-02 12:32:20 +00:00
|
|
|
in
|
2021-01-03 00:43:37 +00:00
|
|
|
-- (if vx1 == 0 then posColl else dt > dtx vx1)
|
|
|
|
(dt > dtx vx1 || posColl)
|
2021-01-02 21:46:46 +00:00
|
|
|
&&
|
2021-01-03 00:43:37 +00:00
|
|
|
-- (if vy1 == 0 then posColl else dt > dty vy1)
|
|
|
|
(dt > dty vy1 || posColl)
|
2021-01-02 12:32:20 +00:00
|
|
|
|
|
|
|
-- | This Function is called for every collision on both colliding objects.
|
2020-12-27 04:19:51 +00:00
|
|
|
collide
|
2021-01-02 12:32:20 +00:00
|
|
|
:: (Collidible other)
|
|
|
|
=> c -- ^ Original object
|
|
|
|
-> other -- ^ Collision partner
|
|
|
|
-> c -- ^ Updated original object
|
|
|
|
collide = elasticCollision 0.9
|
|
|
|
|
|
|
|
-- | Implementation of a dampened elastic collision used as default collision
|
|
|
|
-- implementation of the collision reaction
|
|
|
|
elasticCollision
|
|
|
|
:: (Collidible c1, Collidible c2)
|
|
|
|
=> Double
|
|
|
|
-> c1
|
|
|
|
-> c2
|
|
|
|
-> c1
|
|
|
|
elasticCollision damping mo1 mo2 =
|
|
|
|
let (V2 v1x v1y) = velocity mo1
|
|
|
|
(V2 v2x v2y) = velocity mo2
|
2021-01-03 00:43:37 +00:00
|
|
|
p1@(V2 p1x p1y) = position mo1
|
|
|
|
p2 = position mo2
|
|
|
|
(V2 m1x1 m1y1, V2 m1x2 m1y2) = boundary mo1
|
|
|
|
(V2 m2x1 m2y1, V2 m2x2 m2y2) = boundary mo2
|
2021-01-02 12:32:20 +00:00
|
|
|
m1 = mass mo1
|
|
|
|
m2 = mass mo2
|
|
|
|
v1x' = 2 * (m1 * v1x + m2 * v2x) / (m1 + m2) - v1x
|
|
|
|
v1y' = 2 * (m1 * v1y + m2 * v2y) / (m1 + m2) - v1y
|
2021-01-03 00:43:37 +00:00
|
|
|
(V2 dx dy) = p2 - p1
|
|
|
|
np = V2
|
|
|
|
( p1x + if v1x == 0
|
|
|
|
then 0
|
|
|
|
else
|
|
|
|
if dx < 0
|
|
|
|
then (abs m2x1 + abs m1x2 - abs dx)
|
|
|
|
else - (abs m2x2 + abs m1x1 - abs dx)
|
|
|
|
)
|
|
|
|
( p1y + if v1y == 0
|
|
|
|
then 0
|
|
|
|
else
|
|
|
|
if dy < 0
|
|
|
|
then (abs m2y1 + abs m1y2 - abs dy)
|
|
|
|
else - (abs m2y2 + abs m1y1 - abs dy)
|
|
|
|
)
|
2021-01-02 12:32:20 +00:00
|
|
|
in
|
2021-01-03 00:43:37 +00:00
|
|
|
(velocityUpdater ((positionUpdater mo1) np))
|
2021-01-02 12:32:20 +00:00
|
|
|
(if m1 == recip 0
|
|
|
|
then V2 0 0
|
|
|
|
else (damping *) <$>
|
|
|
|
if m2 == recip 0
|
|
|
|
then negate <$> velocity mo1
|
|
|
|
else (V2 v1x' v1y')
|
|
|
|
)
|