Sign in to follow this  
tommy-t

Object detection

Recommended Posts

Hi
I'm building a collision system for my 2D game, broadphasing (sweep and prone + pair detection) works. But I don't know how to separate pickup objects, tiles and the player from each other. My question is; what is the best method for game object type detection? The only thing i come up with is:if(pair.a.type == player && pair.b.type == item) but its does not work if a and b are swapped.
Edited by TomA3000

Share this post


Link to post
Share on other sites

You could implement a class defining the type for your object classes like this:

class ObjectType

{

  public:

     ObjectType(const std::string& n)

       :name(n)

     {}

 

     const std::string& GetTypeName()const { return name;}

 

  private:

     std::string name;

};

 

Then for each of your object class, you do something like this:

// MyObject.h

class MyObject

{

   public:

     const static ObjectType type;

};

 

//  MyObject.cpp

const MyObject::ObjectType type("MyObject");

 

Now you can query your object type and respond to it:

if(myObject.type.GetTypeName() == "MyObject") {

// do something

} else if(...) {

// do something else

}

Share this post


Link to post
Share on other sites
How does introducing strings help anything?

If I understand the problem correctly, you are trying to figure out what function to call to check if two objects intersect. One answer is something called "double dispatch".

Share this post


Link to post
Share on other sites

In my collision system I did something like this:

class Capsule3D{
    //data for capsule object
};
class Sphere3D{
    //data for sphere object
};
Class IColPrimitive{
public:
    virtual TestResult test(const IColPrimitive& aOther)const=0;
    virtual TestResult test(const Capsul3D& aPrim)const=0;
    virtual TestResult test(const Sphere3D& aPrim)const=0;
...
};

class Capsule3DColPrim : public IColPrimitive{
public:
    virtual TestResult test(const IColPrimitive& aOther)const{
        return aOther.test(mCapsule);
    }
    virtual TestResult test(const Capsul3D& aPrim)const{
        Actual test code using mCapsule and aPrim
    }
    virtual TestResult test(const Sphere3D& aPrim)const{
        Actual test code using mCapsule and aPrim
    }
    Capsul3D mCapsule;
};

class Sphere3DColPrim : public IColPrimitive{
public:
    //similar to Capsule3DColPrim
    ...
    Sphere3D mSphere;
};


Then you just have to call a_IColPrimitive.test( another_IColPrimitive );

 

 
I believe this is the Bridge design pattern but I might be mistaken on this.
Hope that help!
Cheers!
Edited by StephanieRct

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this