Advertisement

2d collision detection

Started by July 06, 2000 01:36 PM
7 comments, last by djsteffey 24 years, 6 months ago
Anyone got some code they can show me on doing pixel perfect collision in 2d. I am using MSVC 6.0 DirectX 6.1 640x480x16 All the tuts on this site seem to be using only 8 bit color. So I have tried and tried to convert this to 16 bit color routines with no success. The first thing I do is check for bounding boxes, if they intersect then I find the dimensions of the intersecting portions of each box, then I compare corresponding pixels to see if they != 0, i have also tried != RGB(0, 0, 0) but this doesnt seem to work. What happens is it passes the bounding box detection (which means it does touch), and then proceeds to detect the pixels, but the first pixels it checks it returns the value that means pixel perfect does collide. But I know it doesn''t because to test my algorithm I used two surfaces, one contained a square that used the whole surface and the other contained a small circle that covered less then 1/3 of it. So the problem is that it returns a collision as soon as the bounding boxes touch. Even if no pixels touch. If you already have some code that works send it my way and let me dissect it. Thanks "Now go away or I shall taunt you a second time" - Monty Python and the Holy Grail themGames Productions
Well, does your RGB macro convert those colors to the correct 16 bit format (565 or 555, or other possibilities)?

I'm just asking since the RGB macro is defined in some of the Windows headers and using it might produce a word using the wrong format.

- Muzzafarath

Mad House Software
The Field Marshals

Edited by - Muzzafarath on July 6, 2000 2:47:23 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
Do you blt the sprites?
Do you use a Key Color

if yes then look for pixels NOT colored the key color.

--------------------------
Carpe Diem
D.V.Carpe Diem
Here''s how it''s done...


you have to get the rectangle that has been intersected (meaning the coordinates and put them in a RECT structure)..

once you have that done... do either a nested FOR loop, or a single FOR loop (to speed it up use a single one)... and loop through the Y and X values of that intersection rectangle...

then AND each of the pixels from both and if the result is > 0 then the images have intersected... that''s basically how it works...

the same thing goes for the 16-bit colors, but the only difference is that you AND the RGB values separately, and not the color...

e.g.

WRONG WAY!!

if (Color1 & Color2 > 0)
ImagesIntersect();

RIGHT WAY!!

if (Color1.R & Color2.R > 0 && Color1.G & Color2.G > 0 && Color1.B & Color2.B > 0)
ImagesIntersect();

Hope this helps!

..-=ViKtOr=-..

..-=ViKtOr=-..
The algorithm below works if BLACK is your transparent color... otherwise you would have to change the > 0 to something different, depending on your transparent color choice..

since RGB(0, 0, 0) which is BLACK is still 0, it''s simple to check against it...

..-=ViKtOr=-..
How would i extract the rgb values from a 16 bit color ?
I think directx or maybe mfc has a function like getRValue(color) or something like that but that is for 24 bit colors
I would i get it for 16 bit color ?


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Advertisement
Here are two macros that do that, depending on your RGB values'' bits per channel..

RGB16_555(r,g,b) ((r>>3)<<10)+((g>>3)<<5)+(b>>3)
RGB16_565(r,g,b) ((r>>3)<<11)+((g>>2)<<5)+(b>>3)
thanks gladiator, but i have the color and want to extracty the individual R values the G value and the B value

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Make another set of sprite images in 2 bit colour and compare them, not the coloured ones.

If you take this down to the binary level there are a lot of nifty tricks available to you (I''ve explained some before in another thread). For example, you''ll be able to test 16 pixels of one sprite with 16 pixels of another, by just OR''ing two WORDS.

I can''t give you directX specific examples of how to acheive these results, but I am willing to help you out with the math/logic and code.

Later

Matt





Check out my project at:www.btinternet.com/~Matthew.Bennett

This topic is closed to new replies.

Advertisement