Added function to check for collision of rectangles.

This commit is contained in:
John Shaver 2017-06-28 14:18:55 -07:00
parent 6ce1e7c15b
commit 77fdf01d92
2 changed files with 27 additions and 1 deletions

View file

@ -39,3 +39,8 @@ distanceFromSpeed pixPerSec millis =
fmod : Float -> Float -> Float fmod : Float -> Float -> Float
fmod lh rh = fmod lh rh =
lh - (toFloat (floor (lh / rh)) * rh) lh - (toFloat (floor (lh / rh)) * rh)
rectToRectCollision : ( Point, Point ) -> ( Point, Point ) -> Bool
rectToRectCollision ( ( ax1, ay1 ), ( ax2, ay2 ) ) ( ( bx1, by1 ), ( bx2, by2 ) ) =
ax1 < bx2 && ax2 > bx1 && ay1 < by2 && ay2 > by1

View file

@ -2,7 +2,14 @@ module Tests exposing (..)
import Test exposing (..) import Test exposing (..)
import Expect import Expect
import AnimationHelpers exposing (calculateAnimation, movePosition, distanceFromSpeed, fmod) import AnimationHelpers
exposing
( calculateAnimation
, movePosition
, distanceFromSpeed
, fmod
, rectToRectCollision
)
trimFloatX : Int -> Float -> Float trimFloatX : Int -> Float -> Float
@ -107,4 +114,18 @@ all =
(fmod 51934 123) (fmod 51934 123)
28 28
] ]
, describe "Rectangle collision testing"
[ test "No collision" <|
\() ->
Expect.false "Expect rectangles not to collide" <|
rectToRectCollision
( ( 10, 10 ), ( 20, 30 ) )
( ( 30, 50 ), ( 40, 60 ) )
, test "Collide left" <|
\() ->
Expect.true "Expect rectangles to collide" <|
rectToRectCollision
( ( 30, 10 ), ( 50, 30 ) )
( ( 10, 10 ), ( 40, 30 ) )
]
] ]