Archived

This topic is now archived and is closed to further replies.

collision detection - how to implement

This topic is 5313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok, this might seem pretty banal, but it really puzzles me: how (or where) is collision detection implemented? i''m talking about a more or less simple 2D zelda-style game and actually i wanted to stick to OOP as close as possible. my issue: where do the NPCs, the hero etc. get the collision data from? one way would be to simply pass a map of the room (or better a pointer) to all objects that need to check for collisions. but hey - actually a monster should not know about this, because this data belongs to the room (what also is a seperate object) or the hero should not know about all the monsters running around. making it this way would heavily violate the black box concept (and yes, i''m actually very keen on this ). another approach would be to implement collision detection somewhere outside, so the map-data can be fetched from the room, the positions from the NPCs etc., do all the collsion detection and "inform" the NPCs accordingly. but this approach again would have the drawback that a lot of functionality is taken away from the objects, so it''s not really encapsulated anymore... now feel free to give me some hints on how to do this

Share this post


Link to post
Share on other sites
Just my 2 cents:

I imagine it that the objects ask the world to check if they are colliding with anything, and they pass it a sort of outline of itself. The world then passes this iformation to any nearby objects and asks them if they collide with this outline. If anything does, then the world sends the object back where, when and with what to the object, so it can handle the situation accordingly.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
what about in 3d? and how long does it take for it to happen in 3d?

Share this post


Link to post
Share on other sites
quote:
Original post by DudeMiester
Just my 2 cents:

I imagine it that the objects ask the world to check if they are colliding with anything, and they pass it a sort of outline of itself. The world then passes this iformation to any nearby objects and asks them if they collide with this outline. If anything does, then the world sends the object back where, when and with what to the object, so it can handle the situation accordingly.


Big problem with this method. If your character is moving 10 tiles per second. There's a wall in front of him that is 1 tile thick. .11 seconds pass between updates. Your character would be .1 tiles in front of the wall, so he would be able to walk through walls. Given enough of a system "lag" he could walk through any wall.

My solution was to define a minimum distance. .05 tiles for example. Now you actually calculate every frame of minimum distance between where the character was last frame and where his velocity would take him this turn. So if he was at x = 1 last frame, and ends up at x = 2 next frame.. there wouldn't be 1 collision test, but 20 called.. one at 1.05, 1.1, 1.15, etc, etc..

This effectively solves the problem as long as the minimum distance is greater than the minimum sized obstacle. Working in whole numbers is alot easier too.. but I wanted to have tiles that could be 1/2 walkable and 1/2 non-walkable.

If you try to implement a perfectly OO collision detection system its going to probably be awkward at best. Don't let one overhyped paradigm rule over your coding. OO is a tool, not a rule. I would really love to know of any commerical game that was written with strict adherance to 'classical' OO. I really don't think its possible. As a side note I don't really consider singletons and other concepts that aren't OO but people like to pretend they are, as OO. Shrink wrapped globals are still globals.



[edited by - haro on May 28, 2003 12:23:00 PM]

Share this post


Link to post
Share on other sites
The singleton pattern is a way to control the instantiation and lifecycle of an object -- how is that not OO? But, enough of that, that''s going on a tangent that''s already been beaten to death around here...

Share this post


Link to post
Share on other sites
quote:
Original post by Mage2k
The singleton pattern is a way to control the instantiation and lifecycle of an object -- how is that not OO? But, enough of that, that's going on a tangent that's already been beaten to death around here...




    

SomeGlobalClass *psgc = NULL;

int main()
{
if(psgc == NULL) psgc = new SomeGlobalClass;
....
....
if(psgc != NULL) delete psgc;
psgc = NULL;
....
}


Wow, I just controlled the instantiation and lifecycle of an object. How is that not OO? I hope you know.


[edited by - haro on May 28, 2003 2:11:40 PM]

Share this post


Link to post
Share on other sites