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 addEntity(CEntity *e);
void removeEntity(CEntity *e);
private:
//list stuff
int mLen;
CEntity *mStart;
//test two boxes for collision
int mTestCollision(CBox b1, CBox b2);
};
thanks for reading,
danny