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* CreateBoundingO bject( vector<Triangle s>& tris);
bool GetCollision(Bo undingObject* b1,BoundingObje ct* 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 BoundingTriangl e : public BoundingObject
{
// specific data
virtual int GetShape() { return B_TRIANGLE; }
};
bool IsCollisionSphe reSphere(Boundi ngSphere* s1,BoundingSphe re* s2);
bool IsCollisionSphe reBox(BoundingS phere* s1,BoundingBox* s2);
bool IsCollisionSphe reTriangle(Boun dingSphere* s1,BoundingTria ngle* s2);
bool GetCollision(Bo undingObject* b1,BoundingObje ct* b2)
{
if( b1.GetShape() == B_SPHERE)
{
if( b1.GetShape() == B_SPHERE) return IsCollisionSphe reSphere(
static_cast<Bou ndingSphere*> b1,
static_cast<Bou ndingSphere*> b2 );
if( b2.GetShape() == B_BOX ) return
IsCollisionSphe reBox( static_cast<Bou ndingSphere*> b1,
static_cast<Bou ndingBox*> 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->pBoundingObjec t = BoundingObject* CreateBoundingO bject(
pObj->triData ) ;
object* pObj2 = new Obj;
// Some specific code.... //
pObj2->pBoundingObjec t = BoundingObject* CreateBoundingO bject(
pObj->triData ) ;
bool IsCol = IsCollision(pOb j1,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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:Tj******** ********@newsre ad1.dllstx09.us .to.verio.net.. .
Simon Elliott wrote: If I have a base class and several possible derived classes, and I have
a pointer to the base class, what's the best way of determining whether
the pointer is pointing to a base class or to a derived class?
The best way is not to do it.
The classes have virtual functions, so I could use a dynamic_cast. Is
there much overhead associated with this?
There is some. Whenever you're concerned with performance, you need to
test it. Nobody will tell you what it's going to be except a profiler.
Alternatively I could have an instance variable in the base class which
derived classes are required to set, or a virtual function which
derived classes are required to override. This puts a burden on
developers of derived classes.
Any other options? Which is best?
The best is to design your program so that you don't need to try to know
what derived class object it is.
V