Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

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 sorry if this was already answered but I couldn't find the answer. So I am doing a tile based platformer and when I was making collision I had quiet a struggle I looked here and googled for answers but they all seems to be overcomplicated for such a simple task/game.

I did come up with this code and it works almost fine:

Rectangle boundRect = new Rectangle((int)position.X, (int)position.Y,
            (int)(sprite.Texture.Width * scale), (int)(sprite.Texture.Height * scale));

        Vector2 previousPosition = position;
        Vector2 previousVelocity = velocity;

        if (boundRect.Intersects(collider))
            velocity = Vector2.Zero;
            position = previousPosition - previousVelocity;

It works almost as good as I want, axcept few things. When I constantly move (i.e.) to left of a collider object(tile) my player sprite is shaking, if thats right word to use, for like 1-3 pixels from edge of object, also in this state I can't move any other direction (i.e. jumping up) till I stop my current movement.

So my question is basicly: "Can I improve this while keeping simplicity, or I need to use better(more complex) approach?"

P.S. Sorry for my English, it is not my native language.

share|improve this question
up vote 3 down vote accepted

You are going to need to do your move/collision in two steps, once for the horizontal direction, and another time for the vertical direction.

  1. Move Horizontal
  2. Check for horizontal collision - adjust position if needed
  3. Move Veritcal
  4. Check for veritcal collision - adjust position if needed

Doing the collision check like this should solve your stuttering issue. Checking both directions at once is fine for collision detection, however, when dealing with collision response it is best to do each direction individually.

share|improve this answer
Thanks very much, I think that helped! This is how I did it now: link I also extended Rectangle a bit but thats not my code I find it somewhere on internet – RedPanda May 7 '13 at 18:08

If it's wobbling like you describe, then the game is drawing both before the collision occurs, and after the collision occurs. Make sure your Update function moves all of your objects first, and then handles collisions before that same Update call returns. That way, only the result of all collisions is drawn.

share|improve this answer
@RedPanda I agree. It seems you are checking for collision before you move your object on screen. – Jon May 7 '13 at 11:17
@Jon yeah big thakns guys, I did checked it before moving, but thats resolved only half of the problem. I still can't figure why I can't move in other direction when colliding and moving in other direction at same time. – RedPanda May 7 '13 at 12:46

I think the reason your sprite is shaking is because you're moving it back from the object based on previousVelocity. I'm assuming that when you press or hold the movement, it sets the velocity. If this is true, then I think what's happening is:

  1. Your player is moving towards an obstacle.
  2. Player's next move intersects obstacle. (boundRect.Intersects(collider))
  3. You move player in the opposite direction. (position = previousPosition - previousVelocity;)
  4. Player is holding down button and velocity gets set again since the player is not against the obstacle.
  5. GOTO 1

Hence, your player would appear to bounce back and forth.

So, what you need to do is make the player go backwards to the point where they are resting against the obstacle instead of the full velocity. Then, ensure that when they have the button pressed down, the velocity doesn't get set if they are against something or it doesn't get set if there is an obstacle within the initial movement range (i.e. position + velocity).

share|improve this answer

Your Answer


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

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