Sign in to follow this  
_Zac_

[C++] Collision Handling

Recommended Posts

I guess a few things to start off with, I'm using C++.

Alright, I have this class called ObjectHandler, which pretty much handles all the game objects. A game Object is defined as anything with a loop() and render() function as well as collision functions. The ObjectHandler stores all the Objects in a vector that when you call the ObjectHandler's loop() or render() functions it then calls every Objects loop() or render() functions.

Now, when calling the loop command the ObjectHandler gives itself to the object (like calling Object.loop(ObjectHandler this) so that the Object can use the ObjectHandler to check collisions with all other objects. The problem here is that if I call the ObjectHandler for a collision check I cannot check for specific subclasses of Object without creating separate functions and separate vectors.

So for a gist here's some psuedocode for what I want to do:

[code]

bool ObjectHandler::collision(class x)

{

for(int i=0;i<ObjVect.size;i++)

{

if(ObjVect[i] could be considered an x && ObjVect[i]->collision())

{

return true;

}

}

return false;

}

[/code]

Now of course collision would have a few more variables to pass in, but I believe this gets the point across of what I want to do.

Now I hear there is an easy way of doing this in C#, but I also hear that if you ever have to call a function to tell if a certain object is a certain class then you have design flaws. So I'm wondering if doing it like this is a major flaw in design and would be greatful if you could point me towards some faqs or give some advice if you believe that is the case.

Share this post


Link to post
Share on other sites
[url="http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/"]http://t-machine.org...lopment-part-1/[/url]

You should put a representation of all objects that can collide in a seperate container and use that container for your collision code, not ObjVec.

Share this post


Link to post
Share on other sites
Static - Static
Do nothing to save 99% of the time

Dynamic - Static
Use a grid or tile array

Dynamic - Dynamic
Use heap sort with the X dimension before letting each item test collisions to the neighbours.
Continue in each direction until the biggest item would not collide from that distance using the fact that abs(X) <= sqrt(X^2 + Y^2 + Z^2).

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