I came across the following code in a game book but I can't get it, does it check if a point (left, top) in a rectangle?

bool CheckCollision(float left, float top,  float SpriteX, float SpriteWidth, float SpriteY, float SpriteHeight)
    return !((left >SpriteX + SpriteWidth) ||
            (top > SpriteY + SpriteHeight) ||
            (SpriteX > left + SpriteWidth) ||
            (SpriteY> top +SpriteHeight));

  • \$\begingroup\$ Yes, that's what it seems to be doing. Do you have any specific questions about how it does this? It seems really trivial to me, but on the other hand I have no idea of how much you know about C++, so I wouldn't know where to begin my explanation. \$\endgroup\$ – Philipp Feb 5 '15 at 21:28
  • \$\begingroup\$ Just a visual figure is enough. So the above function is different than Rect to Rect collision ? \$\endgroup\$ – andre Feb 5 '15 at 21:36

I don't have enough reputation to comment, so I will post this as an answer.

To get the most out of this exercise, I suggest you take a piece of paper, draw a rectangle and then try to map out each check on the paper.

For example: if the left coordinate of my actor is bigger than the wall, it will look like so.

Hopefully you understand what I mean, it's hard to explain with words. :P

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.