Results 1 to 2 of 2

Thread: Need help with breakout collision detection

  1. #1

    Need help with breakout collision detection

    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

    Code:
     
         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

  2. #2
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Need help with breakout collision detection

    The most simple approach is to do one axis at a time.

    Like this:[pascal]// 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![/pascal]


    Mind you your detection is incorrect [size=9px](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)[/size] 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.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •