2

I am working on a simple DirectX 9 2d game using sprites. I have written (and pieced together some code snippets I found from books and online) a small engine and am now up to the Collision detection element of the application. I am receiving stack overflow errors at the function I have created for collision detection:

UINT Game::IsCollision(int a, int b)
{
  if(a != b)
  {
    if((m_Sprites[a].posrect.bottom >= m_Sprites[b].posrect.top) && (m_Sprites[a].posrect.top <= m_Sprites[b].posrect.bottom) && (m_Sprites[a].posrect.right >= m_Sprites[b].posrect.left) && (m_Sprites[a].posrect.left <= m_Sprites[b].posrect.right))
    {
      if(m_Sprites[a].posrect.top < m_Sprites[b].posrect.top && m_Sprites[a].posrect.top < m_Sprites[b].posrect.bottom && m_Sprites[a].posrect.bottom < m_Sprites[b].posrect.bottom)
      {
        return BOTTOM;
      }
      else if(m_Sprites[b].posrect.top < m_Sprites[a].posrect.top && m_Sprites[a].posrect.top < m_Sprites[b].posrect.bottom && m_Sprites[b].posrect.bottom < m_Sprites[a].posrect.bottom)
      {
        return TOP;
      }
      else if( m_Sprites[a].posrect.left < m_Sprites[b].posrect.left && m_Sprites[b].posrect.left < m_Sprites[a].posrect.right && m_Sprites[a].posrect.right < m_Sprites[b].posrect.right)
      {
        return RIGHT;
      }
      else if( m_Sprites[b].posrect.left < m_Sprites[a].posrect.left &&  m_Sprites[a].posrect.left < m_Sprites[b].posrect.right &&  m_Sprites[b].posrect.right < m_Sprites[a].posrect.right)
      {
        return LEFT;
      }
    }
    else
    {
       return NOCOLLISION;
    }
  }
  return NOCOLLISION;
}

Also here is how I am calling the function:

void Game::UpdateSprite(int e)
{
  UINT collision;
  for(int f = 0; f < MAX_SPRITES; f++)
  {
    if(m_Sprites[f].occupied && m_Sprites[e].occupied && f != e)
    {
      collision = IsCollision(f, e);

      switch (collision)
      {
        case BOTTOM:
          m_Sprites[e].movey *= -1;
          m_Sprites[f].movey *= -1;
          break;

        case TOP:
          m_Sprites[e].movey *= -1;
          m_Sprites[f].movey *= -1;
          break;

        case RIGHT:
          m_Sprites[e].movex *= -1;
          m_Sprites[f].movex *= -1;
          break;

        case LEFT:
          m_Sprites[e].movex *= -1;
          m_Sprites[f].movex *= -1;
          break;

        case NOCOLLISION:
          break;
      }
      UpdateSprite(e);
      UpdateSprite(f);
    }
  }
}

1 Answer 1

3

Remove the calls to UpdateSprite from the bottom of UpdateSprite. When a function calls itself without a termination case for the recursion, you'll get infinite recursion and a stack overflow.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.