2d collision detection trouble

Nibbie
Posts: 2
Joined: 2010.06
Post: #1
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?

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

-(void)onTimer{
    if(playing){
        if(platformView.MOVED==YES) [platformView refresh:platform];
        
        [ball update];
        [ballView refresh:ball];
    
        if(ball.position.y+ball.radius>=platform.position.y)
            [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);
                
            if(brickView.life==2){
                [brickView setBackgroundColor:[UIColor orangeColor]];
            }
            else if(brickView.life==1){
                [brickView setBackgroundColor:[UIColor yellowColor]];
            }
            else if(brickView.life==0){
                [brickView removeFromSuperview];
                brickView.position = CGPointMake(0, 0);
                brickView.width = 0;
                brickView.height = 0;
            }
            }
        }
    }

    
    if(ball.position.y>=460){
        playing=NO;
        }
}

///////////////////////
// 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;
        
            aBrick.life--;
        
            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);
        
        
        if((balPos.x-aBall.radius)>=(brickPos.x+aBrick.width)-1){
            balPos.x = brickPos.x+aBrick.width-1;
            balVel.x*=-1;
        }
        else if((balPos.x+aBall.radius)<=brickPos.x+1){
            balPos.x = brickPos.x+1;    
            balVel.y*=-1;
        }
        else{
            balVel.y*=-1;
        }
        
        aBall.position = balPos;
        aBall.velocity = balVel;
        [aBall update];
            
        return YES;
    }
}

Thanks a lot guys,
Steve
Quote this message in a reply
Moderator
Posts: 452
Joined: 2008.04
Post: #2
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.

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2010.06
Post: #3
I was able to fix the problem. I guess I was just overthinking everything. Here's the working code:

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
        else{  
            balVel.y*=-1;   //Change y velocity
        }
        
        //Update ball position using new velocity
        aBall.velocity = balVel;
        [aBall update];
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  collision detection godzilla 4 4,079 Mar 24, 2012 06:57 PM
Last Post: SethWillits
  Collision detection using the Bullet physics engine MikeD 0 2,891 Aug 30, 2010 11:14 AM
Last Post: MikeD