Path: chuka.playstation.co.uk!news From: "JohnT" Newsgroups: scee.yaroze.beginners Subject: Re: Collision Detection!.....(Joy) Date: Wed, 23 Jun 1999 11:56:39 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 81 Message-ID: <7kqf2d$qpv1@chuka.playstation.co.uk> References: <7kdntq$4fj13@chuka.playstation.co.uk> <376d3500.12938268@www.netyaroze-europe.com> NNTP-Posting-Host: 212.56.106.143 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 > >4)If all this done in the frame buffer then I have no bother moving stuff > >about but if it's done in memory how would you go about 'shifting the bits' > >and how would you know where to shift them to? > > ah, thje fun part. This next function is going to assume some nasty > things - that both the objects you want to check have masks of 16x16 > pixels (ie, 16x16/8 bytes = 32 chars). > > int HasCollided(int x1, int y1, int x2, int y2, u_char*mask1, u_char > mask2) > { > int shift1, shift2; > int numy; > u_long m1, m2; > u_char *line1, *line2; > int i; > > // do trivial boundy box reject first > // do these stacked IFs give quicker execution than > // one huge IF statement? havent checked so hypothetical > if (x1 > (x2+16)) return (0); > if (x1 <= (x2-16)) return (0); > if (y1 > (y2+16)) return (0); > if (y1 <= (y2-16)) return (0); > > // ok so bound boxes overlap... now some annoying stuff... > // find offset from 16 pixel boundaries for the masks > shift1 = x1 & 0x0f; > shift2 = x2 & 0x0f; > if (y1 > y2) > { > numy = y2 - y1 + 16; > line1 = mask1; > line2 = mask2 + (16-numy); > } > else > { > numy = y1 - y2 + 16; > line1 = mask1 + (16-numy);; > line2 = mask2; > } > > // now do the actual checking loop > for (i=0; i { > t1 = (*line1 >> shift2) >> 16; > t2 = (*line2 >> shift1) >> 16; > if (t1&t2) return (1); > line1++; > line2++; > } > > return (0); > } > Surely you do not need to use any shifts to detect the results of anding the two images? I would have thought it would be quicker to check the ints or longs for TRUE as any value other than zero is true. Going back to the original idea of the array containing a 2 bit area of the screen size with the two 2 bit masks anded on as per instructions ;) The following function will check it far quicker than using shifts. Oh, this routine assumes the screen res is 320x256 :) BOOL CheckCollide(long *array) { int i; for(i=0; i<2560; i++) // 2560 is 320 * 256 / 32 (32 bits to a long as we all should know ;) { if(array[i]) return (1); } return (0); } Regards, JohnT