Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am developing an iPhone game. There is a fountain that the user should not be able to walk through. Now, this question is about checking circle-point collision or anything like that. I am checking collision with CGRectIntersectsRect(character1.frame, fountain.frame).

But, here is the problem: If I prevent the person from moving; he will never be able to move. So, I tried combatting this with following:

    if ( > && CGRectIntersectsRect(character1.frame, fountain.frame) && g == false) {
        g = true; = CGPointMake(,;
    if ( > && CGRectIntersectsRect(character1.frame, fountain.frame)) { = CGPointMake(,;
    if ( < && CGRectIntersectsRect(character1.frame, fountain.frame)) { = CGPointMake(,;
    if ( < && CGRectIntersectsRect(character1.frame, fountain.frame)) { = CGPointMake(,;

This should check what side the character1 is intersecting the fountain and then make it so it is not intersecting the fountain by incrementing or decrementing the x or y coordinates of the characters it. But, this still gives weird results. It will move it diagonally across the fountain if keep holding down on the the top side of the fountain and also, it will keep moving it left other wise on the top of the fountain.

share|improve this question
up vote 6 down vote accepted

Speaking generally, the way this is handled is by moving you backwards along the collision normal. You find the axis the collision took place on (normal is a physics term, just think of it as the vector indicating the direction you penetrated the other object. Then you figure out how far you have to move back on that vector to no longer be colliding. See for more information.

share|improve this answer

I would check for the collision before the character is moved to the new position. So that when you update the position of the character you know that it's valid.

share|improve this answer
This is okay for slow moving objects, but anything that can move a sizable amount in a single frame will look weird. – coderanger Jul 24 '10 at 23:58
This isn't suitable for all types of game, but it is for many 2D games where characters move at constant speeds. It's certainly the easiest thing to do. – SpoonMeiser Jul 25 '10 at 1:39
Great points, I was just trying to make it simpler for his case. – Steve Tranby Jul 28 '10 at 22:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.