Message-ID: <19971017142431.270.qmail@hotmail.com> From: "John Patton" To: djgpp AT delorie DOT com Cc: darn AT merlin DOT imag DOT net Subject: Re: collision detection Content-Type: text/plain Date: Fri, 17 Oct 1997 07:24:30 PDT Precedence: bulk >From: Darren Grant >Subject: Re: collision detection >Date: Thu, 16 Oct 1997 22:10:34 -0700 >Reply-To: darn AT merlin DOT imag DOT net >To: djgpp AT delorie DOT com > >Rob Farley wrote: >> >> >Andrew Deren wrote: >> >> >> >> Does anyone know how to detect if two rectangles overlap each other at >> >> some point. Let's say we have something like this: > > >Given two rectangles, A and B, check to see if any of the vertices of B >lie on A. If this is true, then there must be overlap. > >For collision-detection, you probably want to intersect your velocity >vectors with the rectangle to determine where the collision occurs. > Well, let's 'C', I've used rectangle collision detection before. It worked something like this: RECT a, b; a.top = TopA; a.left = LeftA; a.bottom = BottomA; a.right = RightA; b.top = TopB; b.left = LeftB; b.bottom = BottomB; b.right = RightB; ... int collision(RECT *a, RECT *b) { // check if left side of a is inbetween b's right & left if (a.left > b.left && a.left < b.right) { // check if the top of a is between b's top and bottom if (a.top < b.top && a.top > b.top) return TRUE; // collision! // check if the bottom of a is between b's top and bottom if (a.bottom < b.top && a.bottom > b.top) return TRUE; // collision! } // check if right side of a is between b's left and right if (a.right > b.left && a.right < b.right) { // check if the top of a is between b's top and bottom if (a.top < b.top && a.top > b.top) return TRUE; // collision! // check if the bottom of a is between b's top and bottom if (a.bottom < b.top && a.bottom > b.top) return TRUE; // collision! } // Then do the same thing for b: // check if left side of a is inbetween b's right & left if (b.left > a.left && b.left < a.right) { // check if the top of a is between b's top and bottom if (b.top < a.top && b.top > a.top) return TRUE; // collision! // check if the bottom of a is between b's top and bottom if (b.bottom < a.top && b.bottom > a.top) return TRUE; // collision! } // check if right side of a is between b's left and right if (b.right > a.left && b.right < a.right) { // check if the top of a is between b's top and bottom if (b.top < a.top && b.top > a.top) return TRUE; // collision! // check if the bottom of a is between b's top and bottom if (b.bottom < a.top && b.bottom > a.top) return TRUE; // collision! } return FALSE; // no collision! } I know it's probably not the best method, but of the top of my mind, I can't think of any others... -John ______________________________________________________ Get Your Private, Free Email at http://www.hotmail.com