Thread: breakout style game collision detection

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    breakout style game collision detection

    i have gotten the paddle to ball and the wall collisions working, but i can't get the ball to block collisions working good. I think the prob is because i am usualy moving the ball 5 units at a time. I am trying using a loop so if i move 5 units at a time i test for each 5 but only draw at the end, but this still does not work.
    here is some of the code

    Code:
    	for (i =0; i< 6; i++)
    	{
    	   block_collision_detection();
                       textprintf(screen, font, 2, 
    	   ball.x += ball.x_speed/6;
    	   ball.y += ball.y_speed/6;
    	}
    
    
    
    void block_collision_detection()
    {
    	int x,y;
    	for (x=0; x<SCRX/BLOCK_W; x++)
    	{
    		for (y=0; y<SCRY/BLOCK_H-2; y++)
    		{
    			if (map.block[x][y].active && BBCD(ball.bounding_box, map.block[x][y].bounding_box))
    			{
    				//find out which side of the block the bounce is on and bounce
    				if (!block_hit)
    				{
    					if (ball.bounding_box.bottom >= map.block[x][y].bounding_box.top && ball.bounding_box.bottom <= map.block[x][y].bounding_box.bottom && ball.bounding_box.left >= map.block[x][y].bounding_box.left && ball.bounding_box.right <= map.block[x][y].bounding_box.right) ball.bounce(TOP, xblip);
    					else if (ball.bounding_box.top <= map.block[x][y].bounding_box.bottom && ball.bounding_box.top >= map.block[x][y].bounding_box.top && ball.bounding_box.left >= map.block[x][y].bounding_box.left && ball.bounding_box.right <= map.block[x][y].bounding_box.right) ball.bounce(BOTTOM, xblip);
    					else ball.bounce(LEFT, xblip);
    
    					map.block[x][y].del();
    					block_hit = true;
    				}
    			}
    		}
    	}
    	return;
    }
    Last edited by m00se123; 10-25-2002 at 11:02 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I've made a breakout-like game. It's on my homepage (named Ball), source code included.
    It's not the best collision detection, especially not in higher velocities, but it works.
    Basically, you split the block into 4 triangles and check which one the ball is in (when overlapping).
    Code:
    +------+
    |\    /|
    | \  / |
    |  \/  |
    |  /\  |
    | /  \ |
    |/    \|
    +------+
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    you could try checking before the ball hits the block,

    ie,

    if(ball'snextposition >= block's min width/height etc., && <= blocks' max widh height etc.,)
    bounce back the ball;

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by mickey
    bounce back the ball;
    In which direction then? If it's coming from the bottom right, 45 degrees straight at the corner of the block. Bounce down, or bounce right???
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    huh? it's upto you where you want the ball's next direction be??

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Your algorithm only checks whether the ball overlaps the block or not, nothing of which direction it should bounce. The resulting direction may seem logical to an intelligent human, but not to a computer. Should you reverse DX? DY? Or both?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Maybe just check if the cooridiates of the ball and the range of the paddles' front side is the same

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple 2-D Collision Detection
    By Tonto in forum Game Programming
    Replies: 4
    Last Post: 10-14-2006, 06:54 PM
  2. Some per poly collision detection code
    By BobMcGee123 in forum Game Programming
    Replies: 0
    Last Post: 01-31-2006, 06:37 PM
  3. Ray tracer and collision detection
    By hdragon in forum Game Programming
    Replies: 31
    Last Post: 01-19-2006, 11:09 AM
  4. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 08:43 PM
  5. Writing a DOOM style game in VC++ & OGL
    By Zeeshan Zia in forum Game Programming
    Replies: 1
    Last Post: 10-20-2001, 04:13 AM