Sign in to follow this  

Object detection

This topic is 1711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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

This topic is 1711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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