Sign in to follow this  
Lxu

Good Practices: General Shape Collision Detection?

Recommended Posts

Hello Guys,

I'm programming a 2d game, and i'm trying to use some good programming practices as far as i'm able to see.
I have a general entity class that represents some logical entity in my game, that can interact with the world.
To handle collision detection im using a generic interface of a shape, that represents my entities boundaries. Currently the available implementations are just simple shapes like circles and rectangles, but in the future i will like to have some composed shape implementation.
My shape interface has a method that test if this instance collide against other shape.

So, my problem is that to test this i need to now about the specific shape type, using an if verification to test what is the particular object instance (if is an rectangle-rectangle test i use some algorithm, if is a circle-rectangle i will use another).
I'm interested in know if there is some other approach that i can use, so i dont need to test the instance type, being able to scale this architecture with just some new class implementation.

Ty =)

Share this post


Link to post
Share on other sites
I don't see mention of [i]broadphase [/i]and [i]narrowphase[/i]. Those are two very important steps.
What you do seems to be fairly similar to what Bullet does so it [b]can [/b]be correct... provided you don't screw it! :P

Share this post


Link to post
Share on other sites
The normal solution to this problem is [url="http://en.wikipedia.org/wiki/Double_dispatch"]double dispatch[/url]. You haven't specified a language so hard to go into more detail but if C++, this language doesn't support double dispatch too well and it requires a certain amount of boilerplate.

An alternative, if you create a bunch of free functions that can test each combination of shapes, is to create a 2D array of function pointers indexed by some type value that a shape can return. This does involve a kind of instance test but is more extensible than other methods. If memory serves, [url="http://www.metanetsoftware.com/technique/tutorialA.html"]Metanet use this approach in N[/url].

However, good software engineering aside for a moment, last time I wrote a 2D collision system I gave up over-engineering when I realised that all 2D shapes I required could be described by an aggregation of polygons and circles so I just wrote a shape class that switched on these two types. If memory serves, I later expanded this to specialize for OOBBs as well as the test was so trivial it seemed worthwhile.

While not the "correct" way to do it, in a system where the number of types is never going to grow beyond a small, known number it is sometimes simpler to solve this way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this