Original Post
Hi all, I am using XNA and C#, but it's a general programming so I am putting it here. I am trying to implement collision detection this way: There are two concrete objects derived from the IGameObject interface. I was hoping the overloaded method HandleCollision(Snake snake, Dot dot) will be called, but it seems that it does not work. What are other OOP strategies for handling collision?
private void CheckCollisions(GameTime gameTime)
{
foreach (IGameObject collider in gameObjectsList_)
{
foreach (IGameObject collidee in gameObjectsList_)
{
// Make sure that we are not comparing the same object
if (collider != collidee)
{
if (collider.CheckCollision(gameTime, collidee))
{
HandleCollision(collider, collidee);
}
}
}
}
}
/// <summary>
/// If snake collides with dot
/// </summary>
/// <param name="snake"></param>
/// <param name="dot"></param>
protected virtual void HandleCollision(Snake snake, Dot dot)
{
// Add new segment to snake
// Remove dot from the list
gameObjectsList_.Remove(dot);
}
/// <summary>
/// Unspecified collision
/// </summary>
/// <param name="collider"></param>
/// <param name="collidee"></param>
protected virtual void HandleCollision(IGameObject collider, IGameObject collidee)
{
// Do nothing
}