Click here to Skip to main content
15,890,932 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am struggling to find a way to draw a line of bricks and to create collision detection between the bricks and the ball.

Any help / suggestions are much appreciated.

Cheers,
Posted
Comments
Ron Beyer 16-Jun-13 8:18am    
Winforms, WPF, ASP?
Richard MacCutchan 16-Jun-13 8:30am    
Create a Brick class that can draw itself at a given location. Keep a list of all bricks and their position on screen. Do similar with the ball, and as it moves check whether its edge touches the edge of any brick.

1 solution

If you are using Windows Forms then you may start simply drawing rectangles as bricks and a circle as the ball (then you might refine your graphics).
Collision detection is actually very simple: let {XC, YC} be the coords of center of the ball, R is ray, and {XTL,YTL}, {XBR, YBR} respectively, the top-left and bottom-right point of a brick, then

C#
if ((XTL-XC) < R || (XC-XRB) < R || (YTL-YC) < R || (YC-YBR) < R)
{// handle collision here
}


detects a collision.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900