Well actually most troubles i have with my current solutions is, that i would need somthing like the java "instanceof" operator -
so i need to check wheter some typeid(XXX) is a base of typeid(YYY). Ideally something like dynamic_cast<typeid(target)>(source).
Ahh i guess i just gave myself the answer:
Code:
// The base collider provides methods which return true if the object may be part of the relation
class ICollider
{
virtual bool is_collision(const IGeometricObject &r_lhs, const IGeometricObject &r_rhs) const = 0;
virtual bool does_accept(const IGeometricObject &r_lhs, const IGeometricObject &r_rhs) const = 0;
};
template <typename A, typename B>
class AbstractCollider
: public ICollider
{
public:
typedef A First;
typedef B Second;
virtual bool does_accept(const IGeometricObject &r_lhs, const IGeometricObject &r_rhs) const
{
return (dynamic_cast<const First*>(&r_lhs) != NULL) &&
(dynamic_cast<const Second*>(&r_rhs) != NULL);
}
bool is_collision(const IGeometricObject &r_lhs, const IGeometricObject &r_rhs) const
{
return __is_collision(dynamic_cast<const First&>(r_lhs),
dynamic_cast<const Second &>(r_lhs));
}
// The actual implementation belongs to some derived class
virtual __is_collision(const First &r_lhs, const Second &r_rhs) const = 0;
};
// So all a collider writer now has to do is to inherit from AbstractCollider
// and the does_accept method automatically returns true for the correct types.
class Custom_Sphere_Sphere_Collider
: public AbstractCollider<ISphere, ISphere>
{
public:
bool __is_collision(const ISphere &r_lhs, const ISphere &r_rhs)
{
// Implementation goes here
}
};
// Eventually, there can be a container where colliders can be registered. The container
// can lookup colliders lazily when they are needed (using the does_accept method),
// and cache the result, so that the next collider lookup can happen in constant time
// (if colliders are stored in a matrix, and the types of the objects that can collide
// are LAZILY assigned appropriate index values)
// now i just need to figure out how to get rid of that lazily...