[SDL] Collision

Started by
15 comments, last by theSeby 20 years, 8 months ago
I''m looking for a pixel-exact collision detection between 2 sprites (SDL_Surface) in SDL. I''ve got a box-box collision detection yet and i find the intersection rectangle. Thanks ! ------------------------ - Seby - www.dfhi-bomber.fr.st
------------------------ - Seby -www.dfhi-bomber.fr.st
Advertisement
Loop through that intersection rectangle (2 nested loops), look up the appropriate pixel in each sprite (a multiply and an offset), compare them (if both are ''solid'', you have a collision). It''s easy when you break it down.
Why does that seem extremely slow? Looping through every pixel in each sprite...

Maybe you should have a bounding box and when the boxes intersect, you should only do that pixel-pixel collision detection in that resulting rectangle. Hmmm, I'm stupid anyway.

[edited by - SuperRoy on August 7, 2003 7:08:04 AM]
Sup guys?
I''ve found there are extreamly few times when pixel perfect collision is 100% necessary especially in games.

If rectangles aren''t cutting it try using circles instead, or maybe capsules.

Just a thought.

Michael
OK, thank you all !
I think Michael is right and maybe I don''t really need this pixel-perfect collision... hummm, I''ll see.


------------------------
- Seby -
www.dfhi-bomber.fr.st
------------------------ - Seby -www.dfhi-bomber.fr.st
if your bounding box collision is giving you "fake" collisions. then make the bounding box for that sprite smaller, only containing the main part (ie body or fluesilage) so the arms and wings may not detect damage, but neither will the huge empty space above them.

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Or, allow a sprite to use more than one bounding rectangle.

If you''re really going for gold (though possibly slowness as well) you could write code for a ''collection'' of arbitrary collision shapes - so you can use bounding rectangles, circles, and polygons in the same sprite.

Of course, it''s almost totally unnecessary.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

SuperRoy, there''s no reason why it has to be slow. Think about software 3D renderers that perform calculations for every single pixel on the screen. That is fast enough to get many frames per second, so performing a similar exercise on a tiny fraction of that many pixels when there has been a collision between the bounding boxes will be trivial.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Try this:
http://www.ifm.liu.se/~ulfek/projects/2d_Collision_Detection.html
Its a fast pixel-perfect collision detection library for SDL

- Wyzfen
Thank you very much for your feed back !
I think I can do a box-box collision detection, with box that are a little bit smaller than the entire sprite.

Please check the demo, that's with a box-box col. det., and the box are the same size of the picture (I have to reduce the box). Do you think the box collision with smaller box will be enough ?

my demo

------------------------
- Seby -
www.dfhi-bomber.fr.st

[edited by - theSeby on August 8, 2003 3:32:35 AM]
------------------------ - Seby -www.dfhi-bomber.fr.st

This topic is closed to new replies.

Advertisement