I got a basic physics engine working.
It's a 2D physics engine, currently only supporting AABBs and Spheres.
Let me get to the point:
I have the following classes:
Entity:
Controls forces and position. Has the following important functions:
void HandleCollision(Entity* pEntity, ShtrVector2 *pCollisionPoint, ShtrVector2 *pCollisionNormal)
This function only changes forces and position on itself, not on the entity passed through the function, the other entity is just used for retrieving mass/bounciness variables
virtual Update(float fTimeFactor)
Update position and acceleration through forces
Collidable:
Is a collidable object. Important functions:
virtual bool DoesCollide(ShtrCollidable *pCollideable)
simple virtual functions, when called directly through a collidable object, returns false, to prevent errors.
virtual void CreateBoundingSphere
when called on collidable object, destroys the current bounding sphere and sets it to NULL to prevent errors.
CollidableAABB (public Collidable, public Entity):
Implements DoesCollide and CalculateBoundingSphere further. Also implements update function so it also moves it's AABB
CollidableSphere (public Collidable, public Entity):
Same as CollidableAABB, just with a sphere
My problem
Inside the DoesCollide function, I have some very nasty typeid's. This is the code (this is the one for the AABB):
bool ShtrCollidableAABB::DoesCollide(ShtrCollidable *pCollidable)
{
if (GetBoundingSphere()->Intersect(*pCollidable->GetBoundingSphere())) {
if (typeid(*pCollidable) == typeid(ShtrCollidableAABB)) {
ShtrCollidableAABB *pDummy = dynamic_cast<ShtrCollidableAABB*> (pCollidable);
if (this->GetAABB()->Intersect(*pDummy->GetAABB())) {
return true;
}
} else if (typeid(*pCollidable) == typeid(ShtrCollidableSphere)) {
ShtrCollidableSphere *pDummy = dynamic_cast<ShtrCollidableSphere*> (pCollidable);
if (this->GetAABB()->Intersect(*pDummy->GetSphere())) {
return true;
}
}
}
return false;
}
Is there a way to get rid of these typeid's with polymorphism and good base classes (I haven't got a clue how to do this)? Or is this the only way to go?
Thanks in advance,
Max Henger