Ok Guys,
on the same thread
I'm writing a Game engine, which performs collision detection.
I want to sepate the collision boundings libaray from the main game engine
and load it as a library.
If simplified this example but it illustrates the point....
So i declare an abstract base class ( & function ):
///////////////////////////////////////////////////
Bounding & Collision Libaray
//Boundings.h ( only file #include'd by the engine)
///////////////////////
class BoundingObject
{
public:
virtual int GetShape() =0;
};
BoundingObject* CreateBoundingObject( vector<Triangles>& tris);
bool GetCollision(BoundingObject* b1,BoundingObject* b2);
//Boundings.cpp
////////////////////////////////
#define B_SPHERE 1
#define B_BOX 2
#define B_TRIANGLE 3
class BoundingSphere : public BoundingObject
{
// specific data
virtual int GetShape() { return B_SPHERE; }
};
class BoundingBox : public BoundingObject
{
// specific data
virtual int GetShape() { return B_BOX; }
};
class BoundingTriangle : public BoundingObject
{
// specific data
virtual int GetShape() { return B_TRIANGLE; }
};
bool IsCollisionSphereSphere(BoundingSphere* s1,BoundingSphere* s2);
bool IsCollisionSphereBox(BoundingSphere* s1,BoundingBox* s2);
bool IsCollisionSphereTriangle(BoundingSphere* s1,BoundingTriangle* s2);
bool GetCollision(BoundingObject* b1,BoundingObject* b2)
{
if( b1.GetShape() == B_SPHERE)
{
if( b1.GetShape() == B_SPHERE) return IsCollisionSphereSphere(
static_cast<BoundingSphere*> b1,
static_cast<BoundingSphere*> b2 );
if( b2.GetShape() == B_BOX ) return
IsCollisionSphereBox( static_cast<BoundingSphere*> b1,
static_cast<BoundingBox*> b2 );
//etc....
}
//etc
}
//End Bounding & Collision Library
/////////////////////////////////////////////////////////
Then in my game engine:
class object
{
public:
BoundingObject* pBoundingObject;
vector<Triangle> triData;
};
in the game.....
object* pObj1 = new Obj;
// Some specific code.... //
pObj1->pBoundingObject = BoundingObject* CreateBoundingObject(
pObj->triData ) ;
object* pObj2 = new Obj;
// Some specific code.... //
pObj2->pBoundingObject = BoundingObject* CreateBoundingObject(
pObj->triData ) ;
bool IsCol = IsCollision(pObj1,pObj2);
the idea is that the engine need not know how the objects are being bounded,
but the comparisions between each type of object can be optimised in the
bounding library. But it is horribly ugly. I can't even use enums for the
Bounding types as adding different bounding types would mean a recompile for
the engine!
Any thoughts??
Mike