How to Manage Collisions

Started by
2 comments, last by Alrecenk 14 years, 4 months ago
Heya, I'm working on a platformer in C++ using SFML. All game objects (enemies, items, player, tiles) are derived from a Body base class which has a polygon & AABB. I have my AABB and Polygon collision functions down. My question is: how do I check / manage collisions between objects? I have an idea of how to do so, but I've never written such a thing before and I'm curious if there is a better method than the one I'm about to describe. I'd have an object called Interactions. This object would keep a few lists: friendly objects, unfriendly objects, and tiles. Every game loop, this object would go through the friendly objects and check them with all unfriendly objects. Then, it would check all friendly and unfriendly objects with the land. The actual checking would be done by the objects themselves (since some collision types do not require polygon/polygon collision). For example, the player with the ground is not a polygon/polygon collision; it's simply point-in-polygon. The collision test / response functions in these objects would return true if a collision occurred and false if a collision did not occur. These functions would also take a bool to denote whether or not the actual collision test was run. So all the interaction manager would do is make a bunch of calls like if (Body1->Collide (Body2, false)) Body2->Collide (Body1, true); For each object that needs to be collided with each other object. Make sense? Thanks in advance! edit: Hey, I thought of a better way to do movable object / tile collision. If I store all tiles in a 2-dim array I can just use int. division and collide with all tiles in the area (within the object's width and height). That way, each moving object only has 1 tile to collide with and not a list of hundreds. [Edited by - rfranknj on November 30, 2009 9:14:32 PM]
Advertisement
I would recommend taking a look at this thread http://www.gamedev.net/community/forums/topic.asp?topic_id=554643

It definitely helped me out.
Quote:Original post by simotix
I would recommend taking a look at this thread http://www.gamedev.net/community/forums/topic.asp?topic_id=554643

It definitely helped me out.


are you aware that you just posted a link to your own thread
If you're talking about how to efficiently handle finding the collisions between large numbers of polygons(or other objects) then you're talking about broad phase collision detection and you can google for more info on that. If your talking about how to generally layout your structures to deal with collisions here is how I do it:
In my collision/physics set up I have a physical polygon class and a physical system class. The physical system class is just a big list of physical polygons and some kind of broad phase collision structure to quickly find collisions. Each polygon has a parent object that extends an interface with a single collision response method that passes in all the collision data. Each frame when a physics step is called if a polygon collides with something then it's parent object will be alerted by a call to "collision response" with the collision point,the polygon that's calling the method(since some objects have more than one polygon) the other polygon that it collided with, and the corrections the physics engine made to fix the collision. That way all of the game logic for how an object reacts to collisions is contained entirely within it's own class and separated from collision response.

It seems kind of obvious now, but I think it's worth saying because I recall a time when it wasn't so obvious. I also have a few flags to turn physics off for polygons, but keep collision detection on. Then I can create a sensory polygon by making a big cone or circle around an object and attaching it. The object gets information each frame about all of the objects within it's sensory polygon, and gathering this information benefits from all of the optimizations I've already written into the collision detection for the physics.

Here's a collision response method for an enemyship in my game:
public void collide(double position[],double projection[],double force[], physicalpolygon mypolygon,physicalpolygon collidedpolygon){		unit cr = (unit)collidedpolygon.parent ;		if(cr instanceof bullet && cr.faction !=faction){//if a bullet from a different team			bullet b = ((bullet)cr) ;			health-=b.damage ;//take the damage			if(b.parent instanceof playership){//if a bullet from a playership				((playership)b.parent).score+=difficulty*b.damage/maxhealth ;// then reward points for damage			}		}	}

This topic is closed to new replies.

Advertisement