Breakout Brick Collision Detection

Started by
4 comments, last by freeworld 16 years, 1 month ago
I've been trying to develop a simple version of breakout, and I've got pretty far. I've got the paddle, and ball working along with collision detection for the walls and paddle. But what I don't have is collision detection for my bricks. I'm using an array for the bricks, like so: BRICK map[5][7]; My class for the bricks is:
[source lang=cpp]#ifndef _BRICK_H_INCLUDED
#define _BRICK_H_INCLUDED

class BRICK
{
    public:
        int x,y,type;
        int width,height; 
        BRICK();
};

BRICK::BRICK()
{
    height = 20;
    width = 71;
    //boundaries will be 5 with one between each pixel and the wall.    
}
How would I go about doing collision detection for my array of bricks?
Advertisement
I guess simply looping all your bricks and doing a 2d "is point in rectangle" collision test would be enough (with point being your ball position and rectangle your current brick). You only have to pass on bricks who are still active, I don't know if you use null value for some elements in map or you set the brick type to something else but whatever.

What might be more complex is setting the correct ball collision response. Inverting the velocity in the correct axe is an easy way to do it.
Quote:Original post by Dunge
What might be more complex is setting the correct ball collision response. Inverting the velocity in the correct axe is an easy way to do it.


Aha! But finding which axes to flip the vector based on *where* the ball hit the brick is the fun part! A simple point in rectangle will only tell you that the ball has hit the brick...which is a start I suppose. ;)
"There is no dark side of the moon." - Pink Floyd
Well, I understood that part, but how would I check if the ball was in a brick's rectangle?
try doing this(pseudocode):

foreach ( brick in game){  if (ball hit brickrectangle){destroy brick;}}
Let he who will move the world first move himself--Socrates
First determine if the brick hit and what brick, then say you want to know if the top of the ball hit the brick.

if (ball.y < (block.y+block.height) && (ball.y+ball.height) > (block.y+block.height))

then the top of the ball hit. Just think of it as what ever side hit, the opposite side shouldn't be in the brick, else you need to back step your detection to an earlier position.

hope that helps.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

This topic is closed to new replies.

Advertisement