From: Roman Suzi Newsgroups: comp.os.msdos.djgpp Subject: Re: collision detection Date: Mon, 20 Oct 97 18:51:41 +0300 Distribution: world Organization: unknown Message-ID: Sender: news-service AT sampo DOT karelia DOT ru Reply-To: nuser AT rsuzi DOT pgu DOT karelia DOT ru Lines: 119 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Hi! >> >>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... The solution is much simpler: int collision(RECT *a, RECT *b) { return a.right >= b.left && b.right >= a.left && a.top >= b.bottom && b.top >= a.bottom; }; Please, check! The generalization to more objects is quite obvious. I hope, this will help. Sincerely, Roman -- -- * -- -- Roman A. Suzi * Petrozavodsk Karelia Russia -- -- http://www.angelfire.com/ks/sudensivu --