Messy static Downcasting, any alternatives??

Discussion in 'C++' started by Michael, Oct 15, 2004.

  1. Michael

    Michael Guest

    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
    Michael, Oct 15, 2004
    #1
    1. Advertising

  2. Michael wrote:
    > 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.

    [...]
    > Any thoughts??


    If you want to sepate the libaray, define the collision detection
    in its own terms with its own types and objects and methods. Make
    your game objects create the necessary collision detection objects
    that your libaray is going to understand. Make those objects
    polymorphic so you can repace the libaray with another one if need
    be.

    IOW, collision detection becomes a sub-domain in your problem domain
    and you have to model it all by itself without involving your game
    objects in it. The collision detection code should be aware of your
    game objects just as much as it needs to correctly create is own
    data structures. But don't couple the collision detection objects
    with game objects.

    Those are my any thoughts.

    V
    Victor Bazarov, Oct 15, 2004
    #2
    1. Advertising

  3. Michael

    Michael Guest

    Yes but is that not what i did?

    "Victor Bazarov" <> wrote in message
    news:dEVbd.5014$09.us.to.verio.net...
    > Michael wrote:
    > > 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.

    > [...]
    > > Any thoughts??

    >
    > If you want to sepate the libaray, define the collision detection
    > in its own terms with its own types and objects and methods. Make
    > your game objects create the necessary collision detection objects
    > that your libaray is going to understand. Make those objects
    > polymorphic so you can repace the libaray with another one if need
    > be.


    Yes but is that not what i did? I kept it as abstract as possible with just
    a pointer to an abstract basde class??

    >
    > IOW, collision detection becomes a sub-domain in your problem domain
    > and you have to model it all by itself without involving your game
    > objects in it. The collision detection code should be aware of your
    > game objects just as much as it needs to correctly create is own
    > data structures. But don't couple the collision detection objects
    > with game objects.
    >
    > Those are my any thoughts.
    >
    > V
    Michael, Oct 16, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter O'Reilly

    Solution - Messy Visual Studio Editor

    Peter O'Reilly, Jun 15, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    443
    Peter O'Reilly
    Jun 16, 2004
  2. Bjoern

    debugging messy code?

    Bjoern, Nov 17, 2003, in forum: Java
    Replies:
    1
    Views:
    382
    David Postill
    Nov 20, 2003
  3. Roedy Green
    Replies:
    3
    Views:
    646
  4. buffi
    Replies:
    14
    Views:
    420
    James Stroud
    Sep 17, 2007
  5. pbd22

    Messy TreeView Results

    pbd22, Nov 15, 2007, in forum: ASP .Net
    Replies:
    11
    Views:
    512
    pbd22
    Nov 15, 2007
Loading...

Share This Page