Collision detection in Breakout

Started by
9 comments, last by Tran Minh Quang 21 years, 1 month ago
Can anyone tell me how to do collision detection in Breakout game (the ball hits the bricks) Thanks
Advertisement
many different ways to achieve this..
the easiest and simplest for a beginner, would be to either
(A) Create an array for your blocks, each one containing your own user defined struct (with topleft x, y and width and height values). Then you can just loop through your array, testing a collision based on these values. Not very efficient, but easy, and with breakout clones, wont see a differnce in speed.

(B) A little harder but alot mroe efficient, is to implement a very basic bounding box collision detection. Where every object is enclosed in the smallest square possible (blocks and ball and paddle) then check for a collision based on ball center + length of square touching or inside the area of another objects square.

Array, shold be self explanitory, if your confused on the bounding boxes, theres TONS of articles here on Gamedev about it. heres just the 2 first simple bounding boxes links
First Link
Second Link
<=- Talon -=>
There are plenty of articles on collision detection on this very site

Click ''Articles and Resources''.

Ant
>
I''m assuming that your bricks are not moving (but the paddle and enemies are).

The easiest way (work with multiple balls, etc..) is to create an array:

map[10][10];

Each brick has an element in the array.

brick5 = map[6][2] = 5

You ball moves through the map, but, you don''t want a jerky game, so you translate your balls on screen position in to a position inside of the map array.

ie:

screenWidth = 320;
screenHeight = 240;
mapWidth = 10;
mapHeight = 10;

ballsXinTheArray = ballX / mapWidth;
ballsYInTheArray = ballY / mapHeight;

Then check if map[ballsXinTheArray][ballsYinTheArray] contains something.

You can also use the array to manage bouncing enemies, lazers from your paddle, etc..

To test for enemies hiting your ball / paddle, you just run a check for each enemy, using a bounding box, because there probably aren''t that many of them..

If you''re really intersted in accurate 2D collision detection:

Everytime you draw something to the screen that can be hit, draw a 1 bit version of it (ie: every hitable pixel = 1, every non-hitable pixel = 0) in to a bitmap. We''ll call this a collision bitmap. what you effectivley done is created a image of the screen (in black and white) that shows areas that are clear of hittable things).

Check the balls position inside of the the collision bitmap and bobs your uncle.
If you want to keep track of whats been hit, instead of using a 1 bit image, you use a regular bitmap (256 colors should do). Everytime you draw an enemy/brick in to the collision bitmap, you use an index number for that enemy / brick as the color. Then, when your ball hits ''something'' you know what it is by the ''color'' that the balls hitting.

Good luck,
Will
------------------http://www.nentari.com
i was thinking about making an breakout game (well an arkanoid clone, one of my favourite games, must have spent a fortune in the arcade when i was a kid)

i was just wondering how you would implement collision detection in a game where frame rate independent movement is used.

for example on a slower system it might be possible for a ball to move a distance larger than the height/width of a block, this will make it possible for the ball to pass through blocks

[IMG]http://homepage.ntlworld.com/treanor/pic.gif[/IMG] <--i give up

what would be the best way to solve this problem ? i initially thought about steping along the path, and checking collisions every so often to find the earliest collision, but this seems like a very inefficient method.



[edited by - deal on March 4, 2003 9:27:12 PM]

[edited by - deal on March 4, 2003 9:27:46 PM]

[edited by - deal on March 4, 2003 9:28:50 PM]
The paddle is fixed to the bottom, right? So, just do a check to see if the ball is within the paddle''s left and right boundaries, but has already gone below the paddle.

You probably won''t need to worry about this unless the framerates are really high. If they are around 30-50 (which they should be, at least), the ball will be updated 30 to 50 times a second, moving very slightly, so the chances of the ball jumping over the paddle are slim.

|.dev-c++.|.the gimp.|.seti@home.|.dbpoweramp.|.torn.|.=w=.|
You wont ever really make a game of breakout running at the fastest speed, as if its that fast it wont be playable
You will have to frame lock it to a desired speed, this is especialyl important for manipulating the speed for consideration on what level there on, and even extra goodies as in certain color bricks being hit, or objects gotten that fall from the sky, etc..
So you will always be able to judge collision.

However to awnser yoru question....
In situations where you can have speed at a vary wide variation, you have to FOWARD check your collisions. As in not is it colliding now, but will it collide soon. And even more advanced collision engines, accully have to check not only collisions, but also all the variations the object can take, to check them all for collisions. Howeevr you wont have to worry about this in the beginner forums :L) When you get there youl know it. Hell, im not even near it yet, and only know a little of the theory, not the accual implementation of it.

hope i was a bit helpful.. Basically just lock your breakout clone to a certain framerate, and calculate it off that.
<=- Talon -=>
Another good collision detection method is: Take a line using the co-ordinates of where your ball currently is and where it will be next frame, then check if this line intersects on the edge of any other game objects (such as the paddle, bricks, walls, etc.). This would probably work regardless of frame rate.

Also you can reduce the number of collision tests you have to perform depending on the direction the ball is travelling. For example, if the ball is travelling from left to right you only need to test if the ball hits the left edge of an object as it is not possible for a ball travelling in that direction to collide with the right side of an object (unless the ball has passed through the object).
The same reasoning applies to the vertical component of the balls velocity.

Cheers.
OK! thanks you guys i can do the collision detection.
How do you suggest about the ball''s moving when it collides against the bricks or the paddle. Also, must notice the influence of gravity to the ball?
My breakout''s demo is nearly finish. But i''ve got one question: How can i treat the ball''s moving depends one how the paddle and ball are moving? For example: When the ball is moving toward RIGHT + BOTTOM corner of the screen and reachs the paddle and as soon as the ball hits the paddle the user press RIGHT button then the ball will bounce with opposite its current direction (say: toward LEFT + TOP of the monitor).
Thank you in advance!

This topic is closed to new replies.

Advertisement