SFML community forums

Help => Window => Topic started by: GroundZero on February 04, 2012, 01:07:25 pm

Title: Collision Detection 2.0
Post by: GroundZero on February 04, 2012, 01:07:25 pm
Dear readers,

for several months I have been trying to get a pixel based collision detection working, but nothing is working out.

Is there any guide on the internet, tutorial or anything that I can download, that you know off it is working 100%?

I tried several things but none are working. Either 1000's of errors or just simply not working.

I am using SFML2.0 with QtSDK 4.7.4.

I want pixels based collision detection because when i would use a bounding box, it would look weird i.e. if a enemy shoots magic, you can get hit even when it is not a collision for example because the invisible bounding box around the sprite.


Hopefully someone can tell me how to make such kind of collision detection.
Title: Collision Detection 2.0
Post by: julen26 on February 06, 2012, 05:29:42 pm
Checking every pixel of a image can be an expensive operation. So programmers usually use a boolean bidimensional array to keep the information about the pixels.

Code: [Select]
bool **mask;

You would have to check every pixel and check its transparency, this way you can set pixel

The generation of the matrix can be expensive so you should do it when starting or loading the game.
Title: Collision Detection 2.0
Post by: Nexus on February 06, 2012, 07:10:17 pm
I would use a one-dimensional array and map 2 dimensions to it. And I would prefer STL containers like std::vector<bool> to get rid of memory management, pointers to pointers and unsafe accesses. Although the latter is a little bit an unfortunate specialization with some "special" features...
Title: Collision Detection 2.0
Post by: Breakman79 on February 06, 2012, 07:19:14 pm
I wouldn't throw away the bounding box approach either.  Since collision detection is such a CPU intensive action it works well to use a mult-tier approach.  Check for a collision with the bounding boxes first; if it returns a hit then run the check for a pixel collision on those objects to see if they are actually intersecting.  This way you're not wasting processing time on checking for pixel perfect collisions when the objects aren't even near each other.
Title: Collision Detection 2.0
Post by: julen26 on February 06, 2012, 09:31:43 pm
Totally agree with the last 2 answers =)
Title: Collision Detection 2.0
Post by: dydya-stepa on February 10, 2012, 12:29:41 pm
http://create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel