iDevGames Forums
2d collision detection trouble - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: 2d collision detection trouble (/thread-35.html)

2d collision detection trouble - stevemack - Jun 23, 2010 08:40 AM

Hey guys,

I'm working on a brick breaker game for the iPhone, and I've run into a little trouble. Right now I can draw bricks to the screen, move the paddle, launch the ball, etc.

The collision detection works fine with ball->paddle, but with ball->brick there are some issues.

When the ball collides with the side of a brick and is also touching another brick, it pretty much plows through the brick instead of bouncing off (the x-velocity is supposed to *= -1).

My code is included below - would you mind looking through it to see if I can make any changes to fix this problem?

// This code runs 30 time/second
// It checks for collisions
// and updates views on the screen
// Located in main view controller

        if(platformView.MOVED==YES) [platformView refresh:platform];
        [ball update];
        [ballView refresh:ball];
            [ball checkCollisions:ball withPlatform:platform];
        for(NSInteger i=0; i<=level1.numBricks; i++){
            brickView = [level1.bricks objectAtIndex:i];
            if([ball checkCollisions:ball withBrick:brickView]==YES){
                NSLog(@"%d", i);
                [brickView setBackgroundColor:[UIColor orangeColor]];
            else if({
                [brickView setBackgroundColor:[UIColor yellowColor]];
            else if({
                [brickView removeFromSuperview];
                brickView.position = CGPointMake(0, 0);
                brickView.width = 0;
                brickView.height = 0;


// This is the collision detection code
// Located in the "Ball.m" ball object class

-(BOOL)checkCollisions:(Ball *)aBall withBrick:(BrickView *)aBrick
    if(CGRectIntersectsRect([aBall getRect],[aBrick getRect])){
            CGPoint balVel = aBall.velocity;
            CGPoint balPos = aBall.position;
            CGPoint brickPos = aBrick.position;
            NSLog(@"balPos.x %f", balPos.x);
            NSLog(@"-rad %f", balPos.x-aBall.radius);
            NSLog(@"+rad %f", balPos.x+aBall.radius);
            NSLog(@"brickPos.x %f",brickPos.x);
            NSLog(@"+width %f",brickPos.x+aBrick.width);
            balPos.x = brickPos.x+aBrick.width-1;
        else if((balPos.x+aBall.radius)<=brickPos.x+1){
            balPos.x = brickPos.x+1;    
        aBall.position = balPos;
        aBall.velocity = balVel;
        [aBall update];
        return YES;

Thanks a lot guys,

2d collision detection trouble - AndyKorth - Jun 23, 2010 11:30 AM

Quote: if((balPos.x-aBall.radius)>=(brickPos.x+aBrick.width)-1){

I think you'll need to consider what these values are depending on if the ball comes from the left or right side.

2d collision detection trouble - stevemack - Jun 23, 2010 11:50 AM

I was able to fix the problem. I guess I was just overthinking everything. Here's the working code:

//Check if ball is colliding with either side of the brick
if(balPos.x>brickPos.x+aBrick.width || balPos.x<brickPos.x){
            balVel.x*=-1;  //Change x velocity
               //If ball is colliding with top or bottom of brick
            balVel.y*=-1;   //Change y velocity
        //Update ball position using new velocity
        aBall.velocity = balVel;
        [aBall update];