Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
2d Tile Based Gaming -- Collision Detection Help
Author Message
Mr. T

Posted: Thu Jul 19, 2007 7:19 pm   Post subject: 2d Tile Based Gaming -- Collision Detection Help

I decided to remake my BreakOut game using the new knowledge I had gained from CodeMonkey's 2d Tile tutorial. My collision detecton is a bit off. Help would be appreciated.

BreakOut.zip
Description:
Filename:  BreakOut.zip
Filesize:  1018 Bytes

CodeMonkey2000

Posted: Thu Jul 19, 2007 11:06 pm   Post subject: Re: 2d Tile Based Gaming -- Collision Detection Help

The method I showed works best with top-down games. there is a more flexible method for collision. Basically imagine that the ball is enclosed in an imaginary box. You need to know the midpoint of the box/circle and the distance from the mid-point to the sides of the box. Using this, check where what tiles the corners of the box will be in if the ball continues to move. If any of the corners are not in tile 0, then a collision has occurred.

Here's a function that you may find useful. Just pass in where the ball will be, and its radius.
 Turing: fcn collide (x, y, r : int) : boolean     % coner#1     if tiles ((x - r) div 20, (y + r) div 20) not= 0 then         result true         %corner #2     elsif tiles ((x + r) div 20, (y + r) div 20) not= 0 then         result true         %corner#3     elsif tiles ((x - r) div 20, (y - r) div 20) not= 0 then         result true         %corner#4     elsif tiles ((x + r) div 20, (y - r) div 20) not= 0 then         result true     end if     result false end collide
Mr. T

Posted: Thu Jul 19, 2007 11:32 pm   Post subject: Re: 2d Tile Based Gaming -- Collision Detection Help

I implemented the function, but the ball nonetheless gets caught when it hits the corner of a brick.

BreakOut.zip
Description:
Filename:  BreakOut.zip
Filesize:  1.13 KB

CodeMonkey2000

Posted: Fri Jul 20, 2007 3:04 pm   Post subject: Re: 2d Tile Based Gaming -- Collision Detection Help

That's because you aren't checking for a diagonal collision. Try this code, it isn't efficient but you should be able to simplify it. Also remember that the maximum size of the radius should be 10. If it's bigger you'll need to break the circle down to smaller squares.
 code: if collideX (x (i) + dx (i), y (i) + dy (i), RADIUS) and not collideX (x (i) + dx (i), y (i), RADIUS) and not collideY (x (i), y (i) + dy (i), RADIUS) then             dx (i) := -dx (i)             dy (i) := -dy (i)         else             if collideX (x (i) + dx (i), y (i), RADIUS) then                 dx (i) := -dx (i)             end if             if collideY (x (i), y (i) + dy (i), RADIUS) then                 dy (i) := -dy (i)             end if         end if
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 4 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: