breakout collision

This is a discussion on breakout collision within the Game Programming forums, part of the General Programming Boards category; I'm writing collision code for my breakout clone. Thanks to the people at allegro.cc I have gotten this far. Now ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    breakout collision

    I'm writing collision code for my breakout clone. Thanks to the people at allegro.cc I have gotten this far. Now I need more help with collision detection. I can find out if the ball is within the block and have it bounce off the left side, but that's all. anymore help?

    Code:
       for(x =0; x < MAX_ROW; x++)
       {
          for(y = 0; y < MAX_COL; y++)
         {
            if (ball.x >  blocks[x][y].x && 
                ball.x <= blocks[x][y].width &&
                ball.y >  blocks[x][y].y && 
                ball.y <= blocks[x][y].height) 
           {
                if(blocks[x][y].color > 0)
                    {
                        paddle.score += blocks[x][y].color*10;
                        blocks[x][y].color = 0;
                       if(ball.x > blocks[x][y].x)	
                          ball.dx = -BALL_DELTA; // bounce left
                       else if(ball.x <= blocks[x][y].width)
                               ball.dx = BALL_DELTA; // bounce right
                       else if(ball.y > blocks[x][y].y)
                               ball.dy = -BALL_DELTA; // bounce up
                       else if(ball.y <= blocks[x][y].height)
                               ball.dy = BALL_DELTA; // bounce down
                       numOfBlocks--;
                    }
    			}
    		}
    	}
    Last edited by lambs4; 03-08-2003 at 10:37 AM.

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: breakout collision

    Originally posted by lambs4

    Code:
            if (ball.x >  blocks[x][y].x && 
                ball.x <= blocks[x][y].width &&
                ball.y >  blocks[x][y].y && 
                ball.y <= blocks[x][y].height)
    should be:
    Code:
            if (ball.x >  blocks[x][y].x && 
                ball.x <= blocks[x][y].x + blocks[x][y].width &&
                ball.y >  blocks[x][y].y && 
                ball.y <= blocks[x][y].y+blocks[x][y].height)
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I do add the blocks x and width when I create the levels.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Then you shouldn't call the variable 'width' -- it is very confusing.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    if(ball.x > blocks[x][y].x)
    That condition will always be true. Try to follow and interpret your code.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Ok, I've gotten the collision code written for the blocks and and paddle. The new problem is that the ball gets stuck bouncing within the paddle. Any reason why this occurr?

    Code:
    void checkCollision(void)
    {
       int x,y,i;
       // check ball and block collision
       for(x =0; x < MAX_ROW; x++)
      {
         for(y = 0; y < MAX_COL; y++)
        {
           if (ball.x > blocks[x][y].x && 
              ball.x <= blocks[x][y].x+blocks[x][y].width && 
              ball.y > blocks[x][y].y && 
              ball.y <= blocks[x][y].y+blocks[x][y].height)		
          {				
            if(blocks[x][y].color > 0)
           {
             if(ball.x+ball.width >= blocks[x][y].x+blocks[x][y].width)
               ball.dx = -ball.dx;			
             if(ball.x+ball.width <= blocks[x][y].x+blocks[x][y].width)
               ball.dx = ball.dx;		
    
             ball.dy = -ball.dy;				         
             
             paddle.score += blocks[x][y].color*10;
             blocks[x][y].color = 0;
             numOfBlocks--; 			
             playSound(BALLHITBLOCK);
           }
         }				
       }
    }
    // check ball and paddle collision
    	if (ball.x > paddle.x &&
    	ball.x <= paddle.x+paddle.width &&
    	ball.y > paddle.y &&
    	ball.y <= paddle.y+paddle.height)		
    	{
    		if(ball.x >= paddle.x+paddle.width)
    			ball.dx = -ball.dx;			
    		if(ball.x <= paddle.x+paddle.width)
    			ball.dx = ball.dx;		
    
    	  ball.dy = -ball.dy;
    	  playSound(BALLHITPADDLE);
    	}	
    	
    }
    Last edited by lambs4; 03-13-2003 at 03:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some collision handling fun
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 04-13-2008, 09:45 AM
  2. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 03:25 PM
  3. Breakout Collision Detection Algorithm Help
    By xmltorrent in forum Game Programming
    Replies: 8
    Last Post: 08-24-2006, 03:32 PM
  4. breakout style game collision detection
    By m00se123 in forum Game Programming
    Replies: 6
    Last Post: 10-31-2002, 09:18 AM
  5. Replies: 4
    Last Post: 05-03-2002, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21