diff options
author | 3gg <3gg@shellblade.net> | 2022-09-18 17:47:37 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2022-09-18 17:47:37 -0700 |
commit | 3027010b87e30bc1546ff48b9e5011fa3b3e8fb6 (patch) | |
tree | a6c7920e3c1dd4a49bcb4e5f04046ac6db862b3d | |
parent | d81c62adbc955855438f1626c685e92794017d2d (diff) |
Slightly better collision handling.
-rw-r--r-- | demos/pong/Pong.hs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/demos/pong/Pong.hs b/demos/pong/Pong.hs index 906e89b..accc75d 100644 --- a/demos/pong/Pong.hs +++ b/demos/pong/Pong.hs | |||
@@ -76,16 +76,17 @@ newWorld = | |||
76 | stepBall vel = collideBall vel .> moveBall | 76 | stepBall vel = collideBall vel .> moveBall |
77 | 77 | ||
78 | collideBall :: Vector2 -> Step [GameObject] e GameObject (Vector2, GameObject) | 78 | collideBall :: Vector2 -> Step [GameObject] e GameObject (Vector2, GameObject) |
79 | collideBall vel = step $ \_ _ gos _ ball -> | 79 | collideBall vel = step $ \_ dt gos _ ball -> |
80 | let (AABB2 pmin pmax) = aabb ball `aabbAdd` pos ball | 80 | let (AABB2 pmin pmax) = aabb ball `aabbAdd` pos ball |
81 | collideCol = x pmin < 0 || x pmax > 1 | 81 | collideCol = x pmin < 0 || x pmax > 1 |
82 | collideRow = | 82 | collideRow = y pmin < 0 || y pmax > 1 || any (collide ball) (tail gos) |
83 | y pmin < 0 || y pmax > 1 | ||
84 | || any (collide ball) (tail gos) | ||
85 | negx v@(Vector2 x y) = if collideCol then vec2 (- x) y else v | 83 | negx v@(Vector2 x y) = if collideCol then vec2 (- x) y else v |
86 | negy v@(Vector2 x y) = if collideRow then vec2 x (- y) else v | 84 | negy v@(Vector2 x y) = if collideRow then vec2 x (- y) else v |
87 | vel' = negx . negy $ vel | 85 | vel' = negx . negy $ vel |
88 | in ((vel', ball), collideBall vel') | 86 | delta = dt -- A small delta to apply when collision occurs. |
87 | adjustX = if collideCol then scale delta (vec2 (x vel) 0) else vec2 0 0 | ||
88 | adjustY = if collideRow then scale delta (vec2 0 (y vel)) else vec2 0 0 | ||
89 | in ((vel' + adjustX + adjustY, ball), collideBall vel') | ||
89 | 90 | ||
90 | collide go1 go2 = | 91 | collide go1 go2 = |
91 | let (AABB2 (Vector2 xmin1 ymin1) (Vector2 xmax1 ymax1)) = | 92 | let (AABB2 (Vector2 xmin1 ymin1) (Vector2 xmax1 ymax1)) = |