Skip to content
Browse files

Fix bug in collision

* Only bounce if ball moving in direction of intersecting paddle/wall
  • Loading branch information...
1 parent 0a9d4d5 commit 8dad17a2b51425fd1dbcfa7e93c5111fc295b90b @jakeboxer committed
Showing with 8 additions and 4 deletions.
  1. +8 −4 Pong/HelloWorldLayer.m
View
12 Pong/HelloWorldLayer.m
@@ -106,13 +106,17 @@ - (void)update:(ccTime)dt {
- (void)updateGameObjects:(ccTime)dt {
// Check for ball containment
if (!CGRectContainsRect(self.boundingBox, self.ball.sprite.boundingBox)) {
- // Ball collided with a top/bottom wall
- [self.ball flipVelocityY];
+ if ((self.ball.sprite.topY >= self.contentSize.height && self.ball.velocity.y >= 0) ||
+ (self.ball.sprite.bottomY <= 0 && self.ball.velocity.y <= 0)) {
+ // Ball collided with a top/bottom wall and is moving in that wall's direction.
+ [self.ball flipVelocityY];
+ }
}
// Check for ball collisions.
- if (CGRectIntersectsRect(self.ball.sprite.boundingBox, self.paddle1.boundingBox) || CGRectIntersectsRect(self.ball.sprite.boundingBox, self.paddle2.boundingBox)) {
- // Ball collided with a paddle
+ if ((CGRectIntersectsRect(self.ball.sprite.boundingBox, self.paddle1.boundingBox) && self.ball.velocity.x <= 0) ||
+ (CGRectIntersectsRect(self.ball.sprite.boundingBox, self.paddle2.boundingBox) && self.ball.velocity.x >= 0)) {
+ // Ball collided with a paddle and is moving in the paddle's direction.
[self.ball flipVelocityX];
}

0 comments on commit 8dad17a

Please sign in to comment.
Something went wrong with that request. Please try again.