Collision Detection techniques.

Started by
6 comments, last by Finalspace 8 years, 7 months ago

Hi folks,

I know there are a ton of tutorials and examples on the web. But I am am looking for something very specific (I guess). Until now I always constructed my maps with tiles. With tiles collision detection can be handled pretty easy, because they are rectangles. In XNA/Mono one can easily check for collision via Rectangle.Intersects.

What I want to do now is to load entire sprites and use them as ground or walls. The problem is they are never a simple rectangle. In most cases those are shapes which consist of several rectangles.

Let's say the example below could be one of the sprites which represent the bounds for a platformer level:

[sharedmedia=gallery:images:6593]

Are there any techniques to detect collision for a shape like this? I mean sure I could find a workaround like defining multiple rectangles for a single shape.

I just wanted to make sure whether there is already a existing approach I could use for something like this.

As mentioned above my intention is to use this collision detection for a 2D platformer-ish game.

Any suggestions?

Advertisement


I mean sure I could find a workaround like defining multiple rectangles for a single shape.

Why do you think that this is a workaround ? It is by far easier to handle each tile as single collision object, because you often need to check an object vs a handful of rectangles only.

Because i assume that a more plausible solution exists. Am I wrong?

Multiple simple sub-shapes per object is a quite plausible alternative, and commonly used especially if performance is important.

If you need good precision one alternative is to use an image as a mask and do collisions per pixel (where essentially each pixel become one small rectangle). Each object and tile gets its own black-and-white mask where one of the colors means collision, then calculate the intersection of the bounding rectangles of two objects and loop over the pixels in the collision-mask and check for intersecting collision pixels. This usually works best if doing movement etc. in integers so the smallest unit of measure is 1 pixel. This can be extended to more than 2 colors in the mask to handle different materials.

Another alternative is to use convex hulls instead of rectangles, though for an object like the one in your example might require more than one convex hull as well, depending on what precision is needed. A concave polygon around the edges of the sprite is another alternative, though collision detection can become a bit more complicated, though you can probably get away with just looping over all the edges in the first object and for each edge check if it intersects any edge in the second object, as it will cover any case except where one object is fully inside another which is often impossible if you limit your time-step to reasonable limits.

So assuming I use Mono, what would be a good way to define those collision rectangles? Should I define them manually or is there a way to generate them using the texture? Probably generating collision boundaries from a texture would require a per pixel iteration? This sounds like an overhead for my small project. Can you think of anything else?

Hard to say without knowing more details about how you create the texture and build your maps. Do you want to draw one big texture in a drawing-program that will be your entire level, and get proper collision detection against that image?

If so I would suggest manually placing multiple rectangles that fit the parts of texture that require collisions.

Hard to say without knowing more details about how you create the texture and build your maps. Do you want to draw one big texture in a drawing-program that will be your entire level, and get proper collision detection against that image?

If so I would suggest manually placing multiple rectangles that fit the parts of texture that require collisions.

Thank you, this is exactly what I intended to do. This is also how I implemented it now.

There is a much simpler solution for that - in case you have hard edges:

You just trace all the contours in your level sprite, build up the edges and the create chained line segments out of it.

Also you can create polygons out of that edges, but you must convert non-convex polygons to convex polygons!

The theory behind is really simple, but implementation is not that easy - just google for contour tracing.

This topic is closed to new replies.

Advertisement