Skip to content

Commit

Permalink
ball can now collide with bottom of block
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Ly committed Nov 13, 2012
1 parent 081a54f commit 3222c63
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/breakout/game.clj
Expand Up @@ -115,8 +115,11 @@
{paddle-width :width, paddle-height :height} :size} paddle
{{ball-x :x, ball-y :y} :origin,
{ball-vx :x, ball-vy :y} :velocity} ball
ball-next-y (+ ball-y ball-vy)
within-y? (between? ball-y paddle-y ball-next-y)]
ball-next-y (+ ball-y ball-vy)
paddle-y-max (+ paddle-y paddle-height)
within-y? (or
(between? ball-y paddle-y ball-next-y)
(between? ball-y paddle-y-max ball-next-y))]

(if (not within-y?)
;; we can stop right here since the ball won't collide with something
Expand Down Expand Up @@ -144,9 +147,10 @@
(println (str "The ball collided with the rect:\nrect=" rect " ball=" ball))
;; when the ball collides, change the velocity to be upwards
(let [{{x :x, y :y} :origin, {vx :x, vy :y} :velocity} ball
{{rect-y :y} :origin} rect
{{rect-y :y} :origin {rect-width :width} :size} rect
collide-y (if (>= vy 0) (+ rect-y rect-width) rect-y)
anticipated-y (+ y vy)
delta-y (- anticipated-y rect-y)
delta-y (- anticipated-y collide-y)
new-vy (- vy)
new-velocity {:x vx :y new-vy}
new-y (+ y delta-y)
Expand Down

0 comments on commit 3222c63

Please sign in to comment.