I can't say for sure if it's more efficient, but I can try to explain things a little better for you... How's that?
First:
Quote: "Problem is I don't know if my character is going to be the object A or b. So my code is going to get a little out of hand with all the if checks that I am going to have to:
"
And that's where the dynamic_cast<> comes to play... Note: From my understanding of the subject - this is a perfect example of a "case-study" where dynamic_cast<> may solve the problem...
First off, for this particular "case-study" I am making the following assumptions (based upon your code and descriptions of the problem):
1) GameObject is a base class,
2) Character derives from GameObject, and;
3) Element also derives from GameObject.
That is:
class GameObject; // base class...
class Character : public GameObject; // Inherits from GameObject
class Element : public GameObject; // Also Inherits from GameObject
Now, with this, the dynamic_cast<> can be used to convert a pointer to a GameObject [which you're saying is returned from GetUserData()] into either:
a) Character, or;
b) Element...
Thus,
GameObject *objA = reinterpret_cast<GameObject*>(...Shape1->...->GetUserData());
GameObject *objB = reinterpret_cast<GameObject*>(...Shape2->...->GetUserData());
Character *fchar = NULL;
Element *felement = NULL;
fchar = dynamic_cast<Character*>(objA);
if (NULL != fchar) {
// objA was a Character...
foundChar = true;
// Since objA was a Character, then objB *MUST* be an Element...
felement = dynamic_cast<Element*>(objB);
foundElement = true;
} else {
// objA was *NOT* a Character...
felement = dynamic_cast<Element*>(objA);
fchar = dynamic_cast<Character*>(objB);
foundChar = true;
foundElement = true;
}
As far as my understanding goes, the above code should work (slightly modified for brevity); however, I personally would not use this approach. Given that my assumptions above are true (with regards to the inheritance structure above) - I (also assuming that GetUserData() is returning a GameObject*) would make the following design changes:
First, I would add an abstract method to class GameObject:
class GameObject {
public:
virtual void HandleCollision( GameObject* collides_with ) = 0;
};
The HandleCollision() method would be a *required* addition to any inherited classes (ie: Character or Element) that would handle the collisions between itself and the collides_with GameObject...
This will allow the "short-and-sweet" form of:
// I know objA/objB are GameObjects - just not whether they are Character or Element...
GameObject *objA = reinterpret_cast<GameObject*>(Shape1->...->GetUserData());
GameObject *objB = reinterpret_cast<GameObject*>(Shape2->...->GetUserData());
// Tell objA to Handle the Collision with objB...
objA->HandleCollision(objB);
In this approach, we *know* that GetUserData() - in this case - is *matter-of-factly* returning a pointer to a GameObject class; we just don't know if it's a Character or an Element.
Additionally, in either case, since it's a GameObject being returned (whether Character or Element), it knows how to handle its own collisions (as defined by the HandleCollision() method). And thus polymorphism kicks in...
I've probably confused you even more, but I hope not... I do hope this helps;
JTK