#### Archived

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

# Does this seem like a good way to handle collisions?

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

## Recommended Posts

I''m working on a system for collision detection in 2d. The following is a description of what I have come up with. I''m interested to know what others think of it, it there are any problems with it etc.... I''m not sure how games usualy do this, so if there is a normal way please explain. There are entities. Each entity knows where it is, and can give a box describing its boundries. There is a list of entities. Each frame the list is cycled and each entity is updated. Then each entity is check against all the others for collisions. If a collision happens, they are both set with setCollision called with the correct type, then handleCollision is called for both of them. Note that this isnt meant to be totaly correct code, its more of an outline of how I think i might handle this.
class CBox
{
public:
float x1, x2, y1, y2;
};

class CEntity
{

public:

CEntity();
virtual ~CEntity();

//dt = delta time
virtual void update(int dt);
virtual void handleCollision();

//has the object collided with something?
int isCollision();

//tell the entity that it has just collided with another entity of type type
void setCollision(int type);
CBox getBox();

private:
CEntity *mNext, *mPrev;  //linked list stuff
int mType();
};

class CEntityList
{
public:
CEntityList();
~CEntityList();

//go through list and call update on all entities,
//then check for all collisions, if there is a collision
//call handlecollision for the entities involved.
//dt = delta time
void update(int dt);

//list managment
void removeEntity(CEntity *e);

private:
//list stuff
int mLen;
CEntity *mStart;

//test two boxes for collision
int mTestCollision(CBox b1, CBox b2);
};



##### Share on other sites
1) its not per-pixel - depends on what you need this could be critical or not.

solution: if you detect a box collision, do a more exact pixel collision check to make sure.

2) it is O(n^2) with respect to the number of entities, in other words as the number of entities is large it will be too computationally expensive - this is critical if your game has many entities.

solution: Divide your collision space. Do a search here in the forums for "space partitioning", or maybe "BSP".

[edited by - atlnewbie on April 13, 2004 7:10:43 AM]

##### Share on other sites
I have done it like this; (and it works quite well to)

struct tile{int Carrying;//height etc..};tile Map[SizeX][Sizey];

then I simply let the units update their positions to the Map[][].Carrying.

then if a unit needs to check if a space is empty (no collision) it could simply check if Map[unit.x+directionx][unit.y+directiony].Carrying = zero. or whatever.

Do this ^^ for each tile of the units Size... etc, if a unit is 5x3 tiles wide, move the entire "CheckBox" and check the appropriate tiles..

if you need more accuracy u could always use this as a complement to solution number two above.

Hope it helped!

##### Share on other sites
What I did was to write a collider class that has a CollisionDetection(collider&) and a CollisionResponse() methods.

CollisionDetection(collider&) is called for every moving object (in a loop), being collider& one of all the others possible obstacles, the closest collision time is stored in a member variable (custom struct/class) with other info.

I keep track of the time elapsed and I check with CollisionResponse() if it is time to collide, if so, I check the obstacle still exist and I perform the proper response based on the info I saved in the struct.

##### Share on other sites
I''ve developed a system handle many different types of collision objects (player, enemy, bullet, etc) and collision geometries (box, sphere, vector, etc) in this thread here:

By the end of the thread, there is a pretty robust system for handling 2D collision detection. It''s also inheritable so that objects can inherit from other objects and redefine only specific behaviours.

I also have some more complete code for it up here if you want to take it apart:

• 39
• 15
• 9
• 23
• 10