PDA

View Full Version : Need help with breakout collision detection



joyrider
15-12-2005, 10:21 PM
Hi,

i'm trying to make a breakout game using jedi sdl and delphi. I'm using the supplied SpriteEngine. in my balls collisioncheck procedure i use to following code where middleX and middley Is the balls middle, rect1 is the rectangle for the ball and rect2 the rectangle for the block that's been hit



MiddleX := rect1.x +(rect1.w div 2);
MiddleY := Rect1.y +(rect1.h div 2);

if &#40;MiddleX >= rect2.x&#41; and &#40;MiddleX <= Rect2.x + Rect2.w&#41; then
begin
yi &#58;= yi * -1;
y &#58;= y + yi;
TBlock&#40;Parentlist&#91;tel&#93;&#41;.IncreaseHits;
end
else
if &#40;MiddleY >= rect2.y&#41; and &#40;MiddleY <= rect2.y + rect2.h&#41; then
begin
xi &#58;= xi * -1;
x &#58;= x + xi;
TBlock&#40;parentlist&#91;tel&#93;&#41;.IncreaseHits;
end


now that works ok but now i need to add some code to see when the ball hits the block at the corners and change the ball in the correct direction. how would i do that. also i'm stuck on something else as well the code for checking the corners may not be done when there are 2 blocks hit at the same time the above code should be enough then (i think).

can someone help me out here, i could upload my code if that'll be easier cause i'm getting nowhere.

a simple demo with a ball hitting a rectangle would help as well as long as it implements hits from the 4 corners of the rectangle and the ball

thanks

WILL
16-12-2005, 05:38 AM
The most simple approach is to do one axis at a time. :)

Like this:// Move Ball's X-axis

{Now check all the ball's 'detection points' for being inside the block}
//if so; Make adjustments for X-axis collision!

// Move Ball's Y-axis

{Now check all the ball's 'detection points' for being inside the block again}
//if so; Make adjustments for Y-axis collision!


Mind you your detection is incorrect (you have to check both X and Y axis to see if one of the ball's 'detection points' are inside the block you are detecting for) so work out an effective routeen for checking for a single X,Y point inside a block first. Then you can use that for each point you need for your ball to find hit blocks.